Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4eb7001

Browse filesBrowse files
committed
added MovingPolygon
1 parent 9c73a20 commit 4eb7001
Copy full SHA for 4eb7001

File tree

Expand file treeCollapse file tree

4 files changed

+146
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+146
-1
lines changed
Open diff view settings
Collapse file

‎ch15/MovingPolygon.java‎

Copy file name to clipboard
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
Collapse file

‎ch15/RegularPolygon.java‎

Copy file name to clipboardExpand all lines: ch15/RegularPolygon.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

Collapse file

‎ch15/Sim3.java‎

Copy file name to clipboard
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}
Collapse file

‎ch15/Sim4.java‎

Copy file name to clipboard
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.