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 
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); //blue
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 //      LogManager.traceDebug(0, "progressbarr height :"+height+" width:"+size);
00074     if (showBar) {
00075         g.setColor(new Color(192, 192, 192)); //Light gray
00076         g.fillRect(contourWidth, contourWidth, size-contourWidth, height-contourWidth);
00077         drawBar(g);
00078     } else {
00079         g.setColor(new Color(0, 0, 0)); //black
00080         g.fillRect(0, 0, size, height);
00081         g.setColor(new Color(255, 255, 255)); //WHITE
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 /*public Dimension getPreferredSize() {
00098         int size = this.getWidth();
00099     return new Dimension(size, size);
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 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4