-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIceRink.java
More file actions
89 lines (70 loc) · 2.76 KB
/
IceRink.java
File metadata and controls
89 lines (70 loc) · 2.76 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
package physicsSimulator;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;
//draws and manipulates the lines and pucks within the JFrame
public class IceRink extends JPanel implements ActionListener{
//private instance variables
private Timer t = new Timer(10, this); //sets the speed (fps) of the program
private Puck p1, p2;
private boolean collide = false;
//Receives the array from the InputOutput class and creates an IceRink object
public IceRink(double[] arr) {
p1 = new Puck(arr[0], arr[1], arr[2], arr[3], arr[4]);
p2 = new Puck(arr[5], arr[6], arr[7], arr[8], arr[9]);
}
//draws what the user will actually see on the JFrame
public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creates the outer box of the arena
g.drawLine(100,100,1300,100);
g.drawLine(100,100,100,700);
g.drawLine(1300,100,1300,700);
g.drawLine(100,700,1300,700);
//creates the two hockey pucks and fills them in
Ellipse2D circle = new Ellipse2D.Double(p1.getXPos() ,p1.getYPos(), 40, 40);
Ellipse2D circle2 = new Ellipse2D.Double(p2.getXPos() ,p2.getYPos(), 40, 40);
g2.fill(circle);
g2.fill(circle2);
//runs the action performed method below and waits the amount of time specified
t.start();
}
public void actionPerformed (ActionEvent e) {
//changes the pucks position and ensures it is in the boundaries
p1.movePuck();
inBounds(p1);
p2.movePuck();
inBounds(p2);
//if the pucks get too close to each other they will "collide"
if (!collide && p1.getXPos() - p2.getXPos() <= 35 && p1.getXPos() - p2.getXPos() >= -35
&& p1.getYPos() - p2.getYPos() <= 35 && p1.getYPos() - p2.getYPos() >= -35) {
Puck.collision2(p1, p2);
collide = true;
}
//pucks can only collide once per time they are close to each other
//and must separate before they can collide again
else if (p1.getXPos() - p2.getXPos() >= 60 || p1.getXPos() - p2.getXPos() <= -60
|| p1.getYPos() - p2.getYPos() >= 60 || p1.getYPos() - p2.getYPos() <= -60)
collide = false;
//reruns the paintComponent method
//These two methods will run back and forth until the X is pressed
repaint();
}
//if the puck is on or past the bounds it will point
//its velocity inward until it is within the bounds
public void inBounds(Puck p) {
if (p.getXPos() >= 1260)
p.setXVelocity(-1 * Math.abs(p.getXVelocity()));
else if (p.getXPos() <= 100)
p.setXVelocity(Math.abs(p.getXVelocity()));
if (p.getYPos() >= 660)
p.setYVelocity(-1 * Math.abs(p.getYVelocity()));
else if (p.getYPos() <= 100)
p.setYVelocity(Math.abs(p.getYVelocity()));
}
}