-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTarget.java~
153 lines (136 loc) · 4.64 KB
/
Target.java~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// --------------- imports ------------------------------
import wheelsunh.users.*;
import java.awt.Color;
/**
* Target.java:
*
* Displays a simple archery target using multiple Wheels Shapes.
* The entire target is built in a method, makeTarget.
*
* makeTarget has position arguments.
*
* @author rdb and mlb
*/
public class Target
{
//---------------- instance variables ------------------------------
// local "constant" variables define the locations of the inner
// circles relative to the level1 circle.
int level2X = 15, level2Y = 15;
int level3X = 25, level3Y = 25;
int level4X = 30, level4Y = 30;
// local "constant" variables define the sizes of all circles
int level1Size = 80;
int level2Size = 50;
int level3Size = 30;
int level4Size = 20;
// other local variables are used to references Wheels objects
// used to draw the target.
Ellipse level1;
Ellipse level2;
Ellipse level3;
Ellipse level4;
// -----------------------------------------------------------------
/**
* Constructor for the Target class.
*/
public Target()
{
makeTarget( 0, 0 );
}
////////////////////////////////////////////////////////////////////
public Target( int x, int y )
{
makeTarget( x, y );
}
////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
/**
* setLocation( int x, int y ).
* change the location of the target;
* need to invoke setLocation on 4 ellipses that compose target.
*
* @param x int
* @param y int
*
*/
public void setLocation( int x, int y )
{
/////////////////////////////////////////////////////////
level1.setLocation( x, y );
level2.setLocation( x + level2X, y + level2Y);
level3.setLocation( x + level3X, y + level3Y);
level4.setLocation( x + level4X, y + level4Y);
////////////////////////////////////////////////////////
}
// -----------------------------------------------------------------
/**
* move( int dx, int dy ).
* move the location of the target by dx and dy
* newx = oldx + dx
* newy = oldy + dy
* use Target's setLocation method to actually change the location
*
* @param dx int
* @param dy int
*
*/
public void move( int dx, int dy )
{
/////////////////////////////////////////////////////////
int x = level1.getXLocation();
int y = level1.getYLocation();
setLocation( x + dx, y + dy );
////////////////////////////////////////////////////////
}
// -----------------------------------------------------------------
/**
* makeTarget.
* encapsulates all the Wheels components needed to draw a target.
*
* @param x int
* @param y int
*/
public void makeTarget( int x, int y )
{
// create the level1 circle
level1 = new Ellipse( x, y );
level1.setSize( level1Size, level1Size );
// create the next level4 circle
level2 = new Ellipse( x + level2X, y + level2Y );
level2.setSize( level2Size, level2Size );
level2.setColor( Color.BLUE );
// create the next level4 circle
level3 = new Ellipse( x + level3X, y + level3Y );
level3.setSize( level3Size, level3Size );
level3.setColor( Color.CYAN );
// create the level4 circle
level4 = new Ellipse( x + level4X, y + level4Y );
level4.setColor( Color.BLACK );
level4.setSize( level4Size, level4Size );
}
// -----------------------------------------------------------------
/** main program just invokes the class constructor.
*
* @param args String
*
*/
public static void main( String[] args )
{
Frame f = new Frame();
Target t1 = new Target();
////////////////////////////////////////////////////////////////
Target t2 = new Target( 120, 100 );
Target t3 = new Target( 200, 180 );
Utilities.sleep( 1000 );
t2.setLocation( 400, 400 );
Utilities.sleep( 1000 );
t3.move( 10, 10 );
Utilities.sleep( 500 );
t3.move( 20, 20 );
Utilities.sleep( 500 );
t3.move( 30, 30 );
Utilities.sleep( 500 );
t3.move( 40, 40 );
}
} //End of Class Target