gpt4 book ai didi

java - Swing 动画运行速度极慢

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:29 31 4
gpt4 key购买 nike

我当前使用 Java Swing 运行的动画有问题。这是一个离散事件模拟,基于文本的模拟工作正常,我只是在将模拟连接到 GUI 输出时遇到问题。

对于这个例子,我将模拟 10 辆汽车。这些汽车由 JPanels 表示我将在稍后详细说明。

因此请考虑事件 process_car_arrival。每次计划执行此事件时,我都会添加一个 Car反对 ArrayList称为 cars在我的 Model类(class)。 Car类具有以下相关属性:

Point currentPos; // The current position, initialized in another method when knowing route.
double speed; // giving the speed any value still causes the same problem but I have 5 atm.
RouteType route; // for this example I only consider one simple route

此外它还有以下方法move() :

switch (this.route) {
case EAST:
this.currentPos.x -= speed;
return this.currentPos;
.
.
.
//only above is relevant in this example

这一切都很好。所以从理论上讲,当我调用 move() 时,汽车沿着一条从东到西的直路行驶我要移动的每辆车的方法。

回到 process_car_arrival 事件。添加 Car 对象后,它调用方法 addCarToEast()View类(class)。这会在从东到西的道路起点处添加一个 JPanel。

前往 View类现在我有一个 ** 单独的 ** 线程,它执行以下操作( run() 方法):

@Override
public void run() {
while (true) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!cars.isEmpty()) {

cars.get(i).setLocation(
new Point(getModel.getCars().get(i).move()));

if (i == cars.size() - 1) {
i = 0;
} else {
i++;
}
}
}
}

一开始,上面的内容确实使汽车从东向西顺利移动。但是在有 3-4 辆汽车移动后,它最终变得非常慢,而当我有 10 辆汽车移动时,它最终移动得非常小。

只是为了清理,此刻在Model类有一个 ArrayListCar对象,并在 View类还有一个 ArrayListJPanel代表汽车的对象。我正在尝试匹配 Car反对 JPanels ,但我显然在做一个疯狂的工作。

我怀疑我正在做一些非常低效的事情,但我不知道是什么。我最初认为它可能正在访问 ArrayList太多了,我想这会使它变得非常慢。

关于我可以更改哪些内容以使其顺利运行的任何指示?

最佳答案

基于之前的 answer ,下面的示例模拟了一个由三辆出租车组成的车队在矩形网格上随机移动。 javax.swing.Timer 以 5 Hz 的频率驱动动画。模型和 View 在 CabPanel 中紧密耦合,但动画可能会提供一些有用的见解。特别是,您可能会增加出租车的数量或降低计时器延迟。

image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
* @see https://stackoverflow.com/a/14887457/230513
* @see https://stackoverflow.com/questions/5617027
*/

public class FleetPanel extends JPanel {

private static final Random random = new Random();
private final MapPanel map = new MapPanel();
private final JPanel control = new JPanel();
private final List<CabPanel> fleet = new ArrayList<CabPanel>();
private final Timer timer = new Timer(200, null);

public FleetPanel() {
super(new BorderLayout());
fleet.add(new CabPanel("Cab #1", Hue.Cyan));
fleet.add(new CabPanel("Cab #2", Hue.Magenta));
fleet.add(new CabPanel("Cab #3", Hue.Yellow));
control.setLayout(new GridLayout(0, 1));
for (CabPanel cp : fleet) {
control.add(cp);
timer.addActionListener(cp.listener);
}
this.add(map, BorderLayout.CENTER);
this.add(control, BorderLayout.SOUTH);
}

public void start() {
timer.start();
}

private class CabPanel extends JPanel {

private static final String format = "000000";
private final DecimalFormat df = new DecimalFormat(format);
private JLabel name = new JLabel("", JLabel.CENTER);
private Point point = new Point();
private JLabel position = new JLabel(toString(point), JLabel.CENTER);
private int blocks;
private JLabel odometer = new JLabel(df.format(0), JLabel.CENTER);
private final JComboBox colorBox = new JComboBox();
private final JButton reset = new JButton("Reset");
private final ActionListener listener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int ds = random.nextInt(3) - 1;
if (random.nextBoolean()) {
point.x += ds;
} else {
point.y += ds;
}
blocks += Math.abs(ds);
update();
}
};

public CabPanel(String s, Hue hue) {
super(new GridLayout(1, 0));
name.setText(s);
this.setBackground(hue.getColor());
this.add(map, BorderLayout.CENTER);
for (Hue h : Hue.values()) {
colorBox.addItem(h);
}
colorBox.setSelectedIndex(hue.ordinal());
colorBox.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Hue h = (Hue) colorBox.getSelectedItem();
CabPanel.this.setBackground(h.getColor());
update();
}
});
reset.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
point.setLocation(0, 0);
blocks = 0;
update();
}
});
this.add(name);
this.add(odometer);
this.add(position);
this.add(colorBox);
this.add(reset);
}

private void update() {
position.setText(CabPanel.this.toString(point));
odometer.setText(df.format(blocks));
map.repaint();
}

private String toString(Point p) {
StringBuilder sb = new StringBuilder();
sb.append(Math.abs(p.x));
sb.append(p.x < 0 ? " W" : " E");
sb.append(", ");
sb.append(Math.abs(p.y));
sb.append(p.y < 0 ? " N" : " S");
return sb.toString();
}
}

private class MapPanel extends JPanel {

private static final int SIZE = 16;

public MapPanel() {
this.setPreferredSize(new Dimension(32 * SIZE, 32 * SIZE));
this.setBackground(Color.lightGray);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = this.getWidth();
int h = this.getHeight();
g2d.setColor(Color.gray);
for (int col = SIZE; col <= w; col += SIZE) {
g2d.drawLine(col, 0, col, h);
}
for (int row = SIZE; row <= h; row += SIZE) {
g2d.drawLine(0, row, w, row);
}

for (CabPanel cp : fleet) {
Point p = cp.point;
int x = SIZE * (p.x + w / 2 / SIZE) - SIZE / 2;
int y = SIZE * (p.y + h / 2 / SIZE) - SIZE / 2;
g2d.setColor(cp.getBackground());
g2d.fillOval(x, y, SIZE, SIZE);
}
}
}

public enum Hue {

Cyan(Color.cyan), Magenta(Color.magenta), Yellow(Color.yellow),
Red(Color.red), Green(Color.green), Blue(Color.blue),
Orange(Color.orange), Pink(Color.pink);
private final Color color;

private Hue(Color color) {
this.color = color;
}

public Color getColor() {
return color;
}
}

private static void display() {
JFrame f = new JFrame("Dispatch");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FleetPanel fp = new FleetPanel();
f.add(fp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
fp.start();
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
display();
}
});
}
}

关于java - Swing 动画运行速度极慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14886232/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com