/Users/lyon/j4p/src/bookExamples/ch12Nested/Shape.java
|
1 package bookExamples.ch12Nested;
2
3 import java.awt.Graphics;
4
5 /**
6 * DocJava, Inc.
7 * http://www.docjava.com
8 * Programmer: dlyon
9 * Date: Oct 4, 2004
10 * Time: 3:19:20 PM
11 */
12 public abstract class Shape {
13 private int x;
14 private int y;
15 Shape(int x, int y) {
16 this.x = x;
17 this.y = y;
18 }
19 public abstract void draw(Graphics g);
20 // getCircle makes an instance - factory pattern
21 // Using an anonymous local inner class....
22 public static Shape getCircle(final int x, final int y, final int r) {
23 return new Shape(x,y) {
24 public void draw(Graphics g) {
25 g.drawOval(x,y,2*r,2*r);
26 }
27 };
28 }
29 public static Shape getOval(int x, int y, final int w, final int h) {
30 return new Oval(x, y, w, h);
31 }
32
33 public int getX() {
34 return x;
35 }
36
37 public void setX(int x) {
38 this.x = x;
39 }
40
41 public int getY() {
42 return y;
43 }
44
45 public void setY(int y) {
46 this.y = y;
47 }
48
49 // Named static inner class
50 private static class Oval extends Shape {
51 private final int w;
52 private final int h;
53
54 public Oval(int x, int y, int w, int h) {
55 super(x, y);
56 this.w = w;
57 this.h = h;
58 }
59
60 public void draw(Graphics g) {
61 g.drawOval(getX(),getY(),w,h);
62
63 }
64 }
65 }
66