1+ import java .awt .Color ;
12import java .awt .Toolkit ;
23import java .awt .event .ActionEvent ;
34import java .awt .event .ActionListener ;
45import javax .swing .JFrame ;
56import javax .swing .Timer ;
67
78/**
8- * Smoother version using a timer.
9+ * Smoother version of Sim2 using a timer.
910 */
1011public class Sim3 implements ActionListener {
1112
@@ -17,20 +18,23 @@ public class Sim3 implements ActionListener {
1718 */
1819 public Sim3 () {
1920 // create some regular polygons
21+ BlinkingPolygon bp = new BlinkingPolygon (3 , 10 , Color .BLUE );
2022 MovingPolygon mp = new MovingPolygon (8 , 30 );
2123 RotatingPolygon rp = new RotatingPolygon (5 , 50 );
2224
2325 // move them out of the corner
26+ bp .translate (50 , 50 );
2427 mp .translate (100 , 100 );
2528 rp .translate (200 , 200 );
2629
2730 // create drawing, add polygons
2831 drawing = new Drawing (800 , 600 );
32+ drawing .add (bp );
2933 drawing .add (mp );
3034 drawing .add (rp );
3135
3236 // set up the window frame
33- JFrame frame = new JFrame ("Drawing" );
37+ JFrame frame = new JFrame ("Drawing with ActionListener " );
3438 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
3539 frame .setResizable (false );
3640 frame .add (drawing );
@@ -46,15 +50,14 @@ public Sim3() {
4650 */
4751 public static void main (String [] args ) {
4852 Sim3 sim = new Sim3 ();
49- Timer timer = new Timer (1000 / 60 , sim );
53+ Timer timer = new Timer (50 , sim );
5054 timer .start ();
5155 }
5256
5357 @ Override
5458 public void actionPerformed (ActionEvent e ) {
55- drawing .nextact ();
56- drawing .repaint ();
5759 toolkit .sync ();
60+ drawing .next ();
5861 }
5962
6063}
0 commit comments