Effect:
Loadingpanel.java
Package com.learn.loading;
Import Java.awt.Color;
Import java.awt.Dimension;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import java.awt.RenderingHints;
Import Java.awt.Toolkit;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.font.FontRenderContext;
Import Java.awt.font.TextLayout;
Import Java.awt.geom.AffineTransform;
Import Java.awt.geom.Area;
Import Java.awt.geom.Ellipse2D;
Import Java.awt.geom.Point2D;
Import Java.awt.geom.Rectangle2D;
Import javax.swing.JComponent;
Import Javax.swing.JFrame; public class Loadingpanel extends JComponent implements MouseListener {/** * */private static F
inal long serialversionuid = 1L;
Protected area[] ticker = NULL;
protected Thread animation = null;
Protected Boolean started = false;
protected int alphalevel = 0;
protected int rampdelay = 300;
protected float shield = 0.70f;
protected String Text = ""; protected int BarsCount = 14;
protected float fps = 15.0f;
protected renderinghints hints = null;
Public Loadingpanel () {This ("");
Public Loadingpanel (String text) {This (text, 14);
Loadingpanel (String text, int barscount) {This (text, Barscount, 0.70f);
Loadingpanel (String text, int barscount, float shield) {This (text, Barscount, shield, 15.0f); Loadingpanel (String text, int barscount, float shield, float fps) {This (text, Barscount, s
Hield, FPS, 300); Public Loadingpanel (String text, int barscount, float shield, float fps, int rampdelay) {This.text = t
Ext This.rampdelay = Rampdelay >= 0?
rampdelay:0; This.shield = Shield >= 0.0f?
shield:0.0f; This.fps = fps > 0.0f?
fps:15.0f; This.barscount = barscount > 0?
barscount:14; this.hints = new RenderinghintS (renderinghints.key_rendering, renderinghints.value_render_quality);
This.hints.put (renderinghints.key_antialiasing, renderinghints.value_antialias_on);
This.hints.put (Renderinghints.key_fractionalmetrics, renderinghints.value_fractionalmetrics_on);
public void SetText (String text) {repaint ();
This.text = text;
Public String GetText () {return text;
public void Start () {Addmouselistener (this);
SetVisible (TRUE);
Ticker = Buildticker ();
Animation = new Thread (new animator (true));
Animation.start ();
public void Stop () {if (animation!= null) {animation.interrupt ();
Animation = null;
Animation = new Thread (new animator (false));
Animation.start (); } public void Interrupt () {if (animation!= null) {Animation.interrUpT ();
Animation = null;
Removemouselistener (this);
SetVisible (FALSE); @Override public void Paintcomponent (Graphics g) {if (started) {int width
= GetWidth ();
int height = getheight ();
Double Maxy = 0.0;
graphics2d g2 = (graphics2d) g;
G2.setrenderinghints (hints);
G2.setcolor (New Color (255, 255, 255, (int) (Alphalevel * shield));
G2.fillrect (0, 0, width, height);
for (int i = 0; i < ticker.length i++) {int channel = 224-128/(i + 1);
G2.setcolor (New Color (channel, channel, Channel, Alphalevel));
G2.fill (Ticker[i]);
Rectangle2D bounds = ticker[i].getbounds2d ();
if (Bounds.getmaxy () > Maxy) {maxy = Bounds.getmaxy ();
}
} if (text!= null && text.length () > 0) {fontrendercontext context = G2.getfontrenderconte
XT ();
TextLayout layout = new TextLayout (text, GetFont (), context);
Rectangle2D bounds = Layout.getbounds ();
G2.setcolor (Getforeground ()); Layout.draw (G2, (float) (Width-bounds.getwidth ())/2, (float) (Maxy + layout.getleading () + 2
* Layout.getascent ());
}} private area[] Buildticker () {area[] ticker = new Area[barscount];
Point2d.double Center = new Point2d.double (double) getwidth ()/2, (Double) getheight ()/2);
Double fixedangle = 2.0 * Math.PI/(Barscount);
for (Double i = 0.0 i < Barscount, i++) {Area primitive = buildprimitive ();
AffineTransform tocenter = affinetransform.gettranslateinstance (Center.getx (), center.gety ()); AffinEtransform Toborder = affinetransform.gettranslateinstance (45.0,-6.0);
AffineTransform tocircle = affinetransform.getrotateinstance (-i * fixedangle, Center.getx (), Center.getY ());
AffineTransform Towheel = new AffineTransform ();
Towheel.concatenate (Tocenter);
Towheel.concatenate (Toborder);
Primitive.transform (Towheel);
Primitive.transform (tocircle);
ticker[(int) i] = primitive;
return ticker;
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; } protected class Animator implements Runnable {private BoOlean Rampup = true;
Protected Animator (Boolean rampup) {this.rampup = Rampup; @Override public void Run () {point2d.double Center = new Point2d.double (Double) get
Width ()/2, (Double) getheight ()/2);
Double fixedincrement = 2.0 * Math.PI/(Barscount);
AffineTransform tocircle = affinetransform.getrotateinstance (Fixedincrement, Center.getx (), Center.getY ());
Long start = System.currenttimemillis ();
if (Rampdelay = = 0) {alphalevel = Rampup? 255:0;
} started = true;
Boolean inramp = Rampup; while (! Thread.interrupted ()) {if (!inramp) {for (int i = 0; i < ticker.length; i++)
{Ticker[i].transform (tocircle);
} repaint (); if (rampup{if (Alphalevel < 255) {alphalevel = (int) (255 * (System.currentti
Memillis ()-start)/rampdelay);
if (alphalevel >= 255) {alphalevel = 255;
Inramp = false; else if (Alphalevel > 0) {alphalevel = (int) (255
-(255 * (System.currenttimemillis ()-start)/rampdelay);
if (alphalevel <= 0) {alphalevel = 0;
Break
} try {Thread.Sleep (inramp: (int) (1000/FPS));
catch (Interruptedexception IE) {break;
} Thread.yield ();
} if (!rampup) {started = false; Repaint ();
SetVisible (FALSE);
Removemouselistener (Loadingpanel.this); @Override public void mouseclicked (MouseEvent e) {} @Override Publi
c void mousepressed (MouseEvent e) {} @Override public void mousereleased (MouseEvent e) {} @Override public void mouseentered (MouseEvent e) {} @Override public void mouseexited (Mouseeve
NT e) {} public static void Main (string[] args) {JFrame frame = new JFrame ();
// ...
Loadingpanel GlassPane = new Loadingpanel ();
Dimension Dimension = Toolkit.getdefaulttoolkit (). Getscreensize ();
Glasspane.setbounds (Dimension.width)/2, (Dimension.height)/2);
Frame.setglasspane (GlassPane);
Glasspane.settext ("Loading data, please wait ..."); Glasspane.start ()//Start animation load effect frame.setvisible(true);
Later, to disable, close the animation effect//Glasspane.stop () in the appropriate place;
}
}
When you need it to show, do it:
Thread mythread = new Thread (new Runnable () {
public void Run () {
Infiniteprogresspanel gl = thisref.glasspane;
Gl.start ();
Gl.settext (Please wait while the data is being saved ...);
try {
//Here's the thing
to do ... Gl.settext ("Save complete, Welcome to use.") ");
Thread.Sleep (1000);
} catch (Interruptedexception ex) {
}finally{
gl.stop ();
}}}
);
Mythread.start ();