import javax.swing.Timer;
Add attribute;
Timer timer;
boolean b; // for starting and stoping animation
Add the following code to the frame constructor.
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
});
Override paint(Graphics g)and draw a polygon from data that has been modified using actionPerformed(e).
Finally, the button that starts / stops the animation has the following code in the event handler.
if (b) {
timer.start();
} else {
timer.stop();
}
b = !b;
source
share