00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.openmobileis.modules.common.gui;
00029
00030 import java.awt.Canvas;
00031 import java.awt.Color;
00032 import java.awt.Dimension;
00033 import java.awt.Frame;
00034 import java.awt.Graphics;
00035 import java.awt.GridLayout;
00036 import java.awt.Label;
00037
00038 import org.openmobileis.common.util.log.LogManager;
00039
00044 public final class ProgressBar extends Canvas {
00045 protected static final long serialVersionUID = 5521257935120563452L;
00046 private boolean showBar=false;
00047 private double percent =0;
00048
00049 private Color barColor = new Color(0, 0, 255);
00050 private static final int contourWidth=0;
00051
00052
00053 public ProgressBar() {
00054 }
00055
00060 public void updateBar(int percenttoShow) {
00061 percent = Math.min(percenttoShow/(double)100, 1.0);
00062 showBar = true;
00063 repaint();
00064 }
00065
00066 public void update(Graphics g) {
00067 paint(g);
00068 }
00069
00070 public void paint(Graphics g) {
00071 int size = this.getWidth();
00072 int height = this.getHeight();
00073
00074 if (showBar) {
00075 g.setColor(new Color(192, 192, 192));
00076 g.fillRect(contourWidth, contourWidth, size-contourWidth, height-contourWidth);
00077 drawBar(g);
00078 } else {
00079 g.setColor(new Color(0, 0, 0));
00080 g.fillRect(0, 0, size, height);
00081 g.setColor(new Color(255, 255, 255));
00082 g.fillRect(contourWidth, contourWidth, size-contourWidth, size-contourWidth);
00083 }
00084 }
00085
00086 void drawBar(Graphics g) {
00087 int size = this.getWidth();
00088 int height = this.getHeight();
00089 if (percent<0.0)
00090 percent = 0.0;
00091 int barEnd = (int)(size*percent)-contourWidth;
00092 if (barEnd<0) barEnd=0;
00093 g.setColor(barColor);
00094 g.fillRect(contourWidth, contourWidth, barEnd, height-contourWidth);
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00107 public static void main(String[] args) {
00108 try {
00109 Frame frame = new Frame();
00110 frame.setLayout(new GridLayout(2,1));
00111 frame.add(new Label("progressbar"));
00112 ProgressBar pr = new ProgressBar();
00113 frame.add(pr);
00114 frame.setSize(new Dimension(240, 100));
00115 frame.setVisible(true);
00116 Thread.currentThread().sleep(1000);
00117 for (int i=0; i<=100; i+=10){
00118 pr.updateBar(i);
00119 Thread.currentThread().sleep(1000);
00120 }
00121 frame.setVisible(false);
00122 frame.dispose();
00123 } catch (Throwable ex) {
00124 ex.printStackTrace();
00125 }
00126 }
00127
00128 }