后覆盖在Java组件的paint方法 [英] Post overriding the paint method of the components in java

查看:1029
本文介绍了后覆盖在Java组件的paint方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的Java AWT或摇摆,当你想改变一些组件通常必须覆盖的方法油漆(图形克)绘画(在AWT)或的paintComponent(图形G)(在摆动)。

 这通常是(也许永诺 - 我不知道)当您创建例如组件完成的:

 的JP​​anel的JPanel =新JPanel(){
                @覆盖
                保护无效paintComponent(图形G){                    super.paintComponent方法(G);                    Graphics2D的G2D =(Graphics2D的)克;
                    // ...我的实现油漆,某些transfromations,旋转等
                }            };

假设你有组件的容器,例如可以由一些的JLabel,有的JTextField的,有的形象。这将是所有放在一个组成部分。
通过容器我的意思是你有一些列表或使用IDS或一些类似的结构,这是你会把在一个JFrame中的所有组件映射。


现在的问题是,如果我能与所有在这个列表中的时刻,当所有的人都已经创建的组件的创建后更改涂装方法。例如,我希望做旋转动作(旋转),这是在Graphisc2D定义,所有的人。


所以basicaly我想那是什么我throught成品的配件我说的列表:
你们(组件),这是在列表中会受到某些角度旋转。那可能吗?如果是的话怎么样?

编辑:

这是我的工作不正常解决方法:

  graphicalDisplayPanel =新JPanel(){
                    @覆盖
                    保护无效paintComponent(图形G){                        super.paintComponent方法(G);
                        G2D =(Graphics2D的)克;
                        g2d.rotate(Math.PI,anchorx,anchorY)结束;
                    }                 @覆盖
                      公共无效paintChildren(图形G){
                           super.paintChildren(G);
                           Graphics2D的g2d2 =(Graphics2D的)克;                        g2d2.rotate(Math.PI,anchorx,anchorY)结束;                      }                };    JFrame中的JFrame JFrame的=();
    // ...设置尺寸,位置,对可见的JFrame等,它工作正常nonrotated    jFrame.setContentPane(graphicalDisplayPanel);


解决方案

  

所以basicaly我想那是什么我throught成品的配件我说的清单:你们(组件),这是在列表中会受到某些角度旋转。


如果你想使用,那么你需要做的风俗画中的paintComponent()方法的面板作为一个单一的所有组件上的旋转盘,因此

如果要旋转,例如,每个人都有的旋转角度不同单个图像,那么你可以再次做到这一点在的paintComponent(...)方法和改变角度为每个组件

或者,在第二种情况下,你可以使用旋转图标类。在这种情况下,图标是刚添加到JLabel。然后,您可以更改度的旋转和重绘的标签,所以没有风俗画(除了在图标本身)。

In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing).
This is usually (maybe allways - I'm not sure) done when you are creating the component for example:

JPanel jPanel = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {

                    super.paintComponent(g);

                    Graphics2D g2d = (Graphics2D) g;
                    //... my implementation of paint, some transfromations, rotation, etc
                }   

            }; 

Imagine that you have container of components which could for example consists of some JLabels, some JTextFields, some image. Which will be all put on one component. By container I mean you have some list or map with ids or some similar structure in which are all components you will put on one JFrame.
The question is if I can change the painting method after creating with all of the components which are in this list in the moment when all of them are already created. For example I want do the rotation action (rotate), which is defined in Graphisc2D, with all of them.
So basicaly what I want is that I throught the list of componets I have and say: "All of you (components) which are in the list will be rotated by some angle". Is that possible? If yes how?

Edit:
This is my not correctly working solution:

  graphicalDisplayPanel = new JPanel() {
                    @Override
                    protected void paintComponent(Graphics g) {

                        super.paintComponent(g);


                        g2d = (Graphics2D) g;
                        g2d.rotate(Math.PI, anchorx, anchory);


                    }

                 @Override
                      public void paintChildren(Graphics g) {
                           super.paintChildren(g);
                           Graphics2D g2d2 = (Graphics2D) g;

                        g2d2.rotate(Math.PI, anchorx, anchory);

                      }

                };

    JFrame jFrame = JFrame();
    // ... setting dimension, position, visible etc for JFrame, it works correctly nonrotated

    jFrame.setContentPane(graphicalDisplayPanel);

解决方案

So basicaly what I want is that I throught the list of componets I have and say: "All of you (components) which are in the list will be rotated by some angle".

If you want to rotate panel and therefore all the components on the panel as a single using then you need to do the custom painting in the paintComponent() method.

If you want to rotate, for example, individual images that each have a different angle of rotation then you can again do this in the paintComponent(...) method and change the angle for each component.

Or, in this second case you can use the Rotated Icon class. In this case the Icon is just added to a JLabel. Then you can change the degrees of rotation and repaint the label, so there is no custom painting (except in the Icon itself).

这篇关于后覆盖在Java组件的paint方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆