我们可以将 JPanel 倾斜一定角度吗?

发布于 2024-10-21 06:40:32 字数 118 浏览 2 评论 0原文

我在 JPanel 中有图像。我想旋转图像。是否可以使用Graphics旋转JPanel,图像是可旋转的,只是出于好奇是否可以旋转JPanel?

I have image inside the JPanel. I would like to rotate the image. Is it possible to rotate the JPanel using Graphics, Image is rotatable, Just out of curiosity is it possible to rotate JPanel ?

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

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

发布评论

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

评论(4

鹤仙姿 2024-10-28 06:40:32

是的!这是可能的,而且也相当简单。我还没有做过旋转,但我在项目中非常成功地完成了其他仿射变换(向上和向下缩放整个 GUI)。我不明白为什么轮换应该有什么不同。

您可以在 Graphics 对象上设置转换,而不是尝试缩放每个组件。由于这是在所有正在渲染的组件之间共享的,因此您可以“免费”立即转换所有内容。重要的是要认识到转换只是渲染过程步骤......即所有组件仍然相信它们具有您在未转换的世界中给它们的边界(位置+大小)。这给我们带来了正确处理鼠标事件的挑战。为此,您只需在主面板前面添加一块玻璃板即可。此窗格收集所有鼠标事件并对事件应用相反的转换,然后将事件向前发送到所有其他组件。

概念上非常简单!不过,我记得还是需要进行一些调整才能让一切变得清晰。尤其是 java 中渲染的文本(字体)没有正确线性缩放(它以与字体大小相对应的离散步骤缩放)这一事实在我的缩放仿射变换案例中带来了最后的挑战。如果只是旋转的话,也许你不必担心这个问题。

我从 JXTransformer 中获得灵感:http://www.java。网/博客/alexfromsun/archive/2006/07/jxtransformer_t.html

Yes! This is possible and fairly straightforward too. I haven't done rotations but I have done other affine transformations (scaling the entire GUI up and down) very successfully on a project. I cannot see why rotations should be any different.

Instead of trying to scale each component use the fact that you can set a transformation on the Graphics object. Since this is shared between all components being rendered you get all things transformed at once "for free". It is important to realize that the transformation is only a rendering-process-step ... i.e. all components still believe they have the bounds (locations+sizes) which you gave them in the untransformed world. This leaves us with the challenge to deal with mouse-events correctly. To do this you simply add a glass-pane in front of your main-panel. This pane collects all mouse-events and apply a reverse of the transform on the event and then sends the event onward towards all other components.

Conceptually very simple! Still, I remember it took some tweaking to get it all crisp though. Especially the fact that rendered texts (fonts) in java are not correctly linearly scaled (it scales in discrete steps corresponding to font-sizes) imposed a final challenge in my scale-affine-transformation-case. Maybe you don't have to worry about that if you only rotate.

I got my inspiration from JXTransformer: http://www.java.net/blog/alexfromsun/archive/2006/07/jxtransformer_t.html

肥爪爪 2024-10-28 06:40:32

据我所知,您无法旋转 JPanel 本身,但您可以使用 Java2D 旋转 JPanel 内的图像。 这里有一篇文章可能会有所帮助。

编辑:

如果您重写 JComponents(例如 JPanel)的 PaintXxx 方法并使用 AffineTransform

As far as I know you can't rotate a JPanel itself but you might be able to rotate the image inside the JPanel using Java2D. Here's an article that might help.

Edit:

There might actually be a way to rotate JComponents (such as JPanel) if you override their paintXxx methods and use AffineTransform.

吹梦到西洲 2024-10-28 06:40:32

不可能旋转 JPanel 本身,但当然可以旋转内部的任何图像。有很多方法可以做到这一点,例如,您可以覆盖 JPanel 的 public void Paint(Graphics g) ,然后将 Graphics 转换为 Graphics2D.这是一个非常有用的类,可以进行旋转等等;)查看 api 文档以获取有关此类的更多信息。

It's not possible to rotate JPanel itself, but it's certainly possible to rotate any image inside. There are quite a few ways to do that, you can - for example - override JPanel's public void paint(Graphics g) and then cast Graphics to Graphics2D. It's very useful class, does rotation and much more ;) Check api docs for more info about this one.

独自唱情﹋歌 2024-10-28 06:40:32

是的,这是可能的。但您不会旋转面板,但图像:

public void paintComponent(Graphics gg)
{
    Graphics2D g = (Graphics2D) gg;
    g.setRenderingHint(RenderingHints.KEY_ANTI_ALIAS, RenderingHints.VALUE_ANTI_ALIAS_ON);
    AfflineTransform matrix = g.getTransform(); // Backup
    float angle = Math.PI / 4.0f; // 45°
    g.rotate(angle);
    /* Begin */
    g.drawImage(yourImage, [your coordinates], null);
    /* End */
    g.setTranform(matrix); // Restore

}

/* Begin *//* End */ 之间的所有内容都将被旋转绘制。

(我没有测试代码,所以,它们可能是一些语法错误......)

Yes, it is possible. But you won't rotate the panel, but the image:

public void paintComponent(Graphics gg)
{
    Graphics2D g = (Graphics2D) gg;
    g.setRenderingHint(RenderingHints.KEY_ANTI_ALIAS, RenderingHints.VALUE_ANTI_ALIAS_ON);
    AfflineTransform matrix = g.getTransform(); // Backup
    float angle = Math.PI / 4.0f; // 45°
    g.rotate(angle);
    /* Begin */
    g.drawImage(yourImage, [your coordinates], null);
    /* End */
    g.setTranform(matrix); // Restore

}

Everything between /* Begin */ and /* End */ will be drawn rotated.

(I didn't test the code, so, they may be some syntax errors...)

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