WoLv3rine
WoLv3rine

Reputation: 107

How to grow/shrink a circle from/to a point in JAVA?

I initially start with a circle and when the start button is pressed the circle must shrink to a point and if the stop button is pressed during that process, it should stop the shrink process. If the start button is then pressed it must continue shrinking..

Once it has shrunk to a point, it must grow back to a circle and the buttons must respond similarly when clicked.

I am not sure on how to implement the Grow() and Shrink() using action performed in JAVA.

Upvotes: 0

Views: 3480

Answers (1)

n00begon
n00begon

Reputation: 3492

If you are using Swing in you can draw your circles as if they were a box. This means you can just change the size and draw them with an offset to the centre stays in the same place:

public Circle {

    private int x;
    private int y;
    private int size;

    public void paint(Graphics g) {
        g.drawOval(x + size/2, y + size/2, size, size);
    }

    public void grow(){
        size++;
    }

    public void shrink(){
        size--;
        size = Math.max(size,1);
    }

}

Upvotes: 1

Related Questions