Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- @SuppressWarnings({ "serial", "unused" })
- public class Snooker extends AnimationPanel {
- private static final float BALL_SPEED = 150;
- private static final int BALL_SIZE = 20;
- private static final int MIN_X = 20;
- private static final int MIN_Y = 22;
- private static final int MAX_X = 461 - BALL_SIZE;
- private static final int MAX_Y = 248 - BALL_SIZE;
- private static final float EPSILON = 0.0001f;
- private BufferedImage table;
- private BufferedImage table2;
- private float x, y;
- private int faktor, faktor2;
- public Snooker() {
- setBackground(Color.WHITE);
- try {
- table = ImageIO.read(getClass().getResource("table.png"));
- table2 = new BufferedImage(table.getWidth(), table.getHeight(),
- BufferedImage.TYPE_INT_RGB);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.exit(-1);
- }
- }
- @Override
- public boolean step(double elapsedTime) {
- x += elapsedTime * BALL_SPEED * faktor;
- y += elapsedTime * BALL_SPEED * faktor2;
- if (x > MAX_X)
- faktor = -1;
- if (x < MIN_X)
- faktor = 1;
- if (y > MAX_Y)
- faktor2 = -1;
- if (y < MIN_Y)
- faktor2 = 1;
- return true;
- }
- @Override
- public void paintComponent(Graphics g) {
- Graphics gTable = table2.createGraphics();
- gTable.drawImage(table, 0, 0, null);
- gTable.setColor(Color.RED);
- gTable.fillOval((int) x, (int) y, BALL_SIZE, BALL_SIZE);
- int x = (getWidth() - table.getWidth()) / 2;
- int y = (getHeight() - table.getHeight()) / 2;
- g.drawImage(table2, x, y, null);
- }
- public static void main(String[] args) {
- new Snooker().run(640, 480, "Snooker", true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement