forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackHoleSimulation.java
More file actions
166 lines (148 loc) · 5.56 KB
/
BlackHoleSimulation.java
File metadata and controls
166 lines (148 loc) · 5.56 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
/**
* BlackHoleSimulation.java
*
* Description:
* A graphical Java program simulating particles falling into a black hole.
* Demonstrates advanced Java features:
* - Swing GUI for visualization
* - OOP design with Particle and Panel classes
* - Virtual threads (Java 21+) for lightweight concurrency
*
* Requirements:
* - Java 21 or higher (for virtual threads support)
*
* Usage:
* javac BlackHoleSimulation.java
* java BlackHoleSimulation
*
* Features:
* - Configurable number of particles
* - Particles rotate and fall into the black hole
* - Automatically closes the GUI after all particles have fallen
* - Colorful particle visualization
*/
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class BlackHoleSimulation {
static final int NUM_PARTICLES = 500; // Total number of particles
static final int WINDOW_WIDTH = 800; // Window width
static final int WINDOW_HEIGHT = 600; // Window height
static final int BLACK_HOLE_SIZE = 60; // Diameter of black hole
static final int PARTICLE_SIZE = 5; // Diameter of each particle
static final double PARTICLE_SPEED_MIN = 1.0; // Minimum particle speed
static final double PARTICLE_SPEED_MAX = 3.0; // Maximum particle speed
static final double PARTICLE_ROTATION_STEP = 0.05; // Rotation angle per move
static final int TIMER_DELAY = 30; // ms per repaint
static final int PARTICLE_THREAD_DELAY = 50; // ms per particle movement update
/**
* Represents a particle moving towards the black hole.
*/
static class Particle {
double angle; // Current angle for rotation
double distance; // Current distance from black hole center
double speed; // Distance reduction per step
Color color; // Particle color
Particle() {
angle = ThreadLocalRandom.current().nextDouble(0, 2 * Math.PI);
distance = ThreadLocalRandom.current().nextDouble(50, 250);
speed = ThreadLocalRandom.current().nextDouble(PARTICLE_SPEED_MIN, PARTICLE_SPEED_MAX);
color = new Color(ThreadLocalRandom.current().nextFloat(),
ThreadLocalRandom.current().nextFloat(),
ThreadLocalRandom.current().nextFloat());
}
/**
* Updates particle position by reducing distance and rotating slightly.
*/
void move() {
distance -= speed;
if (distance < 0) distance = 0;
angle += PARTICLE_ROTATION_STEP;
}
/**
* Returns X coordinate for drawing.
*/
int getX(int centerX) {
return (int) (centerX + distance * Math.cos(angle));
}
/**
* Returns Y coordinate for drawing.
*/
int getY(int centerY) {
return (int) (centerY + distance * Math.sin(angle));
}
/**
* Checks if the particle has reached the black hole.
*/
boolean hasFallen() {
return distance <= 0;
}
}
/**
* Custom JPanel to draw black hole and particles.
*/
static class BlackHolePanel extends JPanel {
java.util.List<Particle> particles;
BlackHolePanel(java.util.List<Particle> particles) {
this.particles = particles;
setBackground(Color.BLACK);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = WINDOW_WIDTH / 2;
int centerY = WINDOW_HEIGHT / 2;
// Draw black hole
g2d.setColor(Color.DARK_GRAY);
g2d.fillOval(centerX - BLACK_HOLE_SIZE / 2,
centerY - BLACK_HOLE_SIZE / 2,
BLACK_HOLE_SIZE,
BLACK_HOLE_SIZE);
// Draw active particles
for (Particle p : particles) {
if (!p.hasFallen()) {
g2d.setColor(p.color);
g2d.fillOval(p.getX(centerX),
p.getY(centerY),
PARTICLE_SIZE,
PARTICLE_SIZE);
}
}
}
}
public static void main(String[] args) {
List<Particle> particles = new ArrayList<>();
for (int i = 0; i < NUM_PARTICLES; i++) {
particles.add(new Particle());
}
JFrame frame = new JFrame("Black Hole Simulation");
BlackHolePanel panel = new BlackHolePanel(particles);
frame.add(panel);
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Start particle movement using virtual threads
particles.forEach(p -> Thread.startVirtualThread(() -> {
while (!p.hasFallen()) {
p.move();
try {
Thread.sleep(PARTICLE_THREAD_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}));
Timer timer = new Timer(TIMER_DELAY, e -> {
panel.repaint();
if (particles.stream().allMatch(Particle::hasFallen)) {
((Timer) e.getSource()).stop();
frame.dispose();
System.out.println("All particles have fallen into the black hole!");
}
});
timer.start();
}
}