itro
itro

Reputation: 7228

How to create rotating circle animation in java?

I want to create a rotating circle in the middle of JPanel and be able to set start/stop rotating and also set to visible/invisible.

Does any body know how to do it or any existing example?

private static class RotatingCirclePanel extends JPanel {        
    protected void paintComponent(Graphics g) {

    }                      
}

This is an example of it.

enter image description here

Edited: This is first version.Can any body help to make it as in gif example?

public class ProgressWheelPanel extends JPanel {
private double angleInDegrees = 1;
private Timer rotatingTimer;

public ProgressWheelPanel() {
    rotatingTimer = new Timer(100, new ActionListener() {
        //            @Override
        public void actionPerformed(ActionEvent e) {
            angleInDegrees = angleInDegrees +1;
            if (angleInDegrees == 360) {
                angleInDegrees = 0;
            }
            repaint();
        }
    });
    rotatingTimer.setRepeats(false);
    rotatingTimer.start();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    rotatingTimer.stop();

    g2d.clearRect(0, 0, getWidth(), getHeight());
    g2d.setBackground(Color.white);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
    g2d.rotate(angleInDegrees * Math.PI / 180.0, getWidth() / 2, getHeight() / 2);
    g2d.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2 + 100, getHeight() / 2);
    //**************************************************************************************
    AffineTransform transformer = new AffineTransform();
    transformer.translate(5,5);
    transformer.scale(2,2);
    g2d.getTransform().concatenate(transformer);
    //***************************************************************************************
    g2d.dispose();
    rotatingTimer.start();
}
public void start(){
    rotatingTimer.start();
}
public void stop(){
  rotatingTimer.stop();
}

public static void main(String[] args) {
    final ProgressWheelPanel demo = new ProgressWheelPanel();
    WebButton btnStrat= new WebButton("Start"),btnStop= new WebButton("Stop");
    btnStrat.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            demo.setVisible(true);
            demo.start();
        }
    });
    btnStop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            demo.stop();
            demo.setVisible(false);
        }
    });
    JFrame frame = new JFrame();
    Container cp = frame.getContentPane();
    cp.add(demo,BorderLayout.CENTER);
    cp.add(btnStrat,BorderLayout.NORTH);
    cp.add(btnStop,BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(500, 500);
    frame.setVisible(true);
}
}  

Upvotes: 3

Views: 8198

Answers (2)

Robin
Robin

Reputation: 36611

This answer contains a code snippet to create such an icon based on an image. It basically uses code to rotate an icon, and a Timer which triggers the rotation to get the animated effect.

Upvotes: 3

Tomas Aschan
Tomas Aschan

Reputation: 60584

You could simply display an animated GIF on either a JFrame or a JPanel*, using for example the image displayed in your question, and then set the visible/invisible properties of the control containing the image. However, this doesn't give you a way (that I know of) to start/stop the animation at any given point in time.

If you want full functionality including start/stop, you'll probably have to roll your own. In that case, start coding! We'll gladly help you when you get run into problems, but we won't write the code for you. (But before you start working on this, ask yourself: do you really need, I mean really need, the start/stop functionality? If you can make do with show/hide, you're already home...)


*) There are many other resources on animated gifs that might come in handy.

Upvotes: 3

Related Questions