U:\NetBeans\PokeBall\src\graphicslab04\PokeBall.java |
1 package graphicslab04; 2 3 import java.awt.Color; 4 import java.awt.Graphics; 5 import java.awt.Graphics2D; 6 import java.awt.geom.AffineTransform; 7 import java.awt.geom.Area; 8 import java.awt.geom.Ellipse2D; 9 import java.awt.geom.Rectangle2D; 10 11 public class PokeBall { 12 13 private static final Area base = new Area(); 14 private static final Area border = new Area(); 15 protected static final Area topHalf = new Area(); 16 17 static { 18 Ellipse2D.Double outer = new Ellipse2D.Double(0, 0, 250, 250); 19 Ellipse2D.Double inner = new Ellipse2D.Double(5, 5, 240, 240); 20 Rectangle2D.Double stripe = new Rectangle2D.Double(0, 120, 250, 10); 21 Ellipse2D.Double buttonOuter = new Ellipse2D.Double(90, 90, 70, 70); 22 Ellipse2D.Double buttonInner = new Ellipse2D.Double(100, 100, 50, 50); 23 Rectangle2D.Double topRect = new Rectangle2D.Double(0, 0, 250, 125); 24 25 base.add(new Area(outer)); 26 27 border.add(base); 28 border.subtract(new Area(inner)); 29 border.add(new Area(stripe)); 30 border.intersect(base); 31 border.add(new Area(buttonOuter)); 32 border.subtract(new Area(buttonInner)); 33 34 topHalf.add(base); 35 topHalf.subtract(border); 36 topHalf.intersect(new Area(topRect)); 37 topHalf.subtract(new Area(buttonOuter)); 38 } 39 40 private final Area myBase; 41 private final Area myBorder; 42 private final Area myTopHalf; 43 44 protected final AffineTransform shift; 45 46 public PokeBall(int xp, int yp) { 47 shift = AffineTransform.getTranslateInstance(xp, yp); 48 myBase = base.createTransformedArea(shift); 49 myBorder = border.createTransformedArea(shift); 50 myTopHalf = topHalf.createTransformedArea(shift); 51 } 52 53 public void drawBall(Graphics g) { 54 drawBase(g); 55 drawTopHalf(g); 56 drawBorder(g); 57 } 58 59 private void drawBase(Graphics g) { 60 g.setColor(Color.white); 61 ((Graphics2D) g).fill(myBase); 62 } 63 64 private void drawBorder(Graphics g) { 65 Color c = (this instanceof PremierBall) ? Color.red : Color.black; 66 g.setColor(c); 67 ((Graphics2D) g).fill(myBorder); 68 } 69 70 protected void drawTopHalf(Graphics g) { 71 g.setColor(Color.red); 72 ((Graphics2D) g).fill(myTopHalf); 73 } 74 75 } 76