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

标签 java multithreading swing repaint

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

但是经过 3 次移动(由打印 3 次的“In Paint component”表示)后没有任何移动。

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

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/

相关文章:

java - 我们如何使用 sessionId 取回特定的 session ?

Java 日期偏移格式问题?

Java:如何使 JSpinner 显示具有一定偏移量的值

java - 如何使 JLabel 不影响 GridBagLayout 中其他元素的大小

Java 默认图标集

python - 如何与 Telegram bot python同时工作

c - GNU backtrace_symbols() 和 dladdr() 是线程安全的吗?

C# One Writer Many Readers 只读一次

swing - java网络编程的最佳框架?

java - 从另一个框架获取值