-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawingComponent.java
145 lines (113 loc) · 3.55 KB
/
DrawingComponent.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
package com.company;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
public class DrawingComponent extends JComponent {
Ellipse2D.Double ball = new Ellipse2D.Double(100, 500, 40, 40);
Rectangle hoop = new Rectangle(100, 500, 40, 40);
Rectangle hoop2 = new Rectangle(300, 200, 40, 40);
private boolean hitHoop1 = true;
private boolean hitHoop2 = false;
private int score = 0;
private boolean isBeingRealigned = false;
public DrawingComponent() {}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("not sure", Font.BOLD, 40));
g2.drawString("" + score, 290, 50);
g2.setColor(new Color(240, 117, 45));
g2.fill(ball);
g2.setColor(new Color(240, 12, 87));
g2.setStroke(new BasicStroke(5));
g2.draw(hoop);
g2.draw(hoop2);
}
public void moveBall2(double xChange, double yChange) {
ball.x += xChange;
ball.y -= yChange;
if(xChange != 0 || yChange != 0){
hitHoop1 =false;
hitHoop2 = false;
}
if (hoop2ContainsBall(hoop2, ball.x, ball.y)) {
ball.x = hoop2.x;
ball.y = hoop2.y;
hitHoop2 = true;
DunkShotRunner.setInitalChanges(0, 0);
}
if (hoopContainsBall(hoop, ball.x, ball.y)) {
ball.x = hoop.x;
ball.y = hoop.y;
hitHoop1 = true;
DunkShotRunner.setInitalChanges(0, 0);
}
if (!hitHoop1 && !hitHoop2) {
DunkShotRunner.decreaseY();
}
if(hitHoop2 && hoop2.y >= 647){
score++;
}
}
public boolean hoopContainsBall(Rectangle hoop, double ballX, double ballY) {
if (hoop.getX() <= ballX && hoop.getX() + hoop.getWidth() >= ballX && hoop.getY() <= ballY && hoop.getY() + hoop.getHeight() >= ballY) {
return true;
} else {
return false;
}
}
public boolean hoop2ContainsBall(Rectangle hoop, double ballX, double ballY) {
if (hoop.getX() -20 <= ballX && hoop.getX() + hoop.getWidth() >= ballX && hoop.getY() -20 <= ballY && hoop.getY() + hoop.getHeight() >= ballY) {
return true;
} else {
return false;
}
}
public boolean isOut(){
if(ball.x >= 560 || ball.x <= 0){
return true;
}else
return false;
}
public double getBallY() {
return ball.getY();
}
public double getHoopX() {
return hoop.getX();
}
public double getHoopY() {
return hoop.getY();
}
public boolean isInHoop2(){
return hitHoop2;
}
public int getScore(){
return score;
}
public void reset() {
ball = new Ellipse2D.Double(100, 500, 40, 40);
hoop = new Rectangle(100, 500, 40, 40);
hoop2 = new Rectangle(300, 200, 40, 40);
hitHoop1 = true;
hitHoop2 = false;
score = 0;
isBeingRealigned = false;
}
public void realign(){
if(ball.y < 650){
ball.y += 3;
hoop.y += 3;
hoop2.y += 3;
isBeingRealigned = true;
}
if(ball.y >= 650){
hoop = hoop2;
hoop2 = new Rectangle( (int)(20 + Math.random() * 550), (int)(125 + Math.random() * 425), 40, 40);
hitHoop2 = false;
hitHoop1 = true;
isBeingRealigned = false;
}
}
public boolean getRealgined() {
return isBeingRealigned;
}
}