gpt4 book ai didi

java - 为单独的对象设置动画

转载 作者:行者123 更新时间:2023-12-01 23:08:01 24 4
gpt4 key购买 nike

我一直在开发“电梯模拟器”,我必须为“电梯”制作动画。我能够创建不同的电梯对象,这是通过让每个电梯对象都具有宽度、高度和坐标参数来实现的。然后,我将它们全部存储到一个数组中,并使用 JPanel 的 PaintComponent 方法使用 for 循环将它们绘制到我的框架中。

你们能建议一种方法或给我建议来使它们彼此分开动画,比如独立地上下移动它们吗?当我只有一部电梯时,我能够让它移动,但当我尝试将其应用于多部电梯时,它不起作用。

我之前的尝试涉及一个 ActionListener(监听按钮按下以“启动”动画),它仅更改单个电梯的 x 和 y 坐标。那么我该如何使用多部电梯来做到这一点(电梯的数量对用户来说是任意的)。我是否必须制作与电梯一样多的 ActionListener,以便它可以独立地监听它们?

这是在只有一部电梯时起作用的 ActionListener。它仅向上移动到目前为止。

  private ActionListener timerActionUp = new ActionListener()
{
private int iterator = 0;
private int top = 0;
public void actionPerformed(ActionEvent e)
{
if(top<floorQueue.length){
if(iterator<floorQueue[top]){ //floorQueue is so that the elevator knows where to stop
if(movefloorup<VERTICALFLOORDISTANCE*6){ //this is when the elevator reaches the "top" floor that can fit into the screen, and moves to the next column representing the floors further up
repaint();
movefloorup = movefloorup + VERTICALFLOORDISTANCE;
System.out.println(movefloorup);
iterator++;
}
else{
//timer.stop();
repaint();
switchmarker = 1; //makes elevator moves to the next column
movefloorup = 0;
iterator++;

}
}
else
{
System.out.println("Picking up passengers...");
elevatorCapacity = elevatorCapacity + peopleQueue[floorQueue[top]];
System.out.println("Passengers in elevator: "+elevatorCapacity);
peopleQueue[floorQueue[top]] = 0; //Updates the number of people in the elevator
top++;
if(elevatorCapacity >= 5)
{
System.out.println("WARNING! ELEVATOR FULL!");
elevfull = 1;
}
//timer.stop();
}

}
else
{
System.out.println("Done picking up passengers.");
timer.stop();
}



}
};

非常感谢!

最佳答案

"Do I have to make as many ActionListeners as there are elevators, so it can listen to them independently?"

不,那需要多个计时器。尽可能避免这样做。

"Can you guys suggest a way or give me advice to animate them separately from each other, like say move them up and down independently?"

您应该做的是尝试在 Elevator 类中的方法中实现业务逻辑,然后在循环数组中的所有 Elevators 时调用这些方法。

两个使电梯看起来独立移动,您可以有标志,例如在您的move方法中,例如

public void move() {
if (move) {
// do something
}
}

无论你让电梯移动的原因是什么,这都将是升旗的原因。反之亦然。也许类似于 if (onFloor) { lift.move = false },可能持续 20 个计时器“迭代”,并在电梯类中保留计数,即当计数达到 20 时将重置回 0,然后 move 将恢复为 true。

<小时/>

这是一个您可以使用的示例。我花了大约20分钟就放弃了。逻辑有点不对劲,但基本上指出了我提到的想法。也许你会有更好的运气。您还可以看到移动不同对象的一个​​很好的工作示例 here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ElevatorAnimate extends JPanel {

private static final int D_W = 300;

private static final int FLOORS = 6;
private static final int FLOOR_HEIGHT = 100;
private static final int BUILDING_HEIGHT = FLOORS * FLOOR_HEIGHT;
private static final int D_H = BUILDING_HEIGHT;
private static final int BUILDING_BASE = BUILDING_HEIGHT;
private static final int ELEVATOR_HEIGHT = 60;
private static final int ELEVATOR_WIDTH = 30;

private final List<Elevator> elevators;

public ElevatorAnimate() {
elevators = createElevators();

Timer timer = new Timer(50, new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (Elevator el : elevators) {
el.move();
}
repaint();
}
});
timer.start();
}

private List<Elevator> createElevators() {
List<Elevator> list = new ArrayList<>();
list.add(new Elevator(6, 30));
list.add(new Elevator(4, 90));
list.add(new Elevator(2, 150));
return list;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawFloors(g);
for (Elevator el : elevators) {
el.drawElevator(g);
}
}

private void drawFloors(Graphics g) {
for (int i = 1; i <= FLOORS; i++) {
g.drawLine(0, FLOOR_HEIGHT * i, D_W, FLOOR_HEIGHT * i);
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}

public class Elevator {

int floor, x, y;
boolean move = false;
boolean up = true;
int stopCount = 0;

public Elevator(int floor, int x) {
this.floor = floor;
y = BUILDING_HEIGHT - (floor * FLOOR_HEIGHT);
this.x = x;
}

public void drawElevator(Graphics g) {
g.fillRect(x, y, ELEVATOR_WIDTH, ELEVATOR_HEIGHT);
}

public void move() {
if (y <= 0) {
up = false;
} else if (y >= BUILDING_BASE + ELEVATOR_HEIGHT) {
up = true;
}

if (isOnFloor()) {
move = false;
}

if (move) {
if (up) {
y -= 2;
} else {
y += 2;
}
} else {
if (stopCount >= 20) {
move = true;
stopCount = 0;
} else {
stopCount++;
}
}
}

private boolean isOnFloor() {
return y / FLOOR_HEIGHT == 100;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new ElevatorAnimate());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

关于java - 为单独的对象设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22482196/

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