How to make a java shape animate in a circle?

user5531310

I'm trying to animate a square moving in a circle within the canvas. I have gotten a square to move back and forth within the frame but I'm having some difficulty laying out the circular motion. I created a variable theta which changes of a swing timer, therefore altering the overall position of the shape. However when I run it nothing happens. I'm also not sure if I should be using doubles or ints since the drawRectangle command only accepts ints but the math is complex enough to require double values. Here's what I have so far:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Circle extends JPanel implements ActionListener{

Timer timer = new Timer(5, this);
Double theta= new Double(0);
Double x = new Double(200+(50*(Math.cos(theta))));
Double y = new Double(200+(50*(Math.sin(theta))));
Double change = new Double(0.1);
int xp = x.intValue();
int yp = y.intValue();

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillRect(xp, yp, 50, 50);
    timer.start();
}

public void actionPerformed(ActionEvent e) {

    theta=theta+change;
    repaint();
}

public static void main(String[] args){
    Circle a = new Circle();
    JFrame frame = new JFrame("Circleg");
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(a);

}

}

camickr
theta=theta+change;
repaint();

You don't change the xp, yp values. They will not magically update themselves just because the theta value changes.

Move the code calculating the x/y location right into the paintComponent() method.

Double x = new Double(200+(50*(Math.cos(theta))));
Double y = new Double(200+(50*(Math.sin(theta))));
int xp = x.intValue();
int yp = y.intValue();
g.fillRect(xp, yp, 50, 50);

Also,

timer.start();

Do NOT start the Timer in the painting method. Painting methods are for painting only.

The Timer should be started in the constructor of your class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS/CSS: how to animate a circle to pill shape?

From Dev

iOS animate/morph shape from circle to square

From Dev

fabric.js : How to make an image in the shape of a circle with white borders

From Dev

How to make an image inside a diamond shape morph to circle

From Dev

How to make a javafx button with circle shape of 3xp diameter?

From Dev

how to animate a circle

From Dev

How to draw a ring shape in Java using unit circle trig

From Dev

How to make Circle image Label in Java?

From Dev

How to animate a View around a circle?

From Dev

How to animate a circle using bokeh

From Dev

How to animate two divs in html / css to make a semi-circle transition?

From Dev

Want to make 3 circle same as shown in the figure, but only one stoke is not working in the circle shape xml, how do I figure it out?

From Dev

How to make a flickering circle?

From Dev

How to make circle clickable

From Dev

How to animate a circle to fill instead of using .trim?

From Dev

How to add stroke around whole circle and animate to rotate circle in svg?

From Dev

How to make this shape in html?

From Dev

How to make this shape in html?

From Dev

How to make this shape in CSS?

From Dev

how to make this shape for title?

From Dev

How to crop image in a circle shape in Titanium?

From Dev

How to turn images into circle shape? Bootstrap 3

From Dev

How to turn images into circle shape? Bootstrap 3

From Dev

How to create a circle shape within UITableViewCell?

From Dev

How to draw multiple circle shape in SFML

From Dev

How to create a circle shape without solid polygon or non solid circle``

From Dev

Java how to make a shape(s) that can be draw by clicking button

From Dev

What shape would this make in Java?

From Dev

How to make UILabel in Swift a circle

Related Related

  1. 1

    JS/CSS: how to animate a circle to pill shape?

  2. 2

    iOS animate/morph shape from circle to square

  3. 3

    fabric.js : How to make an image in the shape of a circle with white borders

  4. 4

    How to make an image inside a diamond shape morph to circle

  5. 5

    How to make a javafx button with circle shape of 3xp diameter?

  6. 6

    how to animate a circle

  7. 7

    How to draw a ring shape in Java using unit circle trig

  8. 8

    How to make Circle image Label in Java?

  9. 9

    How to animate a View around a circle?

  10. 10

    How to animate a circle using bokeh

  11. 11

    How to animate two divs in html / css to make a semi-circle transition?

  12. 12

    Want to make 3 circle same as shown in the figure, but only one stoke is not working in the circle shape xml, how do I figure it out?

  13. 13

    How to make a flickering circle?

  14. 14

    How to make circle clickable

  15. 15

    How to animate a circle to fill instead of using .trim?

  16. 16

    How to add stroke around whole circle and animate to rotate circle in svg?

  17. 17

    How to make this shape in html?

  18. 18

    How to make this shape in html?

  19. 19

    How to make this shape in CSS?

  20. 20

    how to make this shape for title?

  21. 21

    How to crop image in a circle shape in Titanium?

  22. 22

    How to turn images into circle shape? Bootstrap 3

  23. 23

    How to turn images into circle shape? Bootstrap 3

  24. 24

    How to create a circle shape within UITableViewCell?

  25. 25

    How to draw multiple circle shape in SFML

  26. 26

    How to create a circle shape without solid polygon or non solid circle``

  27. 27

    Java how to make a shape(s) that can be draw by clicking button

  28. 28

    What shape would this make in Java?

  29. 29

    How to make UILabel in Swift a circle

HotTag

Archive