Skip to content

Instantly share code, notes, and snippets.

@pteacher
Created October 25, 2017 13:50
Show Gist options
  • Save pteacher/b8bb5a5aca40918d2ba7a2d7d94bf326 to your computer and use it in GitHub Desktop.
Save pteacher/b8bb5a5aca40918d2ba7a2d7d94bf326 to your computer and use it in GitHub Desktop.
Java graphics and animation
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Draw extends JPanel {
static int x = 0;
static int delay = 10;
static int speed = 5;
public static void main(String[] args) {
JFrame frame = new JFrame("Project");
frame.setSize(600, 400);
frame.add(new Draw());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x += speed;
if (x > 600) {
x = - 100;
}
frame.repaint();
}
};
new Timer(delay, taskPerformer).start();
}
public void paint (Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 400);
g.setColor(Color.ORANGE);
g.fillOval(x, 100, 100, 100);
}
public void go() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment