-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingComponent.java
More file actions
277 lines (244 loc) · 9.48 KB
/
DrawingComponent.java
File metadata and controls
277 lines (244 loc) · 9.48 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Gerard Ortega, 171668
I have not discussed the Java language code
in my program with anyone
other than my instructor or the teaching
assistants assigned to this course.
I have not used Java language code
obtained from another student, or
any other unauthorized source, either
modified or unmodified.
If any Java language
code or documentation used in my program was
obtained from another source, such as a text
book or course notes, those have been clearly
noted with a proper citation in the
comments of my code. */
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public class DrawingComponent extends JComponent
{
private int width;
private int height;
private Point2D.Double vanishingPoint;
private Line frameBottom;
private int horizonY;
ArrayList<DrawingObject> allDrawingObjects;
Timer animationTimer;
private int leftGroudon_i;
private int rightGroudon_i;
private RedOrb orb;
public DrawingComponent(int w, int h)
{
width = w;
height = h;
allDrawingObjects = new ArrayList<DrawingObject>();
vanishingPoint = new Point2D.Double(512,350);
horizonY = 400;
frameBottom = new Line(0,height,width,height);
setUpBackground();
setUpWater();
decorateWall();
giveGreetings();
setUpListeners();
setUpBGM();
}
public void setUpBGM()
{
try
{
URL url = this.getClass().getClassLoader().getResource("BGM.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch(Exception e)
{
System.out.println("An error happened in reading the BGM audio file.");
}
}
public void setUpListeners()
{
//Make Groudon's Eyes Follow You
MouseMotionListener mouseTracker = new MouseMotionListener()
{
@Override
public void mouseMoved(MouseEvent me)
{
double x = me.getX();
double y = me.getY();
((Groudon)allDrawingObjects.get(leftGroudon_i)).updateEyes(x,y);
((Groudon)allDrawingObjects.get(rightGroudon_i)).updateEyes(x,y);
if(orb.isInside(x,y))
{
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
if(!orb.getIsGlowing())
orb.flipGlowing();
}
else
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
if(orb.getIsGlowing())
orb.flipGlowing();
}
}
public void mouseDragged(MouseEvent me){}
};
this.addMouseMotionListener(mouseTracker);
MouseListener clickTracker = new MouseListener()
{
@Override
public void mousePressed(MouseEvent me)
{
if(orb.isInside(me.getX(),me.getY()))
{
for(int i = 0; i < allDrawingObjects.size(); i++)
if(allDrawingObjects.get(i).canBeAwakened())
allDrawingObjects.get(i).awakenToggle();
try
{
URL url = this.getClass().getClassLoader().getResource( !((Groudon)allDrawingObjects.get(leftGroudon_i)).isAwakened() ? "383 - Groudon.wav" : "383P - Groudon (Primal).wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
}
catch(Exception e)
{
System.out.println("An error happened in reading the audio file.");
}
}
}
@Override
public void mouseClicked(MouseEvent me){}
@Override
public void mouseExited(MouseEvent me){}
@Override
public void mouseEntered(MouseEvent me){}
@Override
public void mouseReleased(MouseEvent me){}
};
this.addMouseListener(clickTracker);
ActionListener animator = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
animateAll();
}
};
animationTimer = new Timer(10,animator);
animationTimer.setRepeats(true);
animationTimer.start();
}
public void setUpBackground()
{
Rect theWall = new Rect(0,0,width,horizonY,new Color(128,0,0), new Color(168,40,40));
Rect theFloor = new Rect(0,horizonY,width,height-(vanishingPoint.y+50), new Color(237,28,36), new Color(200,0,0));
Line horizon = new Line(0,horizonY,1024,horizonY);
Line shadingLine = new Line(vanishingPoint, new Point2D.Double(400,height));
Poly floorShading = new Poly(horizon.getIntersection(shadingLine), ((Color)theFloor.getColor(false)).darker());
floorShading.awakenableToggle();
floorShading.setAwakenedFillColor(((Color)theFloor.getColor(true)).darker());
floorShading.addPoint(frameBottom.getIntersection(shadingLine));
floorShading.addPoint(new Point2D.Double(0,height));
floorShading.addPoint(new Point2D.Double(0,horizonY));
allDrawingObjects.add(theWall);
allDrawingObjects.add(theFloor);
allDrawingObjects.add(floorShading);
}
private void setUpWater() //drawn in perspective
{
double holeBottomWidth = 800;
Line topPartOfHoleGuide = new Line(0,vanishingPoint.y+150.0,1,vanishingPoint.y+150.0);
Line leftTopSideOfHoleGuide = new Line(vanishingPoint, new Point2D.Double(vanishingPoint.x - holeBottomWidth/2.0, height));
Line rightTopSideOfHoleGuide = new Line(vanishingPoint, new Point2D.Double(vanishingPoint.x + holeBottomWidth/2.0, height));
Point2D.Double topLeftCornerOfHole = topPartOfHoleGuide.getIntersection(leftTopSideOfHoleGuide);
Point2D.Double topRightCornerOfHole = topPartOfHoleGuide.getIntersection(rightTopSideOfHoleGuide);
Line leftDepthLine = new Line(topLeftCornerOfHole,new Point2D.Double(topLeftCornerOfHole.x,topLeftCornerOfHole.y+80));
Line rightDepthLine = new Line(topRightCornerOfHole,new Point2D.Double(topRightCornerOfHole.x,topRightCornerOfHole.y+80));
Line topPartOfWater = new Line(leftDepthLine.pt2,rightDepthLine.pt2);
Line leftSideOfWaterGuide = (new Line(vanishingPoint,topPartOfWater.pt1));
Line rightSideOfWaterGuide = (new Line(vanishingPoint,topPartOfWater.pt2));
Poly dropDown = new Poly(leftDepthLine.pt2,new Color(180,180,180));
dropDown.addPoint(rightDepthLine.pt2);
dropDown.addPoint(rightSideOfWaterGuide.getIntersection(frameBottom));
dropDown.addPoint(rightTopSideOfHoleGuide.pt2);
dropDown.addPoint(rightDepthLine.pt1);
dropDown.addPoint(leftDepthLine.pt1);
dropDown.addPoint(leftTopSideOfHoleGuide.pt2);
dropDown.addPoint(leftSideOfWaterGuide.getIntersection(frameBottom));
allDrawingObjects.add(dropDown);
Poly dropDownDarkShade = new Poly(leftDepthLine.pt2, new Color(160,160,195));
dropDownDarkShade.addPoint(leftSideOfWaterGuide.getIntersection(frameBottom));
dropDownDarkShade.addPoint(leftTopSideOfHoleGuide.pt2);
dropDownDarkShade.addPoint(leftDepthLine.pt1);
dropDownDarkShade.addPoint(rightDepthLine.pt1);
dropDownDarkShade.addPoint(new Point2D.Double(rightDepthLine.pt1.x,rightDepthLine.pt1.y+30));
dropDownDarkShade.addPoint(new Point2D.Double(leftDepthLine.pt1.x,leftDepthLine.pt1.y+40));
allDrawingObjects.add(dropDownDarkShade);
BedOfWater theWater = new BedOfWater(topPartOfWater, frameBottom, vanishingPoint, 80);
allDrawingObjects.add(theWater);
/*
allDrawingObjects.add(new Line(0,vanishingPoint.y + 50,width,vanishingPoint.y + 50, Color.BLACK, 3)); //the floor
allDrawingObjects.add(new Line(topLeftCornerOfHole,topRightCornerOfHole));
allDrawingObjects.add(new Line(topLeftCornerOfHole,leftTopSideOfHoleGuide.pt2));
allDrawingObjects.add(new Line(topRightCornerOfHole,rightTopSideOfHoleGuide.pt2));
allDrawingObjects.add(leftDepthLine);
allDrawingObjects.add(rightDepthLine);
allDrawingObjects.add(topPartOfWater);
allDrawingObjects.add(new Line(leftDepthLine.pt2,leftSideOfWaterGuide.getIntersection(new Line(0,768,1024,768))));
allDrawingObjects.add(new Line(rightDepthLine.pt2,rightSideOfWaterGuide.getIntersection(new Line(0,768,1024,768))));
*/
}
private void decorateWall()
{
Banner leftBanner = new Banner(35,10,new Color(50,50,50),300,5);
allDrawingObjects.add(leftBanner);
Banner midBanner = new Banner(380,40,new Color(50,50,50),280,10);
allDrawingObjects.add(midBanner);
Banner rightBanner = new Banner(705,10,new Color(50,50,50),300,5);
allDrawingObjects.add(rightBanner);
Groudon leftGroudon = new Groudon(80,15);
Groudon rightGroudon = new Groudon(750,15);
leftGroudon_i = allDrawingObjects.size();
allDrawingObjects.add(leftGroudon);
rightGroudon_i = allDrawingObjects.size();
allDrawingObjects.add(rightGroudon);
MagmaM theBigM = new MagmaM(415,105);
allDrawingObjects.add(theBigM);
Point2D.Float orbCenter = new Point2D.Float(515f,425f);
float orbRadius = 50f;
orb = new RedOrb(orbCenter,orbRadius);
allDrawingObjects.add(orb);
}
private void giveGreetings()
{
GreetingText gText = new GreetingText(263,30);
allDrawingObjects.add(gText);
}
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(rh);
AffineTransform af = g2d.getTransform();
for(int i = 0; i < allDrawingObjects.size(); i++)
allDrawingObjects.get(i).draw(g2d,af);
//g2d.setColor(Color.BLACK);
//g2d.fillOval((int)vanishingPoint.x-5,(int)vanishingPoint.y-5,10,10);
}
private void animateAll()
{
for(int i = 0; i < allDrawingObjects.size(); i++)
allDrawingObjects.get(i).animate();
repaint();
}
}