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 1407390

Browse filesBrowse files
committed
draft of ch16
1 parent aa248dc commit 1407390
Copy full SHA for 1407390

File tree

Expand file treeCollapse file tree

12 files changed

+522
-41
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

12 files changed

+522
-41
lines changed
Open diff view settings
Collapse file

‎ch16/ColorPolygon.java‎

Copy file name to clipboardExpand all lines: ch16/ColorPolygon.java
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
/**
66
* Specialization of Polygon that has a color and the ability to draw itself.
77
*/
8-
public class ColorPolygon extends Polygon {
8+
public class ColorPolygon extends Polygon implements Actor {
99

10-
public Color color;
10+
protected Color color;
1111

1212
/**
1313
* Creates an empty polygon.
@@ -27,4 +27,9 @@ public void draw(Graphics g) {
2727
g.fillPolygon(this);
2828
}
2929

30+
@Override
31+
public void step() {
32+
// do nothing
33+
}
34+
3035
}
Collapse file

‎ch16/Drawing.bak‎

Copy file name to clipboard
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.awt.Canvas;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import java.util.ArrayList;
5+
6+
/**
7+
* Draws a collection of actors.
8+
*/
9+
public class Drawing extends Canvas {
10+
11+
private ArrayList<ColorPolygon> list;
12+
13+
/**
14+
* Constructs a drawing of given size.
15+
*
16+
* @param width the width in pixels
17+
* @param height the height in pixels
18+
*/
19+
public Drawing(int width, int height) {
20+
setSize(width, height);
21+
setBackground(Color.WHITE);
22+
list = new ArrayList<ColorPolygon>();
23+
}
24+
25+
/**
26+
* Adds a new actor to the drawing.
27+
*
28+
* @param cp the object to add
29+
*/
30+
public void add(ColorPolygon cp) {
31+
list.add(cp);
32+
}
33+
34+
/**
35+
* Gets current actors as an array.
36+
*
37+
* @return array of actor objects
38+
*/
39+
public Object[] getActors() {
40+
return list.toArray();
41+
}
42+
43+
/**
44+
* Draws all the actors on the canvas.
45+
*
46+
* @param g graphics context
47+
*/
48+
@Override
49+
public void paint(Graphics g) {
50+
for (ColorPolygon cp : list) {
51+
cp.draw(g);
52+
}
53+
}
54+
55+
/**
56+
* Calls the step method of each actor and updates the drawing.
57+
*/
58+
public void step() {
59+
for (ColorPolygon cp : list) {
60+
cp.step();
61+
}
62+
repaint();
63+
}
64+
65+
}
Collapse file

‎ch16/Drawing.java‎

Copy file name to clipboardExpand all lines: ch16/Drawing.java
+7-8Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import java.awt.Color;
33
import java.awt.Graphics;
44
import java.util.ArrayList;
5-
import java.util.List;
65

76
/**
87
* Draws a collection of actors.
98
*/
109
public class Drawing extends Canvas {
1110

12-
private List<Actor> actors;
11+
private ArrayList<Actor> list;
1312

1413
/**
1514
* Constructs a drawing of given size.
@@ -20,16 +19,16 @@ public class Drawing extends Canvas {
2019
public Drawing(int width, int height) {
2120
setSize(width, height);
2221
setBackground(Color.WHITE);
23-
actors = new ArrayList<Actor>();
22+
list = new ArrayList<Actor>();
2423
}
2524

2625
/**
2726
* Adds a new actor to the drawing.
2827
*
29-
* @param actor the actor to add
28+
* @param actor the object to add
3029
*/
3130
public void add(Actor actor) {
32-
actors.add(actor);
31+
list.add(actor);
3332
}
3433

3534
/**
@@ -38,7 +37,7 @@ public void add(Actor actor) {
3837
* @return array of actor objects
3938
*/
4039
public Object[] getActors() {
41-
return actors.toArray();
40+
return list.toArray();
4241
}
4342

4443
/**
@@ -48,7 +47,7 @@ public Object[] getActors() {
4847
*/
4948
@Override
5049
public void paint(Graphics g) {
51-
for (Actor actor : actors) {
50+
for (Actor actor : list) {
5251
actor.draw(g);
5352
}
5453
}
@@ -57,7 +56,7 @@ public void paint(Graphics g) {
5756
* Calls the step method of each actor and updates the drawing.
5857
*/
5958
public void step() {
60-
for (Actor actor : actors) {
59+
for (Actor actor : list) {
6160
actor.step();
6261
}
6362
repaint();
Collapse file

‎ch16/RegularPolygon.java‎

Copy file name to clipboardExpand all lines: ch16/RegularPolygon.java
+3-18Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import java.awt.Color;
2-
import java.awt.Graphics;
3-
import java.awt.Polygon;
42

53
/**
64
* A polygon that is equiangular (all angles are equal in measure) and
75
* equilateral (all sides have the same length). It also has a color.
86
*/
9-
public class RegularPolygon extends Polygon implements Actor {
7+
public class RegularPolygon extends ColorPolygon {
108

119
public static final String[] NAMES = {null, null, null,
1210
"Triangle", "Square", "Pentagon", "Hexagon",
1311
"Heptagon", "Octagon", "Nonagon", "Decagon"};
1412

15-
protected Color color;
16-
1713
/**
1814
* Constructs a regular polygon, given the number of sides.
1915
*
@@ -43,7 +39,7 @@ public RegularPolygon(int nsides, int radius) {
4339
*/
4440
public RegularPolygon(int nsides, int radius, Color color) {
4541

46-
// validate and store arguments
42+
// validate the arguments
4743
if (nsides < 3) {
4844
throw new IllegalArgumentException("invalid nsides");
4945
}
@@ -60,7 +56,7 @@ public RegularPolygon(int nsides, int radius, Color color) {
6056
this.ypoints = new int[nsides];
6157
this.color = color;
6258

63-
// the angle (in radians) at each vertex
59+
// the angle (in radians) for each vertex
6460
double angle = 2.0 * Math.PI / nsides;
6561

6662
// rotation that makes the polygon right-side up
@@ -75,17 +71,6 @@ public RegularPolygon(int nsides, int radius, Color color) {
7571
}
7672
}
7773

78-
@Override
79-
public void draw(Graphics g) {
80-
g.setColor(color);
81-
g.fillPolygon(this);
82-
}
83-
84-
@Override
85-
public void step() {
86-
// do nothing
87-
}
88-
8974
@Override
9075
public String toString() {
9176
StringBuilder str = new StringBuilder();
Collapse file

‎ch16/Sim1.java‎

Copy file name to clipboardExpand all lines: ch16/Sim1.java
+10-11Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import javax.swing.JFrame;
33

44
/**
5-
* Example simulation of stationary objects.
5+
* Initial drawing of stationary objects.
66
*/
77
public class Sim1 {
88

@@ -14,25 +14,24 @@ public class Sim1 {
1414
public static void main(String[] args) {
1515

1616
// create some regular polygons
17-
RegularPolygon p3 = new RegularPolygon(3, 50, Color.GREEN);
18-
RegularPolygon p4 = new RegularPolygon(5, 50, Color.ORANGE);
19-
RegularPolygon p5 = new RegularPolygon(360, 50, Color.MAGENTA);
17+
ColorPolygon p1 = new RegularPolygon(3, 50, Color.GREEN);
18+
ColorPolygon p2 = new RegularPolygon(6, 50, Color.ORANGE);
19+
ColorPolygon p3 = new RegularPolygon(360, 50, Color.BLUE);
2020

2121
// move them out of the corner
22-
p3.translate(100, 100);
23-
p4.translate(250, 250);
24-
p5.translate(400, 400);
22+
p1.translate(100, 80);
23+
p2.translate(250, 120);
24+
p3.translate(400, 160);
2525

2626
// create drawing, add polygons
27-
Drawing drawing = new Drawing(800, 600);
27+
Drawing drawing = new Drawing(500, 250);
28+
drawing.add(p1);
29+
drawing.add(p2);
2830
drawing.add(p3);
29-
drawing.add(p4);
30-
drawing.add(p5);
3131

3232
// set up the window frame
3333
JFrame frame = new JFrame("Drawing (no animation)");
3434
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35-
frame.setResizable(false);
3635
frame.add(drawing);
3736
frame.pack();
3837
frame.setVisible(true);
Collapse file

‎ch16/Sim2.java‎

Copy file name to clipboardExpand all lines: ch16/Sim2.java
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static void main(String[] args) {
3333
// set up the window frame
3434
JFrame frame = new JFrame("Drawing with Thread.sleep");
3535
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36-
frame.setResizable(false);
3736
frame.add(drawing);
3837
frame.pack();
3938
frame.setVisible(true);
Collapse file

‎ch16/Sim3.java‎

Copy file name to clipboardExpand all lines: ch16/Sim3.java
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public Sim3() {
3636
// set up the window frame
3737
JFrame frame = new JFrame("Drawing with ActionListener");
3838
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39-
frame.setResizable(false);
4039
frame.add(drawing);
4140
frame.pack();
4241
frame.setVisible(true);
Collapse file

‎ch16/Sprite.java‎

Copy file name to clipboard
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.awt.Graphics;
2+
import java.awt.Image;
3+
import java.awt.event.KeyEvent;
4+
import java.awt.event.KeyListener;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import javax.imageio.ImageIO;
8+
9+
/**
10+
* A small character that can move around the screen.
11+
*/
12+
public class Sprite implements Actor, KeyListener {
13+
14+
private Image image;
15+
16+
private int xpos;
17+
private int ypos;
18+
private int dx;
19+
private int dy;
20+
21+
/**
22+
* Reads a sprite from a png file.
23+
*
24+
* @param path location of image file
25+
* @param xpos initial X coordinate
26+
* @param ypos initial Y coordinate
27+
*/
28+
public Sprite(String path, int xpos, int ypos) {
29+
try {
30+
this.image = ImageIO.read(new File(path));
31+
this.xpos = xpos;
32+
this.ypos = ypos;
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
@Override
39+
public void draw(Graphics g) {
40+
g.drawImage(image, xpos, ypos, null);
41+
}
42+
43+
@Override
44+
public void step() {
45+
xpos += dx;
46+
ypos += dy;
47+
}
48+
49+
@Override
50+
public void keyTyped(KeyEvent e) {
51+
// do nothing
52+
}
53+
54+
@Override
55+
public void keyPressed(KeyEvent e) {
56+
switch (e.getKeyCode()) {
57+
case KeyEvent.VK_UP:
58+
dy = -5;
59+
break;
60+
case KeyEvent.VK_DOWN:
61+
dy = +5;
62+
break;
63+
case KeyEvent.VK_LEFT:
64+
dx = -5;
65+
break;
66+
case KeyEvent.VK_RIGHT:
67+
dx = +5;
68+
break;
69+
}
70+
}
71+
72+
@Override
73+
public void keyReleased(KeyEvent e) {
74+
switch (e.getKeyCode()) {
75+
case KeyEvent.VK_UP:
76+
case KeyEvent.VK_DOWN:
77+
dy = 0;
78+
break;
79+
case KeyEvent.VK_LEFT:
80+
case KeyEvent.VK_RIGHT:
81+
dx = 0;
82+
break;
83+
}
84+
}
85+
86+
}

0 commit comments

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