File tree Expand file tree Collapse file tree
Chapter 17 - Advanced Topics/src/chapter17sections Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Graphics ;
4+
5+ public interface Actor {
6+ void draw (Graphics g );
7+
8+ void step ();
9+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Color ;
4+ import java .awt .Graphics ;
5+
6+ public class BlinkingPolygon extends RegularPolygon {
7+
8+ protected boolean visible ;
9+ protected int count ;
10+
11+ public BlinkingPolygon (int nsides , int radius , Color color ) {
12+ super (nsides , radius , color );
13+ visible = true ;
14+ count = 0 ;
15+ }
16+
17+ @ Override
18+ public void draw (Graphics g ) {
19+ if (visible ) {
20+ super .draw (g );
21+ }
22+ }
23+
24+ @ Override
25+ public void step () {
26+ count ++;
27+ if (count == 10 ) {
28+ visible = !visible ;
29+ count = 0 ;
30+ }
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Color ;
4+ import java .awt .Graphics ;
5+ import java .awt .Polygon ;
6+
7+ public class DrawablePolygon extends Polygon implements Actor {
8+
9+ protected Color color ;
10+
11+ public DrawablePolygon () {
12+ super ();
13+ color = Color .GRAY ;
14+ }
15+
16+ public void draw (Graphics g ) {
17+ g .setColor (color );
18+ g .fillPolygon (this );
19+ }
20+
21+ @ Override
22+ public void step () {
23+ }
24+
25+ public static void main (String [] args ) {
26+ DrawablePolygon p = new DrawablePolygon ();
27+ p .addPoint (57 , 110 );
28+ p .addPoint (100 , 35 );
29+ p .addPoint (143 , 110 );
30+ p .color = Color .GREEN ;
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Canvas ;
4+ import java .awt .Color ;
5+ import java .awt .Graphics ;
6+ import java .util .ArrayList ;
7+
8+ public class Drawing extends Canvas {
9+
10+ private ArrayList <Actor > list ;
11+
12+ public Drawing (int width , int height ) {
13+ setSize (width , height );
14+ setBackground (Color .WHITE );
15+ list = new ArrayList <Actor >();
16+ }
17+
18+ public void add (Actor actor ) {
19+ list .add (actor );
20+ }
21+
22+ public Object [] getActors () {
23+ return list .toArray ();
24+ }
25+
26+ @ Override
27+ public void paint (Graphics g ) {
28+ for (Actor actor : list ) {
29+ actor .draw (g );
30+ }
31+ }
32+
33+ public void step () {
34+ for (Actor actor : list ) {
35+ actor .step ();
36+ }
37+ repaint ();
38+ }
39+
40+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Color ;
4+ import javax .swing .JFrame ;
5+
6+ public class Main {
7+
8+ public static void main (String [] args ) {
9+
10+ DrawablePolygon p1 = new RegularPolygon (3 , 50 , Color .GREEN );
11+ DrawablePolygon p2 = new RegularPolygon (6 , 50 , Color .ORANGE );
12+ DrawablePolygon p3 = new RegularPolygon (360 , 50 , Color .BLUE );
13+
14+ p1 .translate (100 , 80 );
15+ p2 .translate (250 , 120 );
16+ p3 .translate (400 , 160 );
17+
18+ Drawing drawing = new Drawing (500 , 250 );
19+ drawing .add (p1 );
20+ drawing .add (p2 );
21+ drawing .add (p3 );
22+
23+ JFrame frame = new JFrame ("Drawing" );
24+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
25+ frame .add (drawing );
26+ frame .pack ();
27+ frame .setVisible (true );
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Color ;
4+
5+ public class RegularPolygon extends DrawablePolygon {
6+
7+ public RegularPolygon (int nsides ) {
8+ this (nsides , 50 );
9+ }
10+
11+ public RegularPolygon (int nsides , int radius ) {
12+ this (nsides , radius , Color .GRAY );
13+ }
14+
15+ public RegularPolygon (int nsides , int radius , Color color ) {
16+
17+ if (nsides < 3 ) {
18+ throw new IllegalArgumentException ("invalid nsides" );
19+ }
20+ if (radius <= 0 ) {
21+ throw new IllegalArgumentException ("invalid radius" );
22+ }
23+ if (color == null ) {
24+ throw new NullPointerException ("invalid color" );
25+ }
26+
27+ this .npoints = nsides ;
28+ this .xpoints = new int [nsides ];
29+ this .ypoints = new int [nsides ];
30+ this .color = color ;
31+
32+ double theta = 2.0 * Math .PI / nsides ;
33+
34+ double rotate = Math .PI / nsides + Math .PI / 2.0 ;
35+
36+ for (int i = 0 ; i < nsides ; i ++) {
37+ double x = radius * Math .cos (i * theta + rotate );
38+ double y = radius * Math .sin (i * theta + rotate );
39+ xpoints [i ] = (int ) Math .round (x );
40+ ypoints [i ] = (int ) Math .round (y );
41+ }
42+ }
43+
44+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Graphics ;
4+ import java .awt .Image ;
5+ import java .awt .event .KeyEvent ;
6+ import java .awt .event .KeyListener ;
7+ import java .io .File ;
8+ import java .io .IOException ;
9+ import javax .imageio .ImageIO ;
10+
11+ public class Sprite implements Actor , KeyListener {
12+
13+ private int xpos ;
14+ private int ypos ;
15+ private int dx ;
16+ private int dy ;
17+ private Image image ;
18+
19+ public Sprite (String path , int xpos , int ypos ) {
20+ this .xpos = xpos ;
21+ this .ypos = ypos ;
22+ try {
23+ this .image = ImageIO .read (new File (path ));
24+ } catch (IOException e ) {
25+ e .printStackTrace ();
26+ }
27+ }
28+
29+ @ Override
30+ public void draw (Graphics g ) {
31+ g .drawImage (image , xpos , ypos , null );
32+ }
33+
34+ @ Override
35+ public void step () {
36+ xpos += dx ;
37+ ypos += dy ;
38+ }
39+
40+ @ Override
41+ public void keyPressed (KeyEvent e ) {
42+ switch (e .getKeyCode ()) {
43+ case KeyEvent .VK_UP :
44+ dy = -5 ;
45+ break ;
46+ case KeyEvent .VK_DOWN :
47+ dy = +5 ;
48+ break ;
49+ case KeyEvent .VK_LEFT :
50+ dx = -5 ;
51+ break ;
52+ case KeyEvent .VK_RIGHT :
53+ dx = +5 ;
54+ break ;
55+ }
56+ }
57+
58+ @ Override
59+ public void keyReleased (KeyEvent e ) {
60+ switch (e .getKeyCode ()) {
61+ case KeyEvent .VK_UP :
62+ case KeyEvent .VK_DOWN :
63+ dy = 0 ;
64+ break ;
65+ case KeyEvent .VK_LEFT :
66+ case KeyEvent .VK_RIGHT :
67+ dx = 0 ;
68+ break ;
69+ }
70+ }
71+
72+ @ Override
73+ public void keyTyped (KeyEvent e ) {
74+ }
75+
76+ }
Original file line number Diff line number Diff line change 1+ package chapter17sections ;
2+
3+ import java .awt .Toolkit ;
4+ import java .awt .event .ActionEvent ;
5+ import java .awt .event .ActionListener ;
6+ import javax .swing .JFrame ;
7+ import javax .swing .Timer ;
8+
9+ public class VideoGame implements ActionListener {
10+
11+ private Drawing drawing ;
12+ private Toolkit toolkit ;
13+
14+ public VideoGame () {
15+ Sprite sprite = new Sprite ("face-smile.png" , 25 , 150 );
16+ drawing = new Drawing (800 , 600 );
17+ drawing .add (sprite );
18+ drawing .addKeyListener (sprite );
19+ drawing .setFocusable (true );
20+
21+ JFrame frame = new JFrame ("Video Game" );
22+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
23+ frame .add (drawing );
24+ frame .pack ();
25+ frame .setVisible (true );
26+ toolkit = frame .getToolkit ();
27+ }
28+
29+ @ Override
30+ public void actionPerformed (ActionEvent e ) {
31+ drawing .step ();
32+ toolkit .sync ();
33+ }
34+
35+ public static void main (String [] args ) {
36+ VideoGame game = new VideoGame ();
37+ Timer timer = new Timer (33 , game );
38+ timer .start ();
39+ }
40+
41+ }
You can’t perform that action at this time.
0 commit comments