我只是在爲我的java2d遊戲創建一個多線程渲染器的想法,其中每個線程負責渲染其自己的swingcomponent並提出了一個簡單的程序來嘗試並實現此目的。多線程渲染擺動
*我知道了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());
}
}
注意球只在左側面板彈跳(第一次啓動線程),其他都是固定的每時每刻。
您有問題嗎? –
是的,只有一個線程在進行渲染,其他線程從來沒有開始過(我在問題描述中可能有點不清楚,會編輯,謝謝) – arynaq
這是一個聲明......不是一個問題。 –