Java 如何创建一个LinkedList,每10秒添加一个圆,每15秒删除第一个元素(圆)
我已经完成了对圆圈的随机化,现在我的挑战是将圆圈添加到与实际队列类似的直线队列中(我希望圆圈从左到右排队,最好在帧的顶部。当我调用removeCircle()时,我希望圆圈离开队列并在帧中向下移动。你能帮我处理addCircle()吗和removeCircle()方法。提前感谢您Java 如何创建一个LinkedList,每10秒添加一个圆,每15秒删除第一个元素(圆),java,swing,canvas,Java,Swing,Canvas,我已经完成了对圆圈的随机化,现在我的挑战是将圆圈添加到与实际队列类似的直线队列中(我希望圆圈从左到右排队,最好在帧的顶部。当我调用removeCircle()时,我希望圆圈离开队列并在帧中向下移动。你能帮我处理addCircle()吗和removeCircle()方法。提前感谢您 public class AirTrafficLanding { private static void createAndShowUI() { PlanePanel panel = new PlanePane
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;
}
}
公共级空中交通着陆
{
私有静态void createAndShowUI()
{
刨花板=新刨花板(1);
JFrame=新JFrame(“空中交通着陆系统”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_二者);
框架。添加(面板);
frame.pack();
frame.setVisible(true);
panel.startAnimation();
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowUI();
}
});
}
静态类PlanePanel扩展JPanel实现ActionListener
{
私有ArrayList平面=新ArrayList();
公共平面面板(int planeCount)
{
维度screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setLayout(空);
挫折地面(颜色:黑色);
随机=新随机();
对于(int i=0;i
}
我希望从队列中的零个圆圈开始,每隔10秒添加一个圆圈。我希望每15秒有一个圆圈从队列移动到队列底部。我已经完成了随机圆圈,现在我的挑战是将圆圈添加到直线队列中,就像实际队列一样(我希望圆圈从左到右排队,最好在帧的顶部。当我调用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;
}
}
因此,请将您的计划分解为基本内容: