ProgressBar.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *
00024  *  Modifications :
00025  *  Creation P.Delrieu
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); //blue
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)); //Light gray
00073         g.fillRect(contourWidth, contourWidth, size-contourWidth, height-contourWidth);
00074         drawBar(g);
00075     } else {
00076         g.setColor(new Color(0, 0, 0)); //black
00077         g.fillRect(0, 0, size, height);
00078         g.setColor(new Color(255, 255, 255)); //WHITE
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 /*public Dimension getPreferredSize() {
00095         int size = this.getWidth();
00096     return new Dimension(size, size);
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 }

Generated on Mon Jan 14 17:29:49 2008 for OpenMobileIS by  doxygen 1.5.4