【问题标题】:Why must x and y coordinates be initialized inside paintComponent()?为什么必须在paintComponent() 中初始化x 和y 坐标?
【发布时间】:2015-01-28 02:31:30
【问题描述】:

Exercise1609编写一个使用箭头键绘制线段的程序。当按下右箭头键、上箭头键、左箭头键或下箭头键时,线条从框架的中心开始向东、北、西或南方向绘制。简而言之,画一个迷宫。有关我的问题的描述,请参阅下面的 cmets。

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Exercise1609 extends JFrame {

        private KeyboardPanel panel = new KeyboardPanel();

        public Exercise1609() {
            add(panel);
            panel.setFocusable(true);
        }

        public static void main(String[] args) {
            Exercise1609 frame = new Exercise1609();
            frame.setTitle("Tegn med piltaster");
            frame.setSize(600, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

       //The panel that listens for key and responds by drawing             
       public static class KeyboardPanel extends JPanel {

            private int x,y,previousX,previousY;
            private boolean firstTime = true;

            public KeyboardPanel() {

                /**
                 * why must x and y be initialized inside paintComponent? 
                 * if I want to start drawing from the middle of the panel?
                 * If I remove the if-block inside paintComponent and instead
                 * place the initialization here, as shown with the two lines below:
                 * x = previousX = getWidth() / 2;
                 * y = previousY = getHeight() / 2;
                 * ...then the program will not start to draw from the middle,
                 * but upper left corner of the screen
                 */
                addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        previousY = y;
                        previousX = x;          
                        switch (e.getKeyCode()) {
                        case KeyEvent.VK_DOWN:
                            y++;
                            break;
                        case KeyEvent.VK_UP:        
                            y--;
                            break;
                        case KeyEvent.VK_RIGHT:
                            x++;
                            break;
                        case KeyEvent.VK_LEFT:
                            x--;
                            break;
                        }
                        repaint();
                    }
                });
            }//end constructor

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponents(g);

                if(firstTime) {
                 //Why can't x and y be initialized outiside paintComponent?
                 //Why can't they be initialized inside the constructor of this class?
                 x = previousX = getWidth() / 2;
                 y = previousY = getHeight() / 2;
                 firstTime = false;
                }
                g.drawLine(previousX, previousY, x, y);
                System.out.println(x + " " + y);

            }
        }

    }

如果我尝试在其他任何地方初始化 x 和 y,最后一行 System.out.println(x + " " + y); 输出 0,0 但是paintComponent()。在paintcomponent() 内部初始化时,输出为 292,131...这是我想要的。

【问题讨论】:

标签: java paintcomponent


【解决方案1】:

getWidth()getHeight() 直到 UI 元素通过布局传递后才会正确设置。这保证会在调用 paintComponent() 之前发生,但可能不会在您尝试调用它们的其他点发生。

见:getWidth() and getHeight() are 0 after calling setPreferredSize()

如果在设置/更改组件的宽度和高度时需要通知您,请查看ComponentListenerhttp://docs.oracle.com/javase/7/docs/api/java/awt/event/ComponentListener.html

【讨论】:

  • 您认为在设置坐标之前手动验证会解决问题吗?
  • 你的意思是:“这不保证客人......”?
  • @Vince Emigh:如果我要解决这个问题,我会考虑在单独的“文档”坐标系中将线条绘制到显示它们的坐标系中。然后将面板视为文档的视口。这对于家庭作业来说可能是多余的,所以我可能只是跟踪 offsetX/offsetY 并从面板的当前中心绘制到 (center + offset)。
  • @David Brossard:我不确定你指的是什么……也许某些东西已经被编辑或删除了?
  • 我按照推荐尝试了 componentListener。现在一切正常。只是想让你知道。
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多