在线程中的多线程渲染

时间:2013-06-04 00:09:33

标签: java swing graphics2d

我正在考虑为我的java2d游戏创建一个多线程渲染器,其中每个线程都负责渲染自己的swing组件,并提出了一个简单的程序来尝试并实现这一点。

*我知道Thread.sleep不是首选的方法,但它有    在我使用的单线程渲染中毫无障碍地工作    主动渲染,我没有用swingtimer测试,而是我的    知识Thread.sleep睡眠调用线程,因此不能    这个问题。

问题)程序在四个线程中创建了四个面板,每个面板都有一个弹跳球,但只有第一个创建的线程执行任何操作,其他线程根本没有启动(可以通过sysout验证)运行方法)。因此只有一个线程进行渲染,其他线程从未有机会运行,sysout:System.out.println("Ballbouncer: " + this + " running.");确认了这一点,视觉(添加了图像)也是如此。

BallBouncer(可运行)

public class BallBouncer implements Runnable {
private ColoredPanel ballContainer;
private List<Ellipse2D.Double> balls;
public static final Random rnd = new Random();
private double speedX, speedY;

public BallBouncer(ColoredPanel container) {
    this.ballContainer = container;
    this.balls = new ArrayList<>();
    balls.add(container.getBall());
    this.speedX = 10 * rnd.nextDouble() - 5;
    this.speedY = 10 * rnd.nextDouble() - 5;
}

public BallBouncer(List<ColoredPanel> containers) {
    for (ColoredPanel p : containers) {
        new BallBouncer(p).run();
    }
}

@Override
public void run() {
    while (true) {
        System.out.println("Ballbouncer: " + this + " running.");
        moveBall();
        ballContainer.repaint();
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

private void moveBall() {
    for (Ellipse2D.Double ball : balls) {
        ball.x += speedX;
        ball.y += speedY;

        if (ball.x < 0
                || ball.x + ball.getWidth() > ballContainer.getWidth()) {
            speedX *= -1;
        }
        if (ball.y < 0
                || ball.y + ball.getHeight() > ballContainer.getHeight()) {
            speedY *= -1;
        }
    }


}

容器

public class ColoredPanel extends JPanel {
private Ellipse2D.Double circle;

public ColoredPanel(Color color) {
    circle = new Ellipse2D.Double(0, 0, 10, 10);
    setBackground(color);
}

public Ellipse2D.Double getCircle() {
    return circle;
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(getBackground().darker());
    g2d.fill(circle);
}

@Override
@Transient
public Dimension getPreferredSize() {
    return new Dimension(400, 400);
}

public Double getBall() {
    return getCircle();
}

主要

public class ColoredPanelContainer extends JPanel {

private List<ColoredPanel> panels = new ArrayList<>();

public ColoredPanelContainer() {
    setUpPanels();
    setBackground(Color.black);
}

private void setUpPanels() {
    for (int i = 0; i < 4; i++) {
        Color color = new Color(BallBouncer.rnd.nextInt(256),
                BallBouncer.rnd.nextInt(256), BallBouncer.rnd.nextInt(256));
        panels.add(new ColoredPanel(color));
    }
    for (int i = 0; i < 4; i++) {
        add(panels.get(i));
    }
}

@Override
@Transient
public Dimension getPreferredSize() {
    return new Dimension(1000, 1000);
}

public List<ColoredPanel> getPanels() {
    return panels;
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    ColoredPanelContainer container = new ColoredPanelContainer();
    frame.getContentPane().add(container);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    new BallBouncer(container.getPanels());
}
}

注意球只在左侧面板中弹跳(第一次开始的线程),其他球在任何时候都是静止的。 enter image description here

1 个答案:

答案 0 :(得分:4)

你这样做:

public BallBouncer(List<ColoredPanel> containers) {
    for (ColoredPanel p : containers) {
        new BallBouncer(p).run();
    }
}

这不是启动线程的正确方法。您所做的只是直接运行run方法而顺序运行。这意味着代码在调用构造函数的同一线程中的该循环中运行。

You should read this tutorial.它解释了如何在Swing中使用线程。即,如何使用javax.swing.SwingWorkerSwingUtilities.invoke*There is also this tutorial,解释了如何使用Swing Timer类。

而且,仅仅是关于线程的进一步教育:Here are ways to start threads in java when you're not using swing您在撰写Swing应用程序时不想使用这些示例

相关问题