Due Date: Friday September 26, 2023
Below is the code you will need to code
to a google document.
Then copy it to your Ecliplse
or the online java compiler you can find.
package animate; // This may or may not be needed.
// you may need to change animate to your last name.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CusackSimpleAnimation extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private int x = 0;
private static final int Y = 100;
private static final int DIAMETER = 50;
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 200;
public CusackSimpleAnimation() {
initUI();
}
private void initUI() {
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircle(g);
}
};
add(panel);
setTitle("Cusack Simple Animation");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Timer timer = new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
x += 1;
if (x > getWidth()) {
x = -DIAMETER;
}
repaint();
}
});
timer.start();
}
private void drawCircle(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x, Y, DIAMETER, DIAMETER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CusackSimpleAnimation ex = new CusackSimpleAnimation();
ex.setVisible(true);
}
});
}
}