【问题标题】:Continuous call to repaint using thread.start() not working使用 thread.start() 连续调用重绘不起作用
【发布时间】:2015-03-29 06:45:33
【问题描述】:

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

但是经过三个动作(以“Inpaint component”打印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());
        }
    }
}

我做错了什么。?

【问题讨论】:

    标签: java multithreading swing repaint


    【解决方案1】:

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

    【讨论】:

    • 谢谢。这解决了这个问题。顺便说一句,当我创建两辆汽车并添加到框架时,我现在面临一个新的但相关的问题。我是新来的,所以我应该创建一个新问题..谢谢..
    • @SabraOssen 看看我编辑的答案。你应该努力弄清楚这段代码在做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多