Advertisement
Guest User

Untitled

a guest
May 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package il.co.tomer.game;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Point;
  6. import java.awt.RenderingHints;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.event.MouseMotionListener;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14.  
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17.  
  18.  
  19. public class Game extends JPanel implements MouseMotionListener{
  20.  
  21.     private static final long serialVersionUID = 1L;
  22.  
  23.     public static final int FPS = 60;
  24.     public static final int WINDOW_LEFT_BORDER_SIZE = 8;
  25.     public static final int WINDOW_TOP_BORDER_SIZE = 20;
  26.    
  27.     private Point target;
  28.     private float ballHorizontalLocation = 50f;
  29.     private float ballVerticalLocation = 50f;
  30.     private int ballWidth = 150;
  31.     private int ballHeight = 150;
  32.  
  33.     public static void main(String[] args) // first function to be called
  34.     {
  35.         new Game();
  36.     }
  37.    
  38.     private Game() // constructor for the Game object (also our JPanel and MouseMotionListener)
  39.     {
  40.         initialize(); //initialize our game - set the window settings and add listeners
  41.         startGame(); // start our game loops
  42.     }
  43.    
  44.     private void initialize()
  45.     {
  46.         JFrame jf = new JFrame("Tomer and Gal's Game");
  47.         jf.setVisible(true);
  48.         jf.setSize(800, 600);
  49.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         jf.setLocationRelativeTo(null);
  51.         jf.add(this);
  52.         jf.addMouseMotionListener(this);
  53.     }
  54.    
  55.     private void startGame()
  56.     {
  57.         new Timer().scheduleAtFixedRate(new TimerTask(){
  58.  
  59.             @Override
  60.             public void run() {
  61.                                
  62.                 updateGame(); //update game 60 times a second
  63.                 drawGame(); //draw game 60 times a second
  64.                
  65.             }}, 0, 1000 / FPS); // 60 FPS  
  66.     }
  67.    
  68.     private void drawGame()
  69.     {
  70.         repaint();
  71.     }
  72.    
  73.     private void updateGame()
  74.     {
  75.         if (target != null)
  76.         {  
  77.             if (this.ballHorizontalLocation < target.getX()) // if the ball is left to the mouse
  78.             {
  79.                 this.ballHorizontalLocation += 1; // minimize the gap
  80.             }
  81.            
  82.             if (this.ballHorizontalLocation > target.getX()) // if the ball is right to the mouse
  83.             {
  84.                 this.ballHorizontalLocation -= 1; // minimize the gap
  85.             }
  86.            
  87.             if (this.ballVerticalLocation < target.getY()) // if the ball is above the mouse
  88.             {
  89.                 this.ballVerticalLocation += 1; // minimize the gap
  90.             }
  91.            
  92.             if (this.ballVerticalLocation > target.getY()) // if the ball is below the mouse
  93.             {
  94.                 this.ballVerticalLocation -= 1; // minimize the gap
  95.             }
  96.         }
  97.     }
  98.    
  99.     public void paintComponent(Graphics g)
  100.     {
  101.         //set ANTIALIASING
  102.         Graphics2D g2 = (Graphics2D)g;
  103.             RenderingHints rh = new RenderingHints(
  104.                      RenderingHints.KEY_ANTIALIASING,
  105.                      RenderingHints.VALUE_ANTIALIAS_ON);
  106.             g2.setRenderingHints(rh);  
  107.        
  108.         //draw pink background
  109.         g2.setColor(Color.PINK);
  110.         g2.fillRect(0, 0, 800, 600);
  111.        
  112.         //draw our ball
  113.         g2.setColor(Color.WHITE);
  114.         g2.fillOval((int)ballHorizontalLocation, (int)ballVerticalLocation, ballWidth, ballHeight);
  115.     }
  116.  
  117.     @Override
  118.     public void mouseDragged(MouseEvent arg0) {
  119.  
  120.        
  121.     }
  122.  
  123.     @Override
  124.     public void mouseMoved(MouseEvent e) {
  125.        
  126.         //if we do not have a target yet - create one
  127.         if (this.target == null)
  128.         {
  129.             target = new Point();
  130.         }
  131.        
  132.         //set new values for our ball's target
  133.         target.x = e.getX() - ballWidth / 2 - WINDOW_LEFT_BORDER_SIZE;
  134.         target.y = e.getY() - ballHeight / 2 - WINDOW_TOP_BORDER_SIZE;
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement