import javax.swing.*;import java.awt.*;import java.awt.event.*;public class AnimatorTest extends JFrame implements ActionListener { private Zeichner animation; public AnimatorTest(String title) { super(title); setBackground(Color.lightGray); setLocation(30, 30); setSize(640, 480); JPanel mainPanel=new JPanel(new BorderLayout()); setContentPane(mainPanel); JButton startButton = new JButton("Start"); JButton stopButton = new JButton("Stop"); startButton.addActionListener(this); startButton.setActionCommand("Start"); stopButton.addActionListener(this); stopButton.setActionCommand("Stop"); animation = new Zeichner(); animation.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.red)); mainPanel.add(animation,BorderLayout.CENTER); JPanel knopfPanel = new JPanel(); knopfPanel.setBorder( BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue)); knopfPanel.add(startButton); knopfPanel.add(stopButton); mainPanel.add(knopfPanel,BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { JButton source = (JButton) e.getSource(); if (source.getActionCommand().equalsIgnoreCase("start")) { animation.start(); } else if (source.getActionCommand().equalsIgnoreCase("stop")) { animation.stop(); } } public static void main(String[] args) { new AnimatorTest("Animation"); }}class Zeichner extends javax.swing.JPanel implements Runnable { private boolean animStopped = true; private Thread animation; private int counter=0; public void stop() { animStopped = true; } public void start(){ animStopped=false; animation=new Thread(this); animation.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); if(animStopped){ return; } ++counter; System.out.println(counter); g.drawOval(28 + counter, 31 + counter, 30, 30); g.setColor(new Color(210, 200, 50)); g.fillOval(28 + counter, 31 + counter, 200, 50); } public void run() { while (!animStopped) { repaint(); try{ Thread.sleep(50); }catch(InterruptedException e){animStopped=true;} } } }