home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module10- qtzoo / completed source / progressbar.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  9.0 KB  |  237 lines

  1. import java.awt.*;
  2. import java.net.URL;
  3.  
  4. /**
  5.  * This is a simple Macintosh-like lightweight progress bar bean.
  6.  * This version does not support a disabled or background look,
  7.  * nor does it support different Appearance Manager settings.
  8.  * Please note, this is a simple example and was not written
  9.  * to use a different image, nor was it written with extensibility
  10.  * in mind.
  11.  *
  12.  * @author Levi Brown
  13.  * @author Apple Worldwide Developer Technical Support
  14.  * @author Apple Computer Inc.
  15.  * @version 1.0 10/29/1998
  16.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  17.  *    
  18.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  19.  *                ("Apple") in consideration of your agreement to the following terms, and your
  20.  *                use, installation, modification or redistribution of this Apple software
  21.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  22.  *                please do not use, install, modify or redistribute this Apple software.
  23.  *
  24.  *                In consideration of your agreement to abide by the following terms, and subject
  25.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  26.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  27.  *                reproduce, modify and redistribute the Apple Software, with or without
  28.  *                modifications, in source and/or binary forms; provided that if you redistribute
  29.  *                the Apple Software in its entirety and without modifications, you must retain
  30.  *                this notice and the following text and disclaimers in all such redistributions of
  31.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  32.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  33.  *                Apple Software without specific prior written permission from Apple.  Except as
  34.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  35.  *                are granted by Apple herein, including but not limited to any patent rights that
  36.  *                may be infringed by your derivative works or by other works in which the Apple
  37.  *                Software may be incorporated.
  38.  *
  39.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  40.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  41.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  43.  *                COMBINATION WITH YOUR PRODUCTS.
  44.  *
  45.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  46.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  47.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  49.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  50.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  51.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52.  * 
  53.  */
  54. public class ProgressBar extends BufferedDrawer
  55. {
  56.     /**
  57.      * Creates a new ProgressBar with zero percent.
  58.      */
  59.     public ProgressBar()
  60.     {
  61.         //Initialize our data members
  62.         percent                = 0;
  63.         progressImage        = Util.loadImage(progressImagePath, this, true);
  64.     }
  65.  
  66.     /**
  67.      * Sets the percentage complete this progress bar will indicate.
  68.      * @see #getPercent
  69.      */
  70.     public void setPercent(double percent)
  71.     {
  72.         if (percent < 0 || percent > 1)
  73.             throw new IllegalArgumentException("" + percent + " needs to be greater than zero and less than or equal to one.");
  74.         
  75.         this.percent = percent;
  76.         repaint();
  77.     }
  78.     
  79.     /**
  80.      * Gets the percentage complete this progress bar is currently indicating.
  81.      * @see #setPercent
  82.      */
  83.     public double getPercent()
  84.     {
  85.         return percent;
  86.     }
  87.  
  88.     /** 
  89.      * Returns the preferred size of this component.
  90.      * Overriden here to constrain height.
  91.      * @see java.awt.Component#getMinimumSize
  92.      * @see LayoutManager
  93.      */
  94.     public Dimension getPreferredSize()
  95.     {
  96.         Dimension s = getSize();
  97.         s.height = BAR_HEIGHT;
  98.         
  99.         return s;
  100.     }
  101.  
  102.     /**
  103.      * Renders the progress bar in an offscreen buffer.
  104.      * Renders the bar to indicate the current percent complete.
  105.      * @see #paint
  106.      * @see #setPercent
  107.      * @see #getPercent
  108.      */
  109.     protected void draw()
  110.     {
  111.         //Make sure we give the super a chance to do what it needs to.
  112.         super.draw();
  113.         
  114.         //Get the size of the ofscreen image.
  115.         int imageWidth    = workingImage.getWidth(this);
  116.         int imageHeight    = workingImage.getHeight(this);
  117.         //Determine the location of the percent to indicate
  118.         int progressAreaWidth = imageWidth - 4;
  119.         int progressLocation = (int)((percent * progressAreaWidth) + 2.5);
  120.                 
  121.         //Erase the contents.
  122.         workingGraphics.setColor(gray1);
  123.         workingGraphics.fillRect(0, 0, imageWidth, imageHeight);
  124.  
  125.         //Draw the progress indicator
  126.         
  127.         //If there is not enough room to show the head and tail, just draw a flat color.
  128.         if (progressLocation < 9)
  129.         {
  130.             //Draw the head (for the rightmost edge)
  131.             workingGraphics.drawImage(progressImage, progressLocation - 4, 2, (progressLocation - 4) + 6, 13,
  132.                                                      2, 0, 8, 10, this);
  133.             //Fill the area with a flat color, over the left part of the head (to obfuscate the three dimensional look). 
  134.             workingGraphics.setColor(colorE);
  135.             workingGraphics.fillRect(2, 2, progressLocation - 3, imageHeight - 4);
  136.         }
  137.         //There is enough room to draw the three dimensional look, so go ahead.
  138.         else
  139.         {
  140.             int fillStart    = 4;
  141.             int fillEnd        = progressLocation - 4;
  142.             
  143.             //Draw the fill
  144.             workingGraphics.setColor(colorA);
  145.             workingGraphics.drawLine(fillStart, 6, fillEnd, 6);
  146.             
  147.             workingGraphics.setColor(colorB);
  148.             workingGraphics.drawLine(fillStart, 5, fillEnd, 5);
  149.             workingGraphics.drawLine(fillStart, 7, fillEnd, 7);
  150.             
  151.             workingGraphics.setColor(colorC);
  152.             workingGraphics.drawLine(fillStart, 4, fillEnd, 4);
  153.             workingGraphics.drawLine(fillStart, 8, fillEnd, 8);
  154.             
  155.             workingGraphics.setColor(colorD);
  156.             workingGraphics.drawLine(fillStart, 3, fillEnd, 3);
  157.             workingGraphics.drawLine(fillStart, 9, fillEnd, 9);
  158.             
  159.             workingGraphics.setColor(colorE);
  160.             workingGraphics.drawLine(fillStart, 2, fillEnd, 2);
  161.             workingGraphics.drawLine(fillStart, 10, fillEnd, 10);
  162.             
  163.             workingGraphics.setColor(colorF);
  164.             workingGraphics.drawLine(fillStart, 11, fillEnd, 11);
  165.  
  166.             //Draw the tail
  167.             workingGraphics.drawImage(progressImage, 2, 2, 4, 13,
  168.                                                      0, 0, 2, 10, this);
  169.             
  170.             //Draw the head
  171.             workingGraphics.drawImage(progressImage, fillEnd, 2, fillEnd + 6 /*width of progress bar image*/, 13,
  172.                                                      2, 0, 8, 10, this);
  173.         }
  174.  
  175.         //Draw the border
  176.         workingGraphics.setColor(getBackground());
  177.         workingGraphics.drawLine(1, imageHeight - 1, imageWidth - 1, imageHeight - 1);
  178.         workingGraphics.drawLine(imageWidth - 1, 1, imageWidth - 1, imageHeight - 2);
  179.  
  180.         workingGraphics.setColor(gray0);
  181.         workingGraphics.drawLine(0, imageHeight - 1, 0, imageHeight - 1);
  182.         workingGraphics.drawLine(imageWidth - 1, 0, imageWidth - 1, 0);
  183.         
  184.         workingGraphics.setColor(gray2);
  185.         workingGraphics.drawLine(0, 0, 0, imageHeight - 2);
  186.         workingGraphics.drawLine(1, 0, imageWidth - 2, 0);
  187.         
  188.         workingGraphics.setColor(white);
  189.         workingGraphics.drawLine(imageWidth - 1, 1, imageWidth - 1, imageHeight - 1);
  190.         workingGraphics.drawLine(1, imageHeight - 1, imageWidth - 2, imageHeight - 1);
  191.  
  192.         workingGraphics.setColor(black);
  193.         workingGraphics.drawRect(1, 1, imageWidth - 3, imageHeight - 3);
  194.         
  195.     }
  196.  
  197.     /** 
  198.      * @deprecated As of JDK version 1.1,
  199.      * replaced by setBounds(int, int, int, int).
  200.      * Overriden here to constrain height.
  201.      */
  202.     public void reshape(int x, int y, int width, int height)
  203.     {
  204.         //Make sure to call the super's reshape method and not the
  205.         //super's setBounds method, or we end up in an infitite loop.
  206.         super.reshape(x, y, width, BAR_HEIGHT);
  207.     }
  208.  
  209.     //Hard coded colors to suppor the default Appearance Manager progress bar colors.
  210.     protected static final Color white = Color.white;
  211.     protected static final Color gray0 = new Color(222, 222, 222);
  212.     protected static final Color gray1 = new Color(189, 189, 189);
  213.     protected static final Color gray2 = new Color(173, 173, 173);
  214.     protected static final Color gray3 = new Color(140, 140, 140);
  215.     protected static final Color gray4 = new Color(82, 82, 82);
  216.     protected static final Color black = Color.black;
  217.  
  218.     protected static final Color colorA = new Color(239, 239, 239);
  219.     protected static final Color colorB = new Color(206, 206, 255);
  220.     protected static final Color colorC = new Color(156, 156, 255);
  221.     protected static final Color colorD = new Color(99, 99, 206);
  222.     protected static final Color colorE = new Color(49, 49, 156);
  223.     protected static final Color colorF = new Color(0, 0, 82);
  224.  
  225.     //Hard coded height for the progress bar.
  226.     protected static final int BAR_HEIGHT = 14;
  227.     //Relative location for the progress bar image file
  228.     protected final String progressImagePath = "Source/images/progressbar.gif";
  229.     
  230.     /**
  231.      * The current percentage the progress bar will display.
  232.      */
  233.     protected double percent;
  234.     //The loaded image representing the head and tail of the three dimensional progress bar.
  235.     protected Image progressImage;
  236. }
  237.