Can we tilt a JPanel at an angle?

江枫思渺然 提交于 2019-12-25 00:37:06

问题


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 ?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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...)



来源:https://stackoverflow.com/questions/5271436/can-we-tilt-a-jpanel-at-an-angle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!