繁体   English   中英

调用repaint不会导致在线程中调用paint方法

[英]calling repaint does not result in paint method being called in thread

由于鼠标按下事件,正在执行以下线程:

// simulates bouncing ball
class Ball extends JPanel implements Runnable {  
  int Horz = 200;  
  int Vert = 200;  
  int xDim = 10;  
  int yDim = 10;  
  private int xPos;  
  private int yPos;  
  private int dxPos;  
  private int dyPos;  

  // constructor - set initial ball position to where mouse clicked; deltas to random numbers
  public Ball(int startx, int starty)  
  {  
    xPos   = startx;  
    yPos   = starty;  
    dxPos =  (int) (Math.random() * 5 + 2);  
    dyPos =  (int) (Math.random() * 5 + 2);  
  }
  // logic to update position of ball
  public void move() {

    xPos += dxPos;
    yPos += dyPos;

    if (xPos < 0) {
      xPos = 0;
      dxPos = -dxPos;
    }
    if (xPos + xDim >= Horz) {
      xPos = Horz - xDim;
      dxPos = -dxPos;
    }
    if (yPos < 0) {
      yPos = 0;
      dyPos = -dyPos;
    }

    if (yPos + yDim >= Vert) {
      yPos = Vert - yDim;
      dyPos = -dyPos;
    }
  }

调用Run方法,该方法调用重绘

  @Override
  // run --- paint, sleep, update position - this is being executed
  public void run()     
  {  
    while (true)  
    {         
        System.out.println("I am in RUN");  
        repaint();  

        try
        {
            // sleep thread for 20 milliseconds
            Thread.sleep(20);
        }
        catch (InterruptedException e)
        {
            // interrupted
            System.out.println("Terminated prematurely due to interruption");
        }

        move();               
    }   
  }

但是油漆没有被称为

     // draw ball at current position  
     @Override  
     public void paint( Graphics g )  
     {  
       // THIS IS NOT BEING EXECUTED  
       System.out.println("I am in paint");  

       super.paint( g );  
       g.setColor( Color.BLUE );   
       g.fillOval( xPos, yPos, 10, 10 );   
    }   
}

这是为什么 ? 重涂不是应该调用paint方法吗?

重涂不会立即调用paint; 它计划要重新绘制的组件,这将导致事件队列到达后立即调用绘制。 您很可能通过在其上运行计时器循环来阻止事件分发线程 ,因此它无法处理绘画事件(或任何其他事件)。 在另一个线程上运行循环,或者(更轻松,更安全)将其替换为Swing Timer

Timer t = new Timer(20, (ActionEvent e) -> {
    move();
    repaint();
});
t.start();

@Boan是正确的。 尽管我首先看一下线程是否已启动:

Thread((Ball)ball);
thread.setDaemon(true);
thread.start();

暂无
暂无

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

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