Advertisement
ridjis

Snooker

Apr 16th, 2015
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.image.BufferedImage;
  4. import javax.imageio.ImageIO;
  5.  
  6. @SuppressWarnings({ "serial", "unused" })
  7. public class Snooker extends AnimationPanel {
  8.     private static final float BALL_SPEED = 150;
  9.     private static final int BALL_SIZE = 20;
  10.     private static final int MIN_X = 20;
  11.     private static final int MIN_Y = 22;
  12.     private static final int MAX_X = 461 - BALL_SIZE;
  13.     private static final int MAX_Y = 248 - BALL_SIZE;
  14.     private static final float EPSILON = 0.0001f;
  15.     private BufferedImage table;
  16.     private BufferedImage table2;
  17.     private float x, y;
  18.     private int faktor, faktor2;
  19.  
  20.     public Snooker() {
  21.         setBackground(Color.WHITE);
  22.         try {
  23.             table = ImageIO.read(getClass().getResource("table.png"));
  24.             table2 = new BufferedImage(table.getWidth(), table.getHeight(),
  25.                     BufferedImage.TYPE_INT_RGB);
  26.         } catch (Exception ex) {
  27.             ex.printStackTrace();
  28.             System.exit(-1);
  29.         }
  30.     }
  31.  
  32.     @Override
  33.     public boolean step(double elapsedTime) {
  34.         x += elapsedTime * BALL_SPEED * faktor;
  35.         y += elapsedTime * BALL_SPEED * faktor2;
  36.        
  37.         if (x > MAX_X)
  38.             faktor = -1;
  39.         if (x < MIN_X)
  40.             faktor = 1;
  41.        
  42.         if (y > MAX_Y)
  43.             faktor2 = -1;
  44.         if (y < MIN_Y)
  45.             faktor2 = 1;
  46.         return true;
  47.     }
  48.  
  49.     @Override
  50.     public void paintComponent(Graphics g) {
  51.         Graphics gTable = table2.createGraphics();
  52.         gTable.drawImage(table, 0, 0, null);
  53.         gTable.setColor(Color.RED);
  54.         gTable.fillOval((int) x, (int) y, BALL_SIZE, BALL_SIZE);
  55.         int x = (getWidth() - table.getWidth()) / 2;
  56.         int y = (getHeight() - table.getHeight()) / 2;
  57.         g.drawImage(table2, x, y, null);
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.         new Snooker().run(640, 480, "Snooker", true);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement