-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleBackgroundPanel.java
More file actions
190 lines (159 loc) · 6.37 KB
/
ParticleBackgroundPanel.java
File metadata and controls
190 lines (159 loc) · 6.37 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
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
/**
* Dynamic starfield + floating dots background panel for premium UI.
*/
public class ParticleBackgroundPanel extends JPanel implements ActionListener {
private final int starCount;
private final float[] starPx, starPy, starPz; // star positions
private final float[] starR0, starR1; // star randomness
private final float[] starCr, starCg, starCb; // star colors
private final int dotCount = 30; // floating dots
private final float[] dotX, dotY; // dot positions
private final float[] dotVx, dotVy; // dot velocities
private final float[] dotSize; // dot sizes
private final float[] dotAlpha; // dot alpha
private final float spread;
private final float baseSize;
private final float sizeRandomness;
private final float cameraDistance;
private final Random rnd = new Random();
private final String[] palette = {"#FFD700", "#FFDF00", "#FFD59A", "#FFF9E6", "#FFFFFF"};
private final Timer timer;
private long startTime;
public ParticleBackgroundPanel() {
// Starfield tuned params
this.starCount = 150;
this.spread = 12f;
this.baseSize = 2f;
this.sizeRandomness = 0.45f;
this.cameraDistance = 28f;
starPx = new float[starCount];
starPy = new float[starCount];
starPz = new float[starCount];
starR0 = new float[starCount];
starR1 = new float[starCount];
starCr = new float[starCount];
starCg = new float[starCount];
starCb = new float[starCount];
dotX = new float[dotCount];
dotY = new float[dotCount];
dotVx = new float[dotCount];
dotVy = new float[dotCount];
dotSize = new float[dotCount];
dotAlpha = new float[dotCount];
initParticles();
initDots();
setOpaque(true);
startTime = System.currentTimeMillis();
timer = new Timer(30, this); // ~33 FPS for smooth animation
timer.start();
}
private void initDots() {
for (int i = 0; i < dotCount; i++) {
dotX[i] = rnd.nextFloat();
dotY[i] = rnd.nextFloat();
dotVx[i] = (rnd.nextFloat() - 0.5f) * 0.0003f;
dotVy[i] = (rnd.nextFloat() - 0.5f) * 0.0003f;
dotSize[i] = 3f + rnd.nextFloat() * 8f;
dotAlpha[i] = 0.15f + rnd.nextFloat() * 0.25f;
}
}
private void initParticles() {
for (int i = 0; i < starCount; i++) {
// Use normalized screen-space coordinates so stars are evenly dispersed
starPx[i] = rnd.nextFloat(); // 0..1 across width
starPy[i] = rnd.nextFloat(); // 0..1 across height
starPz[i] = rnd.nextFloat(); // depth factor 0..1 for size variation
starR0[i] = rnd.nextFloat(); // size variation
starR1[i] = rnd.nextFloat(); // brightness variation
float[] col = hexToRgb(palette[rnd.nextInt(palette.length)]);
starCr[i] = col[0];
starCg[i] = col[1];
starCb[i] = col[2];
}
}
private float[] hexToRgb(String hex) {
hex = hex.replaceFirst("^#", "");
if (hex.length() == 3) {
char[] cs = hex.toCharArray();
hex = "" + cs[0] + cs[0] + cs[1] + cs[1] + cs[2] + cs[2];
}
int val = Integer.parseInt(hex, 16);
float r = ((val >> 16) & 255) / 255f;
float g = ((val >> 8) & 255) / 255f;
float b = (val & 255) / 255f;
return new float[]{r, g, b};
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
int w = getWidth();
int h = getHeight();
// solid black background
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, w, h);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw stars
for (int i = 0; i < starCount; i++) {
float sx = starPx[i] * w;
float sy = starPy[i] * h;
float depthScale = 0.6f + 0.8f * starPz[i];
float size = baseSize * (1f + sizeRandomness * (starR0[i] - 0.5f)) * depthScale;
size = Math.max(1f, Math.min(6f, size));
float brightness = 0.6f + 0.4f * starR1[i];
int si = Math.max(1, Math.round(size));
Color outer = new Color(starCr[i], starCg[i], starCb[i], clamp(0.12f * brightness, 0f, 1f));
g2.setColor(outer);
int outerSize = si * 3;
g2.fillOval((int) (sx - outerSize / 2f), (int) (sy - outerSize / 2f), outerSize, outerSize);
Color main = new Color(starCr[i], starCg[i], starCb[i], clamp(0.9f * brightness, 0f, 1f));
g2.setColor(main);
g2.fillOval((int) (sx - si / 2f), (int) (sy - si / 2f), si, si);
int inner = Math.max(1, si / 2);
g2.setColor(new Color(1f, 0.96f, 0.6f, clamp(1f * brightness, 0f, 1f)));
g2.fillOval((int) (sx - inner / 2f), (int) (sy - inner / 2f), inner, inner);
}
// Draw floating dots with subtle glow
for (int i = 0; i < dotCount; i++) {
float dx = dotX[i] * w;
float dy = dotY[i] * h;
int ds = Math.round(dotSize[i]);
// Outer soft glow
g2.setColor(new Color(255, 215, 0, (int) (dotAlpha[i] * 40)));
g2.fillOval((int) (dx - ds * 1.5f), (int) (dy - ds * 1.5f), ds * 3, ds * 3);
// Main dot
g2.setColor(new Color(255, 215, 0, (int) (dotAlpha[i] * 255)));
g2.fillOval((int) (dx - ds / 2f), (int) (dy - ds / 2f), ds, ds);
}
g2.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
// Update floating dots positions
for (int i = 0; i < dotCount; i++) {
dotX[i] += dotVx[i];
dotY[i] += dotVy[i];
// Wrap around screen
if (dotX[i] < 0) {
dotX[i] = 1f;
}
if (dotX[i] > 1) {
dotX[i] = 0f;
}
if (dotY[i] < 0) {
dotY[i] = 1f;
}
if (dotY[i] > 1) {
dotY[i] = 0f;
}
}
repaint();
}
private static float clamp(float v, float a, float b) {
return Math.max(a, Math.min(b, v));
}
}