如何在 JPanel (Java) 中正确旋转图像

发布于 2024-08-26 08:18:14 字数 212 浏览 4 评论 0原文

我正在努力旋转加载的图像。我在 JPanel 上设置图形,然后使用标准 AffineTransform 来旋转它,例如 45 度。 不幸的是,如果图像超出面板区域,则会被剪切。 我如何强制 JPanel 向自身添加滚动条(在加载图像文件时,我想通过添加滚动条来调整 JPanel 的大小,而不调整 JFrame 的大小)。

或者说,如何正确旋转整个图像?

预先感谢您的回复!

I'm working on rotating a loaded image. I set the graphics on a JPanel and then use standard AffineTransform in order to rotate it, say, 45 degrees.
Unfortunately, the image is being cut, if it exceeds the panel area.
How may I force JPanel to add scrolls to itself (while loading an image file, I would like to adjust the size of JPanel by adding the scrolls, without adjusting the size of JFrame).

Or, in other words, how to correctly rotate the whole image?

Thank you in advance for the reply!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明媚殇 2024-09-02 08:18:14

请改用 JScrollPane

Use a JScrollPane instead.

孤檠 2024-09-02 08:18:14

您需要将面板放入 jscrollPane 中,然后确保正确设置了首选大小,以便滚动可用。

You need to put the panel in a jscrollPane and then make sure you have the preferredsize set correctly so that the scroll will be available.

稀香 2024-09-02 08:18:14

好吧,我建议您不要在面板的 PaintComponent() 方法中旋转图像,因为这将导致面板的首选尺寸发生变化,并且您不应该在 PaintComponent() 方法中设置首选尺寸。

相反,您可能应该有一个rotate(...) 方法。此方法将创建所需旋转的 BufferedImage。然后你的paintComponent()方法只是绘制缓冲的图像。然后您可以根据旋转设置首选尺寸。

现在,关于旋转图像的首选大小,请查看此 示例

Well, I would suggest that you should not be rotating the image in the paintComponent() method of your panel since this will result in the preferred size of the panel changing and you should not be setting the preferred size in the paintComponent() method.

Instead you should probably have a rotate(...) method. This method will create a BufferedImage of the desired rotation. Then your paintComponent() method simply paints the buffered image. Then you would set the preferred size based on the rotation.

Now regarding the preferred size of the rotated image check out this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文