简体   繁体   English

有没有办法多次执行paintComponent()中的代码?

[英]Is there a way to execute the code in paintComponent() more than once?

I want to move the image around the screen in correlation to the mouse's x and y position.我想根据鼠标的 x 和 y 位置在屏幕上移动图像。 Every time the mouse is clicked, the image should move to that position.每次单击鼠标时,图像都应移动到该位置。

public class testSquare {
  public static void main(String[] args) throws Exception {
    //...
    //object that stores x and y of mouse click
    testSquareInfo obj = new testSquareInfo(0, 0);
    //...
    //panel that draws image, seems to only execute once
    JPanel p = new JPanel ()
    {
        protected void paintComponent(Graphics a){ 
            int x = obj.getXPos();
            int y = obj.getYPos(); 
            a.drawImage(img, x, y, 50, 50, null);
        }        
      };
    //listens for mouse click and sends x and y to object
    p.addMouseListener(new MouseAdapter() {
    @Override 
    public void mousePressed(MouseEvent e) {
        int xMouse = e.getX();
        int yMouse = e.getY(); 
        obj.changeX(xMouse);
        obj.changeY(yMouse); 

        System.out.println(xMouse+" "+yMouse);
    }
    }); 

    window.add(p);
    window.setVisible(true); 
  }  
 } 

//Second Class

public class testSquareInfo 
{
    private int x, y; 
    public testSquareInfo(int x, int y)
    {
       this.x = x;
       this.y = y;
    }
    public void changeX(int xNew)
    {
        x = xNew;
    }
    public void changeY(int yNew)
    {
        y = yNew;
    }
    public int getXPos()
    {
        return x;
    }
    public int getYPos()
    {
        return y;
    } 
} 

Upon running the code, the image is drawn on the window at the 0, 0, coordinate, since the x and y are initialized to these values.运行代码后,图像在窗口的 0, 0, 坐标处绘制,因为 x 和 y 被初始化为这些值。 Clicking around the screen does not move the image, but does properly update the x and y stored in the object.在屏幕周围单击不会移动图像,但会正确更新存储在对象中的 x 和 y。 At one point during testing, the image did move to a different position on the screen.在测试过程中的某个时刻,图像确实移动到了屏幕上的不同位置。 However, this happened when I was not clicking on the window and I have not been able to replicate it since.然而,这发生在我没有点击窗口时,我一直无法复制它。 I am unsure as to when or how it moved.我不确定它何时或如何移动。 Thanks,谢谢,

Add a p.repaint();添加一个p.repaint(); to your mousePressed method.到您的mousePressed方法。 This causes the Panel to be redrawn.这会导致重新绘制面板。

However doing only this will just add another Image to the graphics component.但是,仅这样做只会向图形组件添加另一个图像。

If you do not want to maintain the image at the previous position,如果不想将图像保持在之前的位置,
add a super.paintComponent(a);添加一个super.paintComponent(a); to the beginning of your paintComponent method到你的paintComponent方法的开头
to have the panel in its blank state.使面板处于空白状态。

JPanel p = new JPanel() {

    protected void paintComponent(Graphics a){ 

        super.paintComponent(a);

        int x = obj.getXPos();
        int y = obj.getYPos(); 
        a.drawImage(img, x, y, 50, 50, null);
    }        
  };

//listens for mouse click and sends x and y to object
p.addMouseListener(new MouseAdapter() {
    @Override 
    public void mousePressed(MouseEvent e) {
        int xMouse = e.getX();
        int yMouse = e.getY(); 
        obj.changeX(xMouse);
        obj.changeY(yMouse); 

        System.out.println(xMouse+" "+yMouse);
        p.repaint();
    }
}); 

Changed this:改变了这一点:

 p.addMouseListener(new MouseAdapter() {
    @Override 
    public void mousePressed(MouseEvent e) {
        int xMouse = e.getX();
        int yMouse = e.getY(); 
        obj.changeX(xMouse);
        obj.changeY(yMouse); 

        System.out.println(xMouse+" "+yMouse);
    }
    }); 

To This:对此:

 p.addMouseListener(new MouseAdapter() {
    @Override 
    public void mousePressed(MouseEvent e) {
        int xMouse = e.getX();
        int yMouse = e.getY(); 
        obj.changeX(xMouse);
        obj.changeY(yMouse); 
        p.repaint(); 
        System.out.println(xMouse+" "+yMouse);
    }
    }); 

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

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