Skip to content

Instantly share code, notes, and snippets.

@kleinlennart
Created September 30, 2017 16:22
Show Gist options
  • Save kleinlennart/dcb618dd29f31e43bf788e9b3879e904 to your computer and use it in GitHub Desktop.
Save kleinlennart/dcb618dd29f31e43bf788e9b3879e904 to your computer and use it in GitHub Desktop.
Endless Ball falling from screen animation - Graphics Paint Method
package animation;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel {
int x = 400;
int y = 0;
public boolean direction = true;
private void moveBall() {
if (y < 650)
y = y + 1;
else
y = 0;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, 40, 40);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Sample Frame");
Game game = new Game();
frame.add(game);
frame.setSize(800, 650);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.moveBall();
game.repaint();
Thread.sleep(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment