[英]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方法吗?
@Boan是正确的。 尽管我首先看一下线程是否已启动:
Thread((Ball)ball);
thread.setDaemon(true);
thread.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.