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 EllipticalPath elp_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());
elp_path = new EllipticalPath();
canvas_panel.add(elp_path, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
elp_path.moveElliptic();
}
}
Java code for EllipticalPath class
package dymetric;
import java.awt.*;
public class EllipticalPath extends Canvas {
protected Color ball_colour;
protected int h = 250;
protected int k = 175;
protected int a = 150;
protected int b = 100;
protected int x = h - a;
protected int y = k;
protected final int aWIDTH, aHEIGHT;
public EllipticalPath() {
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 moveElliptic() {
while (x <= h + a) {
y = (int)Math.round(k - ((double)b / a) * Math.sqrt(Math.pow(a, 2) - Math.pow((x - h), 2)));
paint(this.getGraphics());
y = (int)Math.round(k + ((double)b / a) * Math.sqrt(Math.pow(a, 2) - Math.pow((x - h), 2)));
paint(this.getGraphics());
x += 20;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}