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 PeriodicFunction pf_track;
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());
pf_track = new PeriodicFunction();
canvas_panel.add(pf_track, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
pf_track.moveSinusoidal();
}
}
Java code for PeriodicFunction class
package dymetric;
import java.awt.*;
public class PeriodicFunction extends Canvas {
protected Color ball_colour;
protected int theta = 0;
protected int a = 100;
protected int y;
protected final int aWIDTH, aHEIGHT;
public PeriodicFunction() {
setBackground(Color.LIGHT_GRAY);
ball_colour = Color.RED;
aWIDTH = aHEIGHT = 10;
y = (int) Math.round(a * Math.sin(theta * (double) Math.PI / 180));
}
public void paint(Graphics g) {
g.translate(0, 150);
g.setColor(ball_colour);
g.fillOval(theta, y, aWIDTH, aHEIGHT);
}
public void moveSinusoidal() {
while (theta < 750) {
y = (int) Math.round(a * Math.sin(theta * (double) Math.PI / 180));
paint(this.getGraphics());
theta += 15;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}