Canvas/Button class
package dymetric;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ButtonandCanvasPanels implements ActionListener {
public JPanel button_panel, canvas_panel;
public JButton motion_bttn;
public CircularPath cyc_path;
public ButtonandCanvasPanels() {
button_panel = new JPanel();
button_panel.setBackground(Color.PINK);
button_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
motion_bttn = new JButton("Move");
motion_bttn.setBackground(new Color(255, 0, 255));
motion_bttn.addActionListener(this);
button_panel.add(motion_bttn);
canvas_panel = new JPanel();
canvas_panel.setLayout(new BorderLayout());
cyc_path = new CircularPath();
canvas_panel.add(cyc_path, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
cyc_path.moveCyclic();
}
}
Java code for CircularPath class
package dymetric;
import java.awt.*;
public class CircularPath extends Canvas {
protected Color ball_colour;
protected int a = 250;
protected int b = 165;
protected final int r = 150;
protected int x = a - r;
protected int y = b;
protected final int aWIDTH, aHEIGHT;
public CircularPath() {
setBackground(Color.LIGHT_GRAY);
ball_colour = Color.RED;
aWIDTH = aHEIGHT = 10;
}
public void paint(Graphics g) {
g.setColor(ball_colour);
g.fillOval(x, y, aWIDTH, aHEIGHT);
}
public void moveCyclic() {
while (x <= a + r) {
y = b - (int) Math.round(Math.sqrt(Math.pow(r, 2) - Math.pow((x - a), 2)));
paint(this.getGraphics());
y = b + (int) Math.round(Math.sqrt(Math.pow(r, 2) - Math.pow((x - a), 2)));
paint(this.getGraphics());
x += 20;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}