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
00042 public final class ProgressBar extends Canvas {
00043 protected static final long serialVersionUID = 5521257935120563452L;
00044 private boolean showBar=false;
00045 private double percent =0;
00046
00047 private Color barColor = new Color(0, 0, 255);
00048 private static final int contourWidth=0;
00049
00050
00051 public ProgressBar() {
00052 }
00053
00058 public void updateBar(int percenttoShow) {
00059 percent = Math.min(percenttoShow/(double)100, 1.0);
00060 showBar = true;
00061 repaint();
00062 }
00063
00064 public void update(Graphics g) {
00065 paint(g);
00066 }
00067
00068 public void paint(Graphics g) {
00069 int size = this.getWidth();
00070 int height = this.getHeight();
00071 if (showBar) {
00072 g.setColor(new Color(192, 192, 192));
00073 g.fillRect(contourWidth, contourWidth, size-contourWidth, height-contourWidth);
00074 drawBar(g);
00075 } else {
00076 g.setColor(new Color(0, 0, 0));
00077 g.fillRect(0, 0, size, height);
00078 g.setColor(new Color(255, 255, 255));
00079 g.fillRect(contourWidth, contourWidth, size-contourWidth, size-contourWidth);
00080 }
00081 }
00082
00083 void drawBar(Graphics g) {
00084 int size = this.getWidth();
00085 int height = this.getHeight();
00086 if (percent<0.0)
00087 percent = 0.0;
00088 int barEnd = (int)(size*percent)-contourWidth;
00089 if (barEnd<0) barEnd=0;
00090 g.setColor(barColor);
00091 g.fillRect(contourWidth, contourWidth, barEnd, height-contourWidth);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00104 public static void main(String[] args) {
00105 try {
00106 Frame frame = new Frame();
00107 frame.setLayout(new GridLayout(2,1));
00108 frame.add(new Label("progressbar"));
00109 ProgressBar pr = new ProgressBar();
00110 frame.add(pr);
00111 frame.setSize(new Dimension(240, 100));
00112 frame.setVisible(true);
00113 Thread.currentThread().sleep(1000);
00114 for (int i=0; i<=100; i+=10){
00115 pr.updateBar(i);
00116 Thread.currentThread().sleep(1000);
00117 }
00118 frame.setVisible(false);
00119 frame.dispose();
00120 } catch (Throwable ex) {
00121 ex.printStackTrace();
00122 }
00123 }
00124
00125 }