Jonathan
Jonathan

Reputation: 535

Using threads on animations in Java

I have created a class whit two shapes namely two ovals. Here I draw them.

import ...;

public class Drawings extends JPanel{
    public double degrees = 0;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int xcen =  getWidth() / 2;
        int ycen =  getHeight()/ 2;

        int radius = 10;
        degrees++;

        double radians = Math.toRadians(degrees);
        int posx = (int)(100.getDistance() * Math.cos(radians));
        int posy = (int)(100.getDistance() * Math.sin(radians));

        g.setColor(Color.BLUE);
        g.FillOval(xcen + posx, ycen + posy, 20, 20);

        g.setColor(Color.GREEN);
        g.drawOval(xcen + posx, ycen + posy, 100,100)

   }
} 

Now I implement it in a main.

import ....;

public class Animate extends JFrame{
  public static void main(String [] args)
  {

   JFrame window = new JFrame();
   window.add(new Drawings());
   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   window.setSize(500,500);
   window.setLocationRelativeTo(null); 
   window.setVisible(true);

   //now I implement the thread to animate the two shapes
   Thread paintThread = new Thread(new Runnable(){
   @Override
        public void run(){
              while(true)
              {
                   window.repaint();
                   try{
                        Thread.sleep(25);//determines how slow the ovals will move
                   }catch(InterruptedException e){
                       e.printStackTrace();
                   }
                 }
               }
             });

              paintThread.start();//start the animation

            }  
         }

When the program runs the two Ovals rotate on the screen. But the two ovals rotates at the same speed as I would expect but I would like the two ovals to move at diffident speeds.

I have tried using a method to move them at different speed but with no success. How would I get the two ovals moving at different speeds?

Upvotes: 0

Views: 3825

Answers (2)

andi
andi

Reputation: 240

The easiest way is:

import ...;

public class Drawings extends JPanel{
public double degrees = 0;

private int firstOvalSpeed;
private int secondOvalSpeed;

public void paintComponent(Graphics g){
    super.paintComponent(g);
    int xcen =  getWidth() / 2;
    int ycen =  getHeight()/ 2;

    int radius = 10;
    degrees++;

    double radians = Math.toRadians(degrees);
    int posx = (int)(100.getDistance() * Math.cos(radians));
    int posy = (int)(100.getDistance() * Math.sin(radians));

    g.setColor(Color.BLUE);
    g.FillOval(xcen + posx*firstOvalSpeed, ycen + posy*firstOvalSpeed, 20, 20);

    g.setColor(Color.GREEN);
    g.drawOval(xcen + posx*secondOvalSpeed, ycen + posy*secondOvalSpeed, 100,100)

 }
public void setFirstOvalSpeed(int firstOvalSpeed) {
    this.firstOvalSpeed = firstOvalSpeed;
}

public void setSecondOvalSpeed(int secondOvalSpeed) {
    this.secondOvalSpeed = secondOvalSpeed;
}

} 




public class Animate extends JFrame {
public static void main(String[] args) {

    final JFrame window = new JFrame();
    Drawings drawings = new Drawings();
    drawings.setFirstOvalSpeed(1);
    drawings.setSecondOvalSpeed(2);

    window.add(drawings);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(500, 500);
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    // now I implement the thread to animate the two shapes
    Thread paintThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                window.repaint();
                try {
                    Thread.sleep(25);// determines how slow the ovals will
                                        // move
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    paintThread.start();// start the animation

}

}

Upvotes: 0

Theodore Norvell
Theodore Norvell

Reputation: 16221

Make a class to represent an oval. Make two instances. Give the two instances different angular velocities. Currently because you increment degrees by 1.0 every 25 ms you have an angular velocity fixed at 40 degrees per second. If each oval has its own degrees field and you increment the two by different amounts, the ovals will rotate at different rates.

Upvotes: 1

Related Questions