File tree Expand file tree Collapse file tree 4 files changed +146
-1
lines changed Open diff view settings
Expand file tree Collapse file tree 4 files changed +146
-1
lines changed Open diff view settings
Original file line number Diff line number Diff line change 1+ /**
2+ * A polygon that moves around the screen.
3+ */
4+ public class MovingPolygon extends RegularPolygon {
5+
6+ private int dx ;
7+ private int dy ;
8+
9+ /**
10+ * Constructs a moving polygon.
11+ *
12+ * @param nsides the number of sides
13+ * @param length length of each side
14+ */
15+ public MovingPolygon (int nsides , int length ) {
16+ super (nsides , length );
17+ this .dx = 10 ;
18+ this .dy = 5 ;
19+ }
20+
21+ /**
22+ * @param dx how many pixels to move left/right
23+ */
24+ public void setDx (int dx ) {
25+ this .dx = dx ;
26+ }
27+
28+ /**
29+ * @param dy how many pixels to move up/down
30+ */
31+ public void setDy (int dy ) {
32+ this .dy = dy ;
33+ }
34+
35+ @ Override
36+ public void act () {
37+
38+ // edge detection
39+ for (int i = 0 ; i < npoints ; i ++) {
40+ if (xpoints [i ] < 0 || xpoints [i ] > 800 ) {
41+ dx *= -1 ;
42+ break ;
43+ }
44+ }
45+ for (int i = 0 ; i < npoints ; i ++) {
46+ if (ypoints [i ] < 0 || ypoints [i ] > 600 ) {
47+ dy *= -1 ;
48+ break ;
49+ }
50+ }
51+
52+ // move one step
53+ translate (dx , dy );
54+ }
55+
56+ }
Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ public void act() {
7373
7474 @ Override
7575 public void draw (Graphics g ) {
76- System .out .println (this );
76+ // System.out.println(this);
7777 g .drawPolygon (this );
7878 }
7979
Original file line number Diff line number Diff line change 1+ import javax .swing .JFrame ;
2+
3+ /**
4+ * Example simulation of stationary objects.
5+ */
6+ public class Sim3 {
7+
8+ /**
9+ * Test program that draws a few polygons.
10+ *
11+ * @param args command-line arguments
12+ */
13+ public static void main (String [] args ) {
14+
15+ MovingPolygon mp = new MovingPolygon (8 , 30 );
16+ mp .translate (100 , 100 );
17+ Drawing drawing = new Drawing (800 , 600 );
18+ drawing .add (mp );
19+
20+ JFrame frame = new JFrame ("Drawing" );
21+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
22+ frame .setResizable (false );
23+ frame .add (drawing );
24+ frame .pack ();
25+ frame .setVisible (true );
26+
27+ // main simulation loop
28+ while (true ) {
29+
30+ // update the drawing
31+ drawing .nextact ();
32+ drawing .repaint ();
33+
34+ // delay the simulation
35+ try {
36+ Thread .sleep (1000 / 50 );
37+ frame .getToolkit ().sync ();
38+ } catch (InterruptedException e ) {
39+ // do nothing
40+ }
41+ }
42+ }
43+
44+ }
Original file line number Diff line number Diff line change 1+ import java .awt .event .ActionEvent ;
2+ import java .awt .event .ActionListener ;
3+
4+ import javax .swing .JFrame ;
5+
6+ /**
7+ * Example simulation of stationary objects.
8+ */
9+ public class Sim4 implements ActionListener {
10+
11+ private Drawing drawing ;
12+ private JFrame frame ;
13+
14+ public Sim4 () {
15+ MovingPolygon mp = new MovingPolygon (8 , 30 );
16+ mp .translate (100 , 100 );
17+ drawing = new Drawing (800 , 600 );
18+ drawing .add (mp );
19+
20+ frame = new JFrame ("Drawing" );
21+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
22+ frame .setResizable (false );
23+ frame .add (drawing );
24+ frame .pack ();
25+ frame .setVisible (true );
26+ }
27+
28+ /**
29+ * Test program that draws a few polygons.
30+ *
31+ * @param args command-line arguments
32+ */
33+ public static void main (String [] args ) {
34+ Sim4 sim = new Sim4 ();
35+ new javax .swing .Timer (1000 / 50 , sim ).start ();
36+ }
37+
38+ @ Override
39+ public void actionPerformed (ActionEvent e ) {
40+ drawing .nextact ();
41+ drawing .repaint ();
42+ frame .getToolkit ().sync ();
43+ }
44+
45+ }
You can’t perform that action at this time.
0 commit comments