Java 带有Swing的交通动画
我一直在用Java编写一个交通模拟,现在想用swing为模拟制作动画。我在我的主要函数中调用RoadNetwork类,如下所示:Java 带有Swing的交通动画,java,swing,Java,Swing,我一直在用Java编写一个交通模拟,现在想用swing为模拟制作动画。我在我的主要函数中调用RoadNetwork类,如下所示: RoadNetwork roadnetwork = new RoadNetwork(); 函数的作用是:获取每辆车的位置作为xy坐标,用于在JPanel上绘制车辆。我的主课名是AMEC。生成的变量显示车辆是否在模拟中生成,完成的变量表示车辆是否已退出模拟,vehiclecounter变量显示整个模拟中的车辆总数,iconNumber变量保存truckIcon所述车辆
RoadNetwork roadnetwork = new RoadNetwork();
函数的作用是:获取每辆车的位置作为xy坐标,用于在JPanel上绘制车辆。我的主课名是AMEC。生成的变量显示车辆是否在模拟中生成,完成的变量表示车辆是否已退出模拟,vehiclecounter变量显示整个模拟中的车辆总数,iconNumber变量保存truckIcon所述车辆分配到的索引
然而,当我运行主程序时,我没有收到任何视觉输出。我做错了什么?另外,插入计时器并使用此程序更新我的视图的最佳方式是什么
public class RoadNetwork extends JPanel {
// create array size of the amount of trucks generated
BufferedImage truckicons[] = new BufferedImage[100];
int populated[] = new int[100]; // array that indicates the identifier of the truck occupying the corresponding position in truckicons[]
public RoadNetwork() throws IOException{
for (int i = 0; i < 100; i++) {
populated[i] = -1; // initialization value
truckicons[i] = ImageIO.read(getClass().getResource("Truck.png")); // assign icon to each truck
}
}
protected void paintComponent (Graphics g) {
super.paintComponent(g);
int coords[] = new int[2];
for (int i = 0; i < 100; i++) {
if (populated[i] != -1) {
coords = AMEC.getcoord(populated[i]);
g.drawImage(truckicons[i], coords[0], coords[1], this);
}
}
for (int k = 0; k < AMEC.vehiclecounter; k++) {
if (AMEC.vehicle[k].spawned == true && AMEC.vehicle[k].finished == false) { // if the truck is somewhere on the plant
if (AMEC.vehicle[k].iconNumber == -1) { // if the vehicle hasn't been assigned an icon yet
for (int l = 0; l < 100; l++) {
if (populated[l] == -1) {
populated[l] = k;
AMEC.vehicle[k].iconNumber = l;
break;
}
}
}
}
else if (AMEC.vehicle[k].spawned == true && AMEC.vehicle[k].finished == true && AMEC.vehicle[k].iconNumber != -1) { // if the vehicle is done but hasn't been cleared from the icon list yet
populated[AMEC.vehicle[k].iconNumber] = -1;
AMEC.vehicle[k].iconNumber = -1;
}
}
this.repaint();
}
}
公共级道路网扩展JPanel{
//创建生成的卡车数量的数组大小
BuffereImage truckicons[]=新BuffereImage[100];
int-populated[]=new int[100];//数组,指示占据truckicons中相应位置的卡车的标识符[]
公共道路网络()引发IOException{
对于(int i=0;i<100;i++){
填充的[i]=-1;//初始化值
truckicons[i]=ImageIO.read(getClass().getResource(“Truck.png”);//为每个卡车分配图标
}
}
受保护组件(图形g){
超级组件(g);
int-coords[]=新的int[2];
对于(int i=0;i<100;i++){
如果(填充[i]!=-1){
coords=AMEC.getcoord(填充[i]);
g、 drawImage(truckicons[i],coords[0],coords[1],this);
}
}
对于(int k=0;k
如果你需要在后台执行任务,比如动画,你应该使用Swingworker。此类允许您在后台(在另一个线程中)执行长任务,并以托管方式将更改带回UI,这些更改将在正确的线程中执行
有人在这里填写了一个很好的教程:
“另外,插入计时器并使用此程序更新我的视图的最佳方式是什么?”
使用javax.swing.Timer
。基本结构简单。这就像使用带有ActionListener
的按钮,但是计时器
会根据您传递的持续时间,每隔这么多毫秒触发ActionEvent
。构造函数如下所示
Timer(int duration, ActionListener)
因此,示例用法是这样的,其中50是每个要触发的事件的持续时间
public RoadNetwork() {
Timer timer = new Timer(50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do something with image positions like loop through an array to move coordinates
repaint();
}
});
timer.start();
}
另外,在paintComponent()
方法中,不需要调用repaint()
,没有显示什么?卡车图像?我得到了要显示的帧,只是setVisible和add的语法错误。谢谢非常感谢。我将尝试使用您提供的函数进行开发。不要忘记调用timer.start()