-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.java
More file actions
80 lines (66 loc) · 1.88 KB
/
ProgressBar.java
File metadata and controls
80 lines (66 loc) · 1.88 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
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/** @author Gergely Kota
Progress Bar shows progress of a Progressable
*/
public class ProgressBar extends JPanel implements ProgressListener
{
private Progressable owner;
// private Timer timer;
private double progress;
private int edge = 2, size = 16;
private Color color = new Color(0, 136, 157);
private String source;
public ProgressBar()
{
setPreferredSize(new Dimension(size, size));
source = "";
}
public void setProgress(double x)
{
progress = x;
progress = progress < 0? 0 : progress;
progress = progress > 1? 1 : progress;
revalidate();
repaint();
}
public void progressPerformed(ProgressEvent pe)
{
source = pe.getAction();
setProgress(pe.getSource().getProgress());
}
public ProgressBar setColor(Color c)
{
color = c;
return this;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.gray.darker().darker());
g.drawRect(0,0, getWidth()-1, getHeight()-1);
g.setColor(Color.green.darker().darker());
g.setColor(color);
int x = getWidth() - 2*edge;
int y = getHeight() - 2*edge;
g.fillRect(edge, edge, (int) Math.round(progress*x), y);
String s = "" + Math.round(100*progress) + " %";
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.PLAIN, 2*size/3));
int width = g.getFontMetrics().stringWidth(s);
int left = (x - width)/2;
g.drawString(s, left, size-edge);
g.drawString(source, 5, size-edge);
}
public static void main(String[] args)
{
JFrame jf1 = new JFrame();
jf1.setSize(500, 80);
jf1.setLocation(0,0);
jf1.getContentPane().setLayout(new GridLayout(2,1));
// jf1.getContentPane().add(new ProgressBar(new Tester(10)).setColor(Color.blue));
// jf1.getContentPane().add(new ProgressBar(new Tester(5)));
jf1.show();
}
}