Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright (c) 2011 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
   *  All rights reserved. This program and the accompanying materials
   *  are made available under the terms of the GNU Lesser Public License v2.1
   *  which accompanies this distribution, and is available at
   *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
   *
   *  Contributors:
   *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - initial API and implementation
  */
 package de.ipk_gatersleben.bit.bi.edal.rmi.client.util;
 import java.awt.Color;
 
 
 
 public class InfiniteProgressPanel extends JComponent implements MouseListener
 {
 	private static final long serialVersionUID = 1L;
Contains the bars composing the circular shape.
 
     protected Area[]  ticker     = null;
    
The animation thread is responsible for fade in/out and rotation.
 
     protected Thread  animation  = null;
    
Notifies whether the animation is running or not.
 
     protected boolean started    = false;
    
Alpha level of the veil, used for fade in/out.
 
     protected int     alphaLevel = 0;
    
Duration of the veil's fade in/out.
 
     protected int     rampDelay  = 300;
    
Alpha level of the veil.
 
     protected float   shield     = 0.70f;
    
Message displayed below the circular shape.
 
     protected String  text       = "";
    
Amount of bars composing the circular shape.
 
     protected int     barsCount  = 14;
    
Amount of frames per seconde. Lowers this to save CPU.
 
     protected float   fps        = 15.0f;
    
Rendering hints to set anti aliasing.
 
     protected RenderingHints hints = null;
     
     private int width;
     private int height;

    
Creates a new progress panel with default values:
  • No message
  • 14 bars
  • Veil's alpha level is 70%
  • 15 frames per second
  • Fade in/out last 300 ms
 
     public InfiniteProgressPanel()
     {
         this("");
     }

    
Creates a new progress panel with default values:
  • 14 bars
  • Veil's alpha level is 70%
  • 15 frames per second
  • Fade in/out last 300 ms

Parameters:
text The message to be displayed. Can be null or empty.
 
     public InfiniteProgressPanel(String text)
     {
         this(text, 14);
     }

    
Creates a new progress panel with default values:
  • Veil's alpha level is 70%
  • 15 frames per second
  • Fade in/out last 300 ms

Parameters:
text The message to be displayed. Can be null or empty.
barsCount The amount of bars composing the circular shape
 
     public InfiniteProgressPanel(String textint barsCount)
     {
         this(textbarsCount, 0.70f);
     }

    
Creates a new progress panel with default values:
  • 15 frames per second
  • Fade in/out last 300 ms

Parameters:
text The message to be displayed. Can be null or empty.
barsCount The amount of bars composing the circular shape.
shield The alpha level between 0.0 and 1.0 of the colored shield (or veil).
    public InfiniteProgressPanel(String textint barsCountfloat shield)
    {
        this(textbarsCountshield, 15.0f);
    }

    
Creates a new progress panel with default values:
  • Fade in/out last 300 ms

Parameters:
text The message to be displayed. Can be null or empty.
barsCount The amount of bars composing the circular shape.
shield The alpha level between 0.0 and 1.0 of the colored shield (or veil).
fps The number of frames per second. Lower this value to decrease CPU usage.
    public InfiniteProgressPanel(String textint barsCountfloat shieldfloat fps)
    {
        this(textbarsCountshieldfps, 300);
    }

    
Creates a new progress panel.

Parameters:
text The message to be displayed. Can be null or empty.
barsCount The amount of bars composing the circular shape.
shield The alpha level between 0.0 and 1.0 of the colored shield (or veil).
fps The number of frames per second. Lower this value to decrease CPU usage.
rampDelay The duration, in milli seconds, of the fade in and the fade out of the veil.
    public InfiniteProgressPanel(String textint barsCountfloat shieldfloat fpsint rampDelay)
    {
        this. 	   = text;
        this. = rampDelay >= 0 ? rampDelay : 0;
        this.    = shield >= 0.0f ? shield : 0.0f;
        this.       = fps > 0.0f ? fps : 15.0f;
        this. = barsCount > 0 ? barsCount : 14;
    }
    
    
    public void setWidth(int width) {
		this. = width;
	}
	public void setHeight(int height) {
		this. = height;
	}

Changes the displayed message at runtime.

Parameters:
text The message to be displayed. Can be null or empty.
    public void setText(String text)
    {
        this. = text;
        repaint();
    }

    
Returns the current displayed message.
    public String getText()
    {
        return ;
    }

    
Starts the waiting animation by fading the veil in, then rotating the shapes. This method handles the visibility of the glass pane.
    public void start()
    {
        addMouseListener(this);
        setVisible(true);
         = buildTicker();
         = new Thread(new Animator(true));
        .start();
    }

    
Stops the waiting animation by stopping the rotation of the circular shape and then by fading out the veil. This methods sets the panel invisible at the end.
    public void stop()
    {
        if ( != null) {
	        .interrupt();
	         = null;
	         = new Thread(new Animator(false));
	        .start();
        }
    }
    
    
Interrupts the animation, whatever its state is. You can use it when you need to stop the animation without running the fade out phase. This methods sets the panel invisible at the end.
    public void interrupt()
    {
        if ( != null) {
            .interrupt();
             = null;
            removeMouseListener(this);
            setVisible(false);
        }
    }
    public void paintComponent(Graphics g)
    {
        if ()
        {
            int width  = getWidth();
            double maxY = 0.0; 
            Graphics2D g2 = (Graphics2Dg;
            g2.setRenderingHints();
            
            g2.setColor(new Color(255, 255, 255, (int) ( * )));
            g2.fillRect(0, 0, getWidth(), getHeight());
            for (int i = 0; i < .i++)
            {
                int channel = 224 - 128 / (i + 1);
                g2.setColor(new Color(channelchannelchannel));
                g2.fill([i]);
                Rectangle2D bounds = [i].getBounds2D();
                if (bounds.getMaxY() > maxY)
                    maxY = bounds.getMaxY();
            }
            if ( != null && .length() > 0)
            {
	            FontRenderContext context = g2.getFontRenderContext();
	            TextLayout layout = new TextLayout(getFont(), context);
	            Rectangle2D bounds = layout.getBounds();
	            g2.setColor(getForeground());
	            layout.draw(g2, (float) (width - bounds.getWidth()) / 2,
	                    		(float) (maxY + layout.getLeading() + 2 * layout.getAscent()));
            }
        }
    }

    
Builds the circular shape and returns the result as an array of Area. Each Area is one of the bars composing the shape.
    private Area[] buildTicker()
    {
        Area[] ticker = new Area[];
        Point2D.Double center = new Point2D.Double( / 2,  / 2);
        double fixedAngle = 2.0 * . / ((double);
        for (double i = 0.0; i < (doublei++)
        {
            Area primitive = buildPrimitive();
            AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(), center.getY());
            AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0);
            AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAnglecenter.getX(), center.getY());
            AffineTransform toWheel = new AffineTransform();
            toWheel.concatenate(toCenter);
            toWheel.concatenate(toBorder);
            primitive.transform(toWheel);
            primitive.transform(toCircle);
            
            ticker[(inti] = primitive;
        }
        return ticker;
    }

    
Builds a bar.
    private Area buildPrimitive()
    {
        Rectangle2D.Double body = new Rectangle2D.Double(6, 0, 30, 12);
        Ellipse2D.Double   head = new Ellipse2D.Double(0, 0, 12, 12);
        Ellipse2D.Double   tail = new Ellipse2D.Double(30, 0, 12, 12);
        Area tick = new Area(body);
        tick.add(new Area(head));
        tick.add(new Area(tail));
        return tick;
    }

    
Animation thread.
    private class Animator implements Runnable
    {
        private boolean rampUp = true;
        protected Animator(boolean rampUp)
        {
            this. = rampUp;
        }
        public void run()
        {
            Point2D.Double center = new Point2D.Double( / 2,  / 2);
            double fixedIncrement = 2.0 * . / ((double);
            AffineTransform toCircle = AffineTransform.getRotateInstance(fixedIncrementcenter.getX(), center.getY());
    
            long start = System.currentTimeMillis();
            if ( == 0)
                 =  ? 255 : 0;
             = true;
            boolean inRamp = ;
            while (!Thread.interrupted())
            {
                if (!inRamp)
                {
                    for (int i = 0; i < .i++)
                        [i].transform(toCircle);
                }
                repaint();
                if ()
                {
                    if ( < 255)
                    {
                         = (int) (255 * (System.currentTimeMillis() - start) / );
                        if ( >= 255)
                        {
                             = 255;
                            inRamp = false;
                        }
                    }
                } else if ( > 0) {
                     = (int) (255 - (255 * (System.currentTimeMillis() - start) / ));
                    if ( <= 0)
                    {
                         = 0;
                        break;
                    }
                }
                try
                {
                    Thread.sleep(inRamp ? 10 : (int) (1000 / ));
                } catch (InterruptedException ie) {
                    break;
                }
                Thread.yield();
            }
            if (!)
            {
                 = false;
                repaint();
                setVisible(false);
                removeMouseListener(InfiniteProgressPanel.this);
            }
        }
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
New to GrepCode? Check out our FAQ X