KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > example > animation > ArmyTank


1 /*
2  * ArmyTank.java
3  *
4  * Created on 10 July 2003, 13:48
5  */

6
7 package com.thoughtriver.open.vectorvisuals.example.animation;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11
12 import com.thoughtriver.open.vectorvisuals.*;
13
14 /**
15  * This class displays a <CODE>VisualObject</CODE> that looks like a military
16  * tank with two cannons. The turret can be manipulated, and the colors of the
17  * tank can be changed.
18  *
19  * @author Brandon Franklin
20  * @version $Date: 2006/11/25 09:31:50 $
21  */

22 public class ArmyTank extends VisualObject {
23
24     /** The <CODE>VisualObject</CODE> that is used as the turret for the tank */
25     private VisualObject turret = null;
26
27     /** The <CODE>Color</CODE> used for the main body of the tank */
28     protected Color bodyColor = null;
29
30     /** The <CODE>Color</CODE> used for highlighted sections of the tank */
31     protected Color highlightColor = null;
32
33     /** The <CODE>Color</CODE> used to outline objects on the tank */
34     protected Color objectColor = Color.BLACK;
35
36     /**
37      * Creates a new instance of <CODE>ArmyTank</CODE> with a default color
38      * theme based on green.
39      */

40     public ArmyTank() {
41         this(new Color(0, 170, 0));
42     }
43
44     /**
45      * Creates a new instance of <CODE>ArmyTank</CODE> with a color theme
46      * based on the supplied <CODE>Color</CODE>.
47      *
48      * @param themeColor the <CODE>Color</CODE> upon which the other colors of
49      * the tank should be based
50      */

51     public ArmyTank(final Color themeColor) {
52         super(new RoundRectangle2D.Double(-20, -30, 40, 60, 5, 5), new Brush(themeColor.brighter(), null, null), new Brush(themeColor, null, null));
53
54         bodyColor = themeColor;
55         highlightColor = themeColor.brighter();
56
57         buildParts();
58     }
59
60     /**
61      * Returns a reference to the <CODE>VisualObject</CODE> that displays the
62      * turret portion of the tank. This is useful for manually manipulating the
63      * turret in various ways.
64      *
65      * @return a reference to the <CODE>VisualObject</CODE> that displays the
66      * turret portion of the tank
67      */

68     public VisualObject getTurret() {
69         return turret;
70     }
71
72     /**
73      * Rotates the tank's turret by the supplied amount of Theta.
74      *
75      * @param theta the amount of Theta to rotate the tank's turret
76      */

77     public void rotateTurret(final double theta) {
78         Point2D rotationPoint = turret.getRotationPoint();
79         AffineTransform transform = turret.getTransform();
80         transform.rotate(theta, rotationPoint.getX(), rotationPoint.getY());
81         turret.setTransform(transform);
82     }
83
84     /**
85      * This method constructs all of the various subcomponents of the tank, and
86      * embeds them into the primary <CODE>VisualObject</CODE>.
87      */

88     protected void buildParts() {
89
90         // Turret
91
Shape turretShape = new RoundRectangle2D.Double(-15, -15, 30, 30, 7, 7);
92         turret = new VisualObject(turretShape, new Brush(objectColor, null, null), new Brush(highlightColor, null, null));
93         add(turret);
94         turret.setLayer(2);
95
96         // Cannons
97
Shape cannonShape1 = new Rectangle2D.Double(-10, -45, 5, 30);
98         VisualObject cannon1 = new VisualObject(cannonShape1, new Brush(objectColor, null, null), new Brush(highlightColor, null, null));
99         turret.add(cannon1);
100
101         Shape cannonShape2 = new Rectangle2D.Double(5, -45, 5, 30);
102         VisualObject cannon2 = new VisualObject(cannonShape2, new Brush(objectColor, null, null), new Brush(highlightColor, null, null));
103         turret.add(cannon2);
104
105         // Rear hatches
106
Shape hatchShape1 = new Rectangle2D.Double(-12, 17, 7, 10);
107         VisualObject hatch1 = new VisualObject(hatchShape1, new Brush(objectColor, null, null), new Brush(bodyColor, null, null));
108         add(hatch1);
109
110         Shape hatchShape2 = new Rectangle2D.Double(5, 17, 7, 10);
111         VisualObject hatch2 = new VisualObject(hatchShape2, new Brush(objectColor, null, null), new Brush(bodyColor, null, null));
112         add(hatch2);
113
114         // Front panel
115
Shape panelShape = new Rectangle2D.Double(-17, -27, 34, 12);
116         VisualObject panel = new VisualObject(panelShape, new Brush(objectColor, null, null), new Brush(bodyColor, null, null));
117         add(panel);
118     }
119
120 }
121
Popular Tags