gpt4 book ai didi

java - 使用 thread.start() 连续调用重绘不起作用

转载 作者:行者123 更新时间:2023-11-30 08:54:41 25 4
gpt4 key购买 nike

我是 Java swing 的新手。我想创建 2 辆汽车,从屏幕的一端移动到另一端,现在我正在用其中一辆进行测试。

但是经过三个 Action (打印3次“In paint component”表示)没有 Action 。

我在下面附上了完整的代码。

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;

public class Application {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {

JFrame f = new JFrame("Demo");
Car car = new Car();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(car);
f.pack();
f.setVisible(true);

Thread thread = new Thread(car);
thread.start();

}
});
}

}

class Car extends JPanel implements Runnable {

private int xBase = 0, yBase = 50;

@Override
public Dimension getPreferredSize() {
return new Dimension(250, 200);
}

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

System.out.println("In paint component");
xBase = xBase + 20;

// Draw two wheels
g.setColor(Color.BLACK);
g.fillOval(xBase + 10, yBase - 10, 10, 10);
g.fillOval(xBase + 30, yBase - 10, 10, 10);

// Draw the car body
g.setColor(Color.BLUE);
g.fillRect(xBase, yBase - 20, 50, 10);

// Draw the top
g.setColor(Color.DARK_GRAY);
Polygon polygon = new Polygon();
polygon.addPoint(xBase + 10, yBase - 20);
polygon.addPoint(xBase + 20, yBase - 30);
polygon.addPoint(xBase + 30, yBase - 30);
polygon.addPoint(xBase + 40, yBase - 20);
g.fillPolygon(polygon);
}

@Override
public void run() {
try {
validate();
repaint();
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}
}
}

我做错了什么?

最佳答案

您需要调用 repaint()循环中的方法,以便在启动线程后汽车继续移动。例如:

@Override
public void run() {
int limit = 10;
try {
while (limit > 0) {
limit--;
repaint();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}
}

当前 repaint()方法被调用三次,但您看不到任何移动,因为重绘发生在 Car 之前。对象完全可见。

针对您的评论:您可以创建一个方法来绘制汽车,如下所示:

private void drawCar(Graphics g, int x, int y) {
g.fillOval(x, y, 10, 10);
g.fillOval(x + 20, y, 10, 10);
// Draw the car body
g.setColor(Color.BLUE);
g.fillRect(x - 10, y - 10, 50, 10);
// Draw the top
g.setColor(Color.DARK_GRAY);
Polygon polygon = new Polygon();
polygon.addPoint(x, y - 10);
polygon.addPoint(x + 10, y - 20);
polygon.addPoint(x + 20, y - 20);
polygon.addPoint(x + 30, y - 10);
g.fillPolygon(polygon);
}

并在 paintComponent(...) 中调用它多次方法:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("In paint component");
xBase = xBase + 20;
drawCar(g, xBase, yBase);// draw the first car
drawCar(g, xBase + 80, yBase);// draw the second car 80 pixels ahead
drawCar(g, xBase, yBase + 100); // draw the third car 100 pixels lower

g.dispose();
}

关于java - 使用 thread.start() 连续调用重绘不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29326414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com