KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > awtui > ProgressBar


1 package junit.awtui;
2
3 import java.awt.Canvas JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Graphics JavaDoc;
6 import java.awt.Rectangle JavaDoc;
7 import java.awt.SystemColor JavaDoc;
8
9 public class ProgressBar extends Canvas JavaDoc {
10     public boolean fError= false;
11     public int fTotal= 0;
12     public int fProgress= 0;
13     public int fProgressX= 0;
14
15     public ProgressBar() {
16         super();
17         setSize(20, 30);
18     }
19     
20     private Color JavaDoc getStatusColor() {
21         if (fError)
22             return Color.red;
23         return Color.green;
24     }
25     
26     public void paint(Graphics JavaDoc g) {
27         paintBackground(g);
28         paintStatus(g);
29     }
30     
31     public void paintBackground(Graphics JavaDoc g) {
32         g.setColor(SystemColor.control);
33         Rectangle JavaDoc r= getBounds();
34         g.fillRect(0, 0, r.width, r.height);
35         g.setColor(Color.darkGray);
36         g.drawLine(0, 0, r.width-1, 0);
37         g.drawLine(0, 0, 0, r.height-1);
38         g.setColor(Color.white);
39         g.drawLine(r.width-1, 0, r.width-1, r.height-1);
40         g.drawLine(0, r.height-1, r.width-1, r.height-1);
41     }
42     
43     public void paintStatus(Graphics JavaDoc g) {
44         g.setColor(getStatusColor());
45         Rectangle JavaDoc r= new Rectangle JavaDoc(0, 0, fProgressX, getBounds().height);
46         g.fillRect(1, 1, r.width-1, r.height-2);
47     }
48     
49     private void paintStep(int startX, int endX) {
50         repaint(startX, 1, endX-startX, getBounds().height-2);
51     }
52     
53     public void reset() {
54         fProgressX= 1;
55         fProgress= 0;
56         fError= false;
57         paint(getGraphics());
58     }
59     
60     public int scale(int value) {
61         if (fTotal > 0)
62             return Math.max(1, value*(getBounds().width-1)/fTotal);
63         return value;
64     }
65     
66     public void setBounds(int x, int y, int w, int h) {
67         super.setBounds(x, y, w, h);
68         fProgressX= scale(fProgress);
69     }
70     
71     public void start(int total) {
72         fTotal= total;
73         reset();
74     }
75     
76     public void step(boolean successful) {
77         fProgress++;
78         int x= fProgressX;
79
80         fProgressX= scale(fProgress);
81
82         if (!fError && !successful) {
83             fError= true;
84             x= 1;
85         }
86         paintStep(x, fProgressX);
87     }
88 }
Popular Tags