Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ProgressBar.java
1 package ij.gui;
2 
3 import java.awt.*;
4 import java.awt.image.*;
5 
9 public class ProgressBar extends Canvas {
10 
11  private int canvasWidth, canvasHeight;
12  private int x, y, width, height;
13  private double percent;
14  private long startTime;
15  private int count;
16  private boolean showBar;
17  private boolean negativeProgress;
18  private static boolean autoHide;
19 
20  private Color barColor = Color.gray;
21  private Color fillColor = new Color(204,204,255);
22  private Color backgroundColor = ij.ImageJ.backgroundColor;
23  private Color frameBrighter = backgroundColor.brighter();
24  private Color frameDarker = backgroundColor.darker();
25 
27  public ProgressBar(int canvasWidth, int canvasHeight) {
28  this.canvasWidth = canvasWidth;
29  this.canvasHeight = canvasHeight;
30  x = 3;
31  y = 5;
32  width = canvasWidth - 8;
33  height = canvasHeight - 7;
34  showBar = false;
35  negativeProgress = false;
36  count = 0;
37  percent = 0.0;
38  }
39 
40  void fill3DRect(Graphics g, int x, int y, int width, int height) {
41  g.setColor(fillColor);
42  g.fillRect(x+1, y+1, width-2, height-2);
43  g.setColor(frameDarker);
44  g.drawLine(x, y, x, y+height);
45  g.drawLine(x+1, y, x+width-1, y);
46  g.setColor(frameBrighter);
47  g.drawLine(x+1, y+height, x+width, y+height);
48  g.drawLine(x+width, y, x+width, y+height-1);
49  }
50 
55  public void show(int currentValue, int finalValue) {
56  if (currentValue>=finalValue)
57  showBar = false;
58  else {
59  percent = Math.min((currentValue+1)/(double)finalValue, 1.0);
60  showBar = true;
61  }
62  repaint();
63  }
64 
69  public void show(double percent) {
70  count++;
71  if (count==1) {
72  //ij.IJ.log("");
73  //ij.IJ.log("1st call");
74  startTime = System.currentTimeMillis();
75  showBar = false;
76  }
77  else if (count==2) {
78  long time2 = System.currentTimeMillis();
79  //ij.IJ.log("2nd call: "+(time2 - startTime) + "ms");
80  if ((time2 - startTime)>=30)
81  showBar = true;
82  }
83 
84  negativeProgress = percent<this.percent;
85  this.percent = percent;
86  if (percent>=1.0) {
87  //ij.IJ.log("total calls: "+count);
88  count = 0;
89  percent = 0.0;
90  showBar = false;
91  repaint();
92  } else if (showBar)
93  repaint();
94  }
95 
96  public void update(Graphics g) {
97  paint(g);
98  }
99 
100  public void paint(Graphics g) {
101  if (showBar) {
102  fill3DRect(g, x-1, y-1, width+1, height+1);
103  drawBar(g);
104  } else {
105  g.setColor(backgroundColor);
106  g.fillRect(0, 0, canvasWidth, canvasHeight);
107  }
108  }
109 
110  void drawBar(Graphics g) {
111  if (percent<0.0)
112  percent = 0.0;
113  int barEnd = (int)(width*percent);
114  if (negativeProgress) {
115  g.setColor(fillColor);
116  g.fillRect(barEnd+2, y, width-barEnd, height);
117  } else {
118  g.setColor(barColor);
119  g.fillRect(x, y, barEnd, height);
120  }
121  }
122 
123  public Dimension getPreferredSize() {
124  return new Dimension(canvasWidth, canvasHeight);
125  }
126 
127 }