如何在 JPanel (Java) 中正确旋转图像
我正在努力旋转加载的图像。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请改用 JScrollPane 。
Use a JScrollPane instead.
您需要将面板放入 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.
好吧,我建议您不要在面板的 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.