-
Notifications
You must be signed in to change notification settings - Fork 0
/
BallFrame.java
128 lines (109 loc) · 4.56 KB
/
BallFrame.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
public class BallFrame extends JFrame {
private BackButton backButton; // Declaration of backButton object
private BouncingBall bouncingBall; // Declaration of bouncingBall object
public int frameWidth; // Declaration of frameWidth variable
public int frameHeight; // Declaration of frameHeight variable
public BallFrame(Color backgroundColor, JFrame mainFrame) {
this.getContentPane().setBackground(backgroundColor);
this.setSize(700, 700);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.getContentPane().setLayout(null);
backButton = new BackButton(this, mainFrame); // Creating an instance of BackButton
backButton.setBounds(10, 10, 100, 50);
backButton.setOpaque(false);
this.getContentPane().add(backButton);
bouncingBall = new BouncingBall(this); // Creating an instance of BouncingBall
bouncingBall.setBounds(0, 0, 700, 700);
bouncingBall.setOpaque(false);
this.getContentPane().add(bouncingBall);
this.addComponentListener(new ComponentAdapter() { // Adding a ComponentListener
@Override
public void componentResized(ComponentEvent e) {
frameWidth = getWidth(); // Assigning the width of the frame to frameWidth
frameHeight = getHeight(); // Assigning the height of the frame to frameHeight
System.out.println("Frame width: " + frameWidth + ", Frame height: " + frameHeight);
}
});
}
}
/**
* Represents a bouncing ball in a BallFrame.
*/
class BouncingBall extends JPanel {
double x = 0, y = 0, vx = 2, vy = 0; // Declaration of variables
double gravity = 0.1, friction = 0.01, restitution = 0.7; // Declaration of variables
int radius = 25; // Declaration of radius variable
int groundHeight; // Declaration of groundHeight variable
BallFrame ballFrame; // Declaration of ballFrame object
private boolean isDragging = false; // Declaration of isDragging variable
public BouncingBall(BallFrame ballFrame) {
this.ballFrame = ballFrame; // Assigning the passed BallFrame object to ballFrame
Timer timer = new Timer(5, e -> { // Creating a Timer
// gravity
vy += gravity;
// Apply friction
vx *= (1 - friction);
vy *= (1 - friction);
// Update pos
x += vx;
y += vy;
// Check for collision with edge
if (x - radius < 0) {
x = radius;
vx = -vx * restitution;
} else if (x + radius > ballFrame.frameWidth) {
x = ballFrame.frameWidth - radius;
vx = -vx * restitution;
}
if (y - radius < 0) {
y = radius;
vy = -vy * restitution;
} else if (y + radius > ballFrame.frameHeight - groundHeight) {
y = ballFrame.frameHeight - groundHeight - radius;
vy = -vy * restitution;
}
// Update the ground height based on the frame height
groundHeight = ballFrame.frameHeight / 5;
repaint();
});
timer.start();
this.addMouseMotionListener(new MouseAdapter() { // Adding a MouseMotionListener
@Override
public void mouseDragged(MouseEvent e) {
if (isDragging) {
x = e.getX() - radius;
y = e.getY() - radius;
}
}
});
this.addMouseListener(new MouseAdapter() { // Adding a MouseListener
@Override
public void mousePressed(MouseEvent e) {
double distance = Math.hypot(e.getX() - x, e.getY() - y);
if (distance < radius) {
isDragging = true;
}
}
@Override
public void mouseReleased(MouseEvent e) {
isDragging = false;
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval((int) (x - radius), (int) (y - radius), 2 * radius, 2 * radius);
int groundY = ballFrame.frameHeight - groundHeight;
int lineThickness = 5;
for (int i = 0; i < lineThickness; i++) {
g.drawLine(0, groundY + i, ballFrame.frameWidth, groundY + i);
}
}
}