简体   繁体   English

我们可以倾斜一个JPanel吗?

[英]Can we tilt a JPanel at an angle?

I have image inside the JPanel . 我在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 ? 是否可以使用Graphics旋转JPanel ,Image是否可旋转,出于好奇而可以旋转JPanel?

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. 我没有进行轮换,但在项目上做得非常成功,还完成了其他仿射转换(上下缩放整个GUI)。 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. 可以尝试在Graphics对象上设置转换,而不是尝试缩放每个组件。 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 ... ie 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. 特别是在Java中渲染的文本(字体)未正确线性缩放(它以与字体大小相对应的离散步骤缩放)的事实在我的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 我从JXTransformer获得了灵感: http : //www.java.net/blog/alexfromsun/archive/2006/07/jxtransformer_t.html

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. 据我所知,您不能旋转JPanel本身,但是您可以使用Java2D旋转JPanel内部的图像。 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 . 如果您覆盖JComponents的paintXxx方法并使用AffineTransform,则实际上可能存在旋转JComponents的方法。

It's not possible to rotate JPanel itself, but it's certainly possible to rotate any image inside. 不可能旋转JPanel本身,但是可以旋转内部的任何图像。 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 . 有很多方法可以做到这一点,例如,可以重写JPanel的public void paint(Graphics g) ,然后将GraphicsGraphics2D It's very useful class, does rotation and much more ;) Check api docs for more info about this one. 这是一个非常有用的类,可以进行轮换等等;)检查api文档以获取有关此信息的更多信息。

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. /* Begin *//* End */都将旋转绘制。

(I didn't test the code, so, they may be some syntax errors...) (我没有测试代码,因此,它们可能是一些语法错误...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM