使用thread.start()无法正常连续调用重绘

时间:2015-03-29 06:45:34

标签: java multithreading swing repaint

我是Java swing的新手。我想创建2个从屏幕的一端移动到另一端的汽车,现在我用一个测试它。

但经过三次动作(由#34表示;在油漆组件"印刷3次)后,没有动作。

我已附上以下完整代码。

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());
        }
    }
}

我做错了什么。

1 个答案:

答案 0 :(得分:2)

您需要在循环中调用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();
}