SPINNER OR A LOADER ANIMATION IN JAVA-APPLET

SPINNER OR A LOADER ANIMATION IN JAVA-APPLET

Hi everyone,

I believe in this quote!!

When you improve a little thing each day,eventually big things occur.

Today we gonna see about how to create a Spinner in Java with the help of Applet.We have seen it in Android,Python,JQuery and in many.Finally we gonna make it in Java(Applet).

Step 1:

Create a class that extends JFrame.Make the frame visibility true.

Step 2:

Create a create a class that extends JPanel.Inside the panel only we gonna create a graphics method to generate a spinner.

Add the below coding in panel part.

import javax.swing.JPanel;

import java.awt.Color;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Paint;

import java.awt.Point;

import java.awt.RenderingHints;

import java.awt.geom.Arc2D;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Point2D;

import java.awt.geom.Rectangle2D;

 

public class panel extends JPanel{

int value=0;

public void UpdateSpinner(int pvalue)

{

value=pvalue;

}

@Override

public void paint(Graphics g)

{

super.paint(g);

Graphics2D g2=(Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

g2.translate(this.getWidth()/2,this.getHeight()/2);

g2.rotate(Math.toRadians(270));

Arc2D.Float arc =new Arc2D.Float(Arc2D.PIE);

Ellipse2D circle=new Ellipse2D.Float(0,0,110,110);

arc.setFrameFromCenter(new Point(0,0), new Point(120,120));

circle.setFrameFromCenter(new Point(0,0), new Point(110,110));

arc.setAngleStart(1);

arc.setAngleExtent(-value*3.6);

g2.setColor(Color.red);

g2.draw(arc);

g2.fill(arc);

g2.setColor(Color.WHITE);

g2.draw(circle);

g2.fill(circle);

g2.setColor(Color.red);

g2.rotate(Math.toRadians(90));

g.setFont(new Font(“verdana”,Font.PLAIN,50));

FontMetrics fm=g2.getFontMetrics();

Rectangle2D r=fm.getStringBounds(value+”%”, g);

int x=(0-(int)r.getWidth())/2;

int y=(0-(int)r.getHeight())/2+fm.getAscent();

g2.drawString(value+”%”,x,y);

 

}

}

Here I have made a loop to run from 0 to 100.Add the below coding for the actionPerformed in button.

public void bt_runActionPerformed(ActionEvent evt) {

new Thread(new Runnable() {

@Override

public void run() {

for(int num=1;num<=100;num++)

{

try {

jspinner.UpdateSpinner(num);

jspinner.repaint();

Thread.sleep(50);

} catch (InterruptedException ex) {

Logger.getLogger(frame.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}).start();

}

If you want you can make it infinite.It depends upon our requirement.

Thank you all, for any queries please do comment.

Proud to be a Xplorer.

 

0 responses on "SPINNER OR A LOADER ANIMATION IN JAVA-APPLET"

Leave a Message

Your email address will not be published. Required fields are marked *

© Xplore IT Corp 2017. All rights reserved. Designed by ProPlus Logics
Xplore WhatsApp Chat
Send via WhatsApp
X