-
Notifications
You must be signed in to change notification settings - Fork 2
/
HMeter.java
394 lines (353 loc) · 11.4 KB
/
HMeter.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//@ author Muhammad Hasnat Rasool
// HMeter widget to show progress in Applications
// All rights reserved @2024
package Hasnat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
/**
* A highly customizable horizontal progress meter widget.
*/
public class HMeter extends JPanel {
// Fields
private int progress = 0; // Current progress value
private Color progressColor = Color.GREEN; // Color of the progress arc
private Color textColor = Color.BLACK; // Color of the text
private Color backgroundColor = Color.WHITE; // Background color of the meter
private int padding = 10; // Padding around the meter
private String text = ""; // Text displayed in the meter
private boolean isHollow = true; // Whether the progress arc is hollow or filled
private Color boundaryColor = Color.GRAY; // Color of the boundary circle
private int radius = 100; // Radius of the meter
private boolean animate = false; // Whether the progress should be animated
private int animationSpeed = 50; // Speed of the animation
private boolean enabled = true; // Whether the meter is enabled or not
/**
* Constructs a new HMeter with default progress and text.
*/
public HMeter() {
this(0, "");
}
/**
* Constructs a new HMeter with specified progress and default text.
*
* @param progress Initial progress value (0-100)
*/
public HMeter(int progress) {
this(progress, "");
}
/**
* Constructs a new HMeter with specified progress and text.
*
* @param progress Initial progress value (0-100)
* @param text Text to be displayed in the meter
*/
public HMeter(int progress, String text) {
setPreferredSize(new Dimension(200, 200));
setBackground(backgroundColor);
setProgress(progress);
setText(text);
}
// Setter and Getter methods
/**
* Sets the progress value of the meter.
*
* @param progress The progress value (0-100)
*/
public void setProgress(int progress) {
if (progress > -500 && progress < 501) {
this.progress = progress;
} else {
progress = 0;
this.progress = progress;
}
repaint();
}
/**
* Sets the color of the progress arc.
*
* @param progressColor The color of the progress arc
*/
public void setProgressColor(Color progressColor) {
this.progressColor = progressColor;
repaint();
}
/**
* Sets the color of the text.
*
* @param textColor The color of the text
*/
public void setTextColor(Color textColor) {
this.textColor = textColor;
repaint();
}
/**
* Sets the background color of the meter.
*
* @param backgroundColor The background color of the meter
*/
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
repaint();
}
/**
* Sets the padding around the meter.
*
* @param padding The padding around the meter
*/
public void setPadding(int padding) {
this.padding = padding;
repaint();
}
/**
* Sets the text to be displayed in the meter.
*
* @param text The text to be displayed
*/
public void setText(String text) {
if (text.length() < 12) {
this.text = text;
}
repaint();
}
/**
* Sets whether the progress arc is hollow or filled.
*
* @param isHollow True for a hollow arc, false for a filled arc
*/
public void setHollow(boolean isHollow) {
this.isHollow = isHollow;
repaint();
}
/**
* Sets the color of the boundary circle.
*
* @param boundaryColor The color of the boundary circle
*/
public void setBoundaryColor(Color boundaryColor) {
this.boundaryColor = boundaryColor;
repaint();
}
/**
* Sets the radius of the meter.
*
* @param radius The radius of the meter
*/
public void setRadius(int radius) {
this.radius = radius;
repaint();
}
/**
* Sets whether the progress should be animated.
*
* @param animate True to enable animation, false otherwise
*/
public void setAnimate(boolean animate) {
this.animate = animate;
if (animate) {
new Thread(new AnimationThread()).start();
}
}
/**
* Gets the current progress value of the meter.
*
* @return The current progress value
*/
public int getProgress() {
return progress;
}
/**
* Gets the color of the progress arc.
*
* @return The color of the progress arc
*/
public Color getProgressColor() {
return progressColor;
}
/**
* Gets the color of the text.
*
* @return The color of the text
*/
public Color getTextColor() {
return textColor;
}
/**
* Gets the background color of the meter.
*
* @return The background color of the meter
*/
public Color getBackgroundColor() {
return backgroundColor;
}
/**
* Gets the padding around the meter.
*
* @return The padding around the meter
*/
public int getPadding() {
return padding;
}
/**
* Gets the text displayed in the meter.
*
* @return The text displayed in the meter
*/
public String getText() {
return text;
}
/**
* Checks if the progress arc is hollow or filled.
*
* @return True if the arc is hollow, false otherwise
*/
public boolean isHollow() {
return isHollow;
}
/**
* Gets the color of the boundary circle.
*
* @return The color of the boundary circle
*/
public Color getBoundaryColor() {
return boundaryColor;
}
/**
* Gets the radius of the meter.
*
* @return The radius of the meter
*/
public int getRadius() {
return radius;
}
/**
* Sets whether the meter is enabled or not.
*
* @param enable True to enable the meter, false to disable
*/
public void setEnable(boolean enable) {
this.enabled = enable;
if (enable) {
addMouseMotionListener(new MouseMotionAdapter() {
int lastX = 0;
int direction = 0; // 0: no direction, 1: right, -1: left
int threshold = 5; // adjust this value to set the sensitivity
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (Math.abs(x - lastX) >= threshold) {
if (x > lastX) { // right direction
direction = 1;
} else if (x < lastX) { // left direction
direction = -1;
}
int progress = getProgress();
progress = progress + direction;
if (progress >= 0 && progress <= 100) {
setProgress(progress);
} else if (progress < 0) {
setProgress(0);
} else if (progress > 100) {
setProgress(100);
}
}
lastX = x;
}
});
} else {
removeMouseMotionListener(new MouseMotionAdapter() {
// empty implementation
});
}
}
/**
* Adds a mouse motion listener to the meter.
*
* @param listener The mouse motion listener to be added
*/
public void addMouseMotionListener(MouseMotionAdapter listener) {
super.addMouseMotionListener(listener);
}
/**
* Checks if the meter is enabled.
*
* @return True if the meter is enabled, false otherwise
*/
public boolean isEnabled() {
return enabled;
}
/**
* Sets the bounds of the meter, ensuring a minimum height.
*
* @param x The x-coordinate of the upper-left corner
* @param y The y-coordinate of the upper-left corner
* @param width The width of the meter
* @param height The height of the meter
*/
public void setBounds(int x, int y, int width, int height) {
if (height < 150) {
height = 150;
}
super.setBounds(x, y, width, height);
}
/**
* Custom painting method for drawing the meter components.
*
* @param g The graphics context
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw the background circle
Ellipse2D circle = new Ellipse2D.Double(padding, padding, getWidth() - padding * 2, getHeight() - padding * 2);
g2d.setColor(backgroundColor);
g2d.fill(circle);
// Draw the boundary circle
g2d.setColor(boundaryColor);
g2d.setStroke(new BasicStroke(10));
g2d.drawArc(padding, padding, getWidth() - padding * 2, getHeight() - padding * 2, 0, 360);
// Draw the progress arc
g2d.setColor(progressColor);
if (isHollow) {
g2d.setStroke(new BasicStroke(10));
g2d.drawArc(padding, padding, getWidth() - padding * 2, getHeight() - padding * 2, 0, (int) (360 * (progress / 100.0)));
} else {
g2d.fillArc(padding, padding, getWidth() - padding * 2, getHeight() - padding * 2, 0, (int) (360 * (progress / 100.0)));
}
// Draw the text
g2d.setColor(textColor);
Font font = new Font("Tahoma", Font.BOLD, 18);
g2d.setFont(font);
int textWidth = g2d.getFontMetrics().stringWidth(text);
int textHeight = g2d.getFontMetrics().getAscent();
g2d.drawString(text, (getWidth() - textWidth) / 2, ((getHeight() + 50) + textHeight) / 2);
// Draw the progress percentage
String progressText = progress + "%";
int progressTextWidth = g2d.getFontMetrics().stringWidth(progressText);
int progressTextHeight = g2d.getFontMetrics().getAscent();
g2d.drawString(progressText, (getWidth() - progressTextWidth) / 2, ((getHeight() - 50) + progressTextHeight) / 2);
}
// Inner class for handling animation
private class AnimationThread implements Runnable {
@Override
public void run() {
while (animate) {
progress++;
if (progress > 100) {
progress = 0;
}
repaint();
try {
Thread.sleep(animationSpeed);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}