0

I am currently creating a small project, using ACM graphics library, where you will have to move the ball (or an object) of GOval using method movePolar to move in circular motion. So far I know that there are two arguments in movePolar() where the first r argument is the distance you want to move and the second argument is the angle that you want to move in. But I couldn't figure out how to make the ball move in circular motion using movePolar() method. I have tried using multiple movePolar(1,90), movePolar(1,45), etc. and still doesn't got the goal I want. Here is my code:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import acm.graphics.*;
import acm.program.*;

public class ColorCircleDecomp extends GraphicsProgram implements ActionListener {
    public static final int PROGRAM_WIDTH = 800;
    public static final int PROGRAM_HEIGHT = 600;
    public static final int BALL_SIZE = 50;
    public static final int DELAY_MS = 25;
    private GOval ball;
    //TODO create a private GOval here

    public void run() {
        //TODO add your ball here

        Timer t = new Timer(DELAY_MS, this);
         ball = new GOval(300,300,BALL_SIZE,BALL_SIZE);
         add(ball);
        t.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

     ball.movePolar(1,90);
     ball.movePolar(1,0);
     ball.movePolar(1, 270);
     ball.movePolar(1, 180);

    }

    public void init() {
        setSize(PROGRAM_WIDTH, PROGRAM_HEIGHT);
    }

}
Abra
  • 19,142
  • 7
  • 29
  • 41

1 Answers1

0
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import acm.graphics.*;
import acm.program.*;

public class ColorCircleDecomp extends GraphicsProgram implements ActionListener {
public static final int PROGRAM_WIDTH = 800;
public static final int PROGRAM_HEIGHT = 600;
public static final int BALL_SIZE = 50;
public static final int DELAY_MS = 25;
int i = 0;

private GOval ball;

public void run() {
    ball = new GOval (370, 540, BALL_SIZE, BALL_SIZE);
    Timer t = new Timer(DELAY_MS, this);
    t.start();
}

@Override
public void actionPerformed(ActionEvent e) {
    Color color = new Color(0, 0, 255);

    add(ball);

        i+=30;

    ball.setColor(color);
    ball.movePolar(130, i);

}

public void init() {
    setSize(PROGRAM_WIDTH, PROGRAM_HEIGHT);
}

}