import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; public class RobotAnimation extends JPanel implements ActionListener { private static final int NUM_ROBOTS = 6; private static final int SPACING = 90; private static final int ROBOT_Y = 120; private static final int SPEED = 10; private static final int MOVE_FRAMES = 8; // frames spent moving private static final int PAUSE_FRAMES = 10; // frames spent paused private final List robots = new ArrayList<>(); private final Timer timer; private int frameCount = 0; public RobotAnimation() { setPreferredSize(new Dimension(800, 260)); setBackground(Color.BLACK); // Space robots evenly across the panel from left to right int panelWidth = 800; int gap = panelWidth / (NUM_ROBOTS + 1); for (int i = 0; i < NUM_ROBOTS; i++) { robots.add(new Robot(gap * (i + 1) - 14, ROBOT_Y, SPEED)); } timer = new Timer(30, this); timer.start(); } @Override public void actionPerformed(ActionEvent e) { frameCount++; boolean moving = (frameCount % (MOVE_FRAMES + PAUSE_FRAMES)) < MOVE_FRAMES; if (moving) { for (Robot robot : robots) { robot.walk(); } } repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (Robot robot : robots) { robot.draw(g2); } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Robot Parade"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new RobotAnimation()); frame.pack(); frame.setLocationRelativeTo(null); frame.setResizable(false); frame.setVisible(true); }); } }