【发布时间】:2019-09-15 19:49:39
【问题描述】:
我已经完成了随机化圆圈,现在我的挑战是像实际队列一样将圆圈添加到直线队列(我希望圆圈从左到右排队,最好在框架的顶部。当我调用 removeCircle (),我希望圆圈离开队列并向下移动框架。你能帮我使用 addCircle() 和 removeCircle() 方法吗?提前谢谢你。
public class AirTrafficLanding
{
private static void createAndShowUI()
{
PlanePanel panel = new PlanePanel(1);
JFrame frame = new JFrame("Air traffic Landing System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.add( panel );
frame.pack();
frame.setVisible( true );
panel.startAnimation();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
static class PlanePanel extends JPanel implements ActionListener
{
private ArrayList<Plane> planes = new ArrayList<Plane>();
public PlanePanel(int planeCount)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLayout( null );
setBackground( Color.BLACK );
Random random = new Random();
for (int i = 0; i < planeCount; i++)
{
Plane plane = new Plane();
plane.setRandomColor(true);
plane.setLocation(0, 700);
//plane.setLocation(random.nextInt(screenSize.width), random.nextInt(screenSize.height));
plane.setMoveRate(32, 32, 1, 1, true);
plane.setSize(32, 32);
planes.add( plane );
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (Plane plane: planes)
{
plane.draw(g);
}
}
public void startAnimation()
{
Timer timer = new Timer(55, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
move();
repaint();
}
private void move()
{
for (Plane plane : planes)
{
plane.move(this);
}
}
}
static class Plane
{
public Color color = Color.BLACK;
public int x = 0;
public int y = 0;
public int width = 1;
public int height = 1;
private int moveX = 1;
private int moveY = 1;
private int directionX = 1;
private int directionY = 1;
private int xScale = moveX;
private int yScale = moveY;
private boolean randomMove = false;
private boolean randomColor = false;
private Random myRand = null;
public Plane()
{
myRand = new Random();
setRandomColor(randomColor);
}
public void move(JPanel parent)
{
int iRight = parent.getSize().width;
int iBottom = parent.getSize().height;
x += 5 + (xScale * directionX);
y += 5 + (yScale * directionY);
if (x <= 0)
{
x = 0;
directionX *= (-1);
xScale = randomMove ? myRand.nextInt(moveX) : moveX;
if (randomColor) setRandomColor(randomColor);
}
if (x >= iRight - width)
{
x = iRight - width;
directionX *= (-1);
xScale = randomMove ? myRand.nextInt(moveX) : moveX;
if (randomColor) setRandomColor(randomColor);
}
if (y <= 0)
{
y = 0;
directionY *= (-1);
yScale = randomMove ? myRand.nextInt(moveY) : moveY;
if (randomColor) setRandomColor(randomColor);
}
if (y >= iBottom - height)
{
y = iBottom - height;
directionY *= (-1);
yScale = randomMove ? myRand.nextInt(moveY) : moveY;
if (randomColor) setRandomColor(randomColor);
}
}
public void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x, y, width, height);
}
public void setColor(Color c)
{
color = c;
}
public void setLocation(int x, int y)
{
this.x = x;
this.y = y;
}
public void setMoveRate(int xMove, int yMove, int xDir, int yDir, boolean randMove)
{
this.moveX = xMove;
this.moveY = yMove;
directionX = xDir;
directionY = yDir;
randomMove = randMove;
}
public void setRandomColor(boolean randomColor)
{
this.randomColor = randomColor;
switch (myRand.nextInt(3))
{
case 0: color = Color.ORANGE;
break;
case 1: color = Color.GREEN;
break;
case 2: color = Color.RED;
break;
default: color = Color.BLACK;
break;
}
}
public void setSize(int width, int height)
{
this.width = width;
this.height = height;
}
}
}
我希望从队列中的零个圆圈开始,然后每隔 10 秒添加一个圆圈。我希望每 15 秒后有一个圆圈从队列移动到队列底部。我已经完成了随机化圆圈,现在我的挑战是将圆圈添加到像实际队列一样的直线队列中(我希望圆圈从左到右排队,最好是在框架的顶部。当我调用 removeCircle() 时,我希望圆圈离开队列并向下移动框架。你能帮我解决 addCircle() 和 removeCircle() 方法吗?提前谢谢你。
【问题讨论】:
-
我正在为我的作业寻求帮助。非常感谢任何帮助。
-
我设法得到了这个,但在创建 LinkedList 以将圆圈保持在队列中时仍然面临挑战。我在这里粘贴我的代码
-
为什么需要 LinkedLIst。只需使用 ArrayList。您可以创建 2 个摆动计时器。当“添加”计时器触发时,您将一个圆圈添加到 ArrayList。当“删除”计时器触发时,您从 ArrayList 的索引 0 中删除圆圈。所以你的基本逻辑是正确的。但是,您不需要 Timer 的 ActionListner 中的循环。计时器是循环。它将每 10/15 秒生成一个事件。如果您希望每 10/15 秒发生一次,为什么要为 Timer 使用 40 毫秒?
-
@camickr 谢谢。我现在正在使用 ArrayList。我设法取得了进展。我创建了一个应用程序。我在添加和删除方面遇到了挑战。我的程序现在可以添加 3 个圆圈,但它们不在队列中。我要他们排队。并在 5 秒后移除一圈。 10秒后加一圈。我不知道是否有办法让我分享我的代码。
-
两个摆动计时器的问题是我得到的异常:ConcurrentModificationException。我怎样才能摆脱它??