/Users/lyon/j4p/src/ip/gui/frames/ProgressFrame.java

1    package ip.gui.frames; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.WindowEvent; 
6     
7    public class ProgressFrame extends Frame { 
8        long startTime = System.currentTimeMillis(); 
9        private static final long UNIT = 1000; 
10       long elapsedTime = 0; 
11       float timeLeft = 0; 
12       float total = 0; 
13       int n = 0; 
14       String[] s = new String[4]; 
15       double db; 
16    
17       public ProgressFrame(String title) { 
18           super(title); 
19           setSize(200, 200); 
20    
21       } 
22    
23       public void actionPerformed(ActionEvent e) { 
24           if (e.getID() == WindowEvent.WINDOW_CLOSING) { 
25               setVisible(false); 
26               dispose(); 
27               return; 
28           } 
29       } 
30    
31       public static String f2(double f) { 
32           int i = (int) f; 
33           double frac = f - i; 
34           int trunFrac = (int) (frac * 100); 
35           return i + "." + trunFrac; 
36       } 
37    
38       public void setRatioComplete(double d, String id) { 
39           db = d; 
40           s[0] = id; 
41           elapsedTime = System.currentTimeMillis() 
42                   - startTime; 
43           timeLeft = (float) ((1 / d - 1) * elapsedTime); 
44    
45           s[1] = "Elapsed Time: " + f2(elapsedTime / UNIT) + " Seconds"; 
46           s[2] = 
47                   "Estimated time left: " 
48                   + f2(timeLeft / UNIT) 
49                   + " Seconds"; 
50           s[3] = "Percent done:" 
51                   + (int) (d * 100) + "%"; 
52           repaint(); 
53       } 
54    
55       public void paint(Graphics g) { 
56    
57           Font f = new Font("Times", Font.PLAIN, 10); 
58           FontMetrics fm = g.getFontMetrics(f); 
59    
60           g.setFont(f); 
61    
62           Dimension d = getSize(); 
63    
64           int sideMargin = 30; 
65           int topMargin = 40; 
66           int stringxMargin = 15; 
67           int markMargin = 5; 
68    
69           int w = d.width - sideMargin * 2; 
70           int h = d.height / 4; 
71    
72           setBackground(Color.white); 
73           g.setColor(Color.black); 
74    
75           g.clearRect(0, 0, d.width, d.height); 
76           g.drawRect(sideMargin, topMargin, w, h); 
77    
78           g.drawString(s[0], sideMargin, topMargin + h + 20); 
79           g.drawString(s[1], sideMargin, topMargin + h + 40); 
80           g.drawString(s[2], sideMargin, topMargin + h + 60); 
81    
82           g.fillRect(sideMargin, topMargin, (int) (w * db), h); 
83    
84       } 
85    
86       public static void main(String args[]) { 
87           ProgressFrame pb = 
88                   new ProgressFrame("test prog"); 
89           pb.show(); 
90           for (double d = 0; d < 1; d = d + 0.1) { 
91               pb.setRatioComplete(d, 
92                       "Percent done:" + (int) (d * 100) + "%"); 
93               try { 
94                   Thread.sleep(500); 
95               } catch (Exception e) { 
96               } 
97               ; 
98           } 
99       } 
100   
101  }