import javax.swing.*; import java.awt.event.*; import java.awt.*; class World extends JComponent implements ActionListener, KeyListener { Timer timer; Circle current; Foam foam; World(JLabel statusBar) { this.foam = new Foam(statusBar); this.timer = new Timer(200, this); this.timer.start(); this.current = new Circle(this.foam); this.foam.add( this.current ); } public void actionPerformed(ActionEvent e) { if (this.current != null) { if (this.current.fall()) { } else { this.foam.clean(); this.current = null; this.timer.setDelay(200); } } else { if (this.foam.rearranges()) { } else { this.current = new Circle(this.foam); this.foam.add( this.current ); this.timer.setDelay(200); } } this.repaint(); } public void keyPressed(KeyEvent e) { if (this.current == null) return; if (e.getKeyCode() == KeyEvent.VK_LEFT) { this.current.center.x -= 5; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { this.current.center.x += 5; } else { this.timer.setDelay(20); } this.repaint(); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void paintComponent(Graphics g) { if (this.current != null) this.current.draw(g); this.foam.draw(g); } }