aquaballoon

Java - Table Tennis

May 21st, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. // Game.java
  2. Ball ball = new Ball(this);
  3.     Ball1 ball1 = new Ball1(this);
  4.     Racket racket = new Racket(this);
  5.    
  6.     public Game() {
  7.         initComponents();
  8.         new Thread(new Runnable(){
  9.             @Override
  10.             public void run() {
  11.                 while(true){
  12.                     move();
  13.                     table.repaint();
  14.                    
  15.                     try {
  16.                         Thread.sleep(10);
  17.                     } catch (InterruptedException ex) {
  18.                     }
  19.                 }
  20.             }
  21.         }).start();
  22.        
  23.         table.setFocusable(true);
  24.     }
  25.    
  26.     void move(){
  27.         ball.move();
  28.         ball1.move();
  29.         racket.move();
  30.  
  31. ------
  32.  
  33. table = new javax.swing.JPanel(){
  34.             public void paint(Graphics g){
  35.                 super.paint(g);
  36.                 ball.paint(g);
  37.                 ball1.paint(g);
  38.                 racket.paint(g);
  39.  
  40.             }
  41.  
  42.         };
  43.  
  44. -------
  45.  
  46. private void tableKeyPressed(java.awt.event.KeyEvent evt) {                                
  47.         racket.keyPressed(evt);
  48.        
  49.     }                                
  50.  
  51.     private void tableKeyReleased(java.awt.event.KeyEvent evt) {                                  
  52.         racket.keyReleased(evt);
  53.     }  
  54.  
  55.  
  56.  
  57. // Racket.java
  58. package exmay21;
  59.  
  60. import java.awt.Graphics;
  61. import java.awt.event.KeyEvent;
  62.  
  63.  
  64. public class Racket {
  65.    
  66.     Game game;
  67.    
  68.     int x = 0;
  69.     int xMove = 0;
  70.    
  71.     Racket(Game game){
  72.         this.game = game;
  73.     }
  74.    
  75.     void paint(Graphics g){
  76.         g.fillRect(x, game.getHeight()-50, 60, 10);
  77.     }
  78.    
  79.     void move(){
  80.         if(x+xMove > 0 && x+xMove < game.getWidth()-60)
  81.         x=x+xMove;
  82.        
  83.     }
  84.    
  85.     void keyPressed(KeyEvent e){
  86.         if(e.getKeyCode() == KeyEvent.VK_RIGHT)
  87.             xMove = 1;
  88.         if(e.getKeyCode() == KeyEvent.VK_LEFT)
  89.             xMove = -1;
  90.            
  91.     }
  92.    
  93.     void keyReleased(KeyEvent e){
  94.         xMove=0;
  95.     }
  96. }
  97.  
  98.  
  99. // Ball.java
  100. package tableTennis;
  101.  
  102. import java.awt.Color;
  103. import java.awt.Graphics;
  104. import java.awt.Rectangle;
  105. import javax.swing.JOptionPane;
  106.  
  107. public class Ball {
  108.     Tennis tennis;
  109.     int x=0;
  110.     int y=0;
  111.     int xMove = 1;
  112.     int yMove = 1;
  113.     int sizeBall = 30;
  114.    
  115.     Color changeColor = Color.black;
  116.     boolean visible;
  117.    
  118.    
  119.     Ball(Tennis tennis){
  120.         this.tennis=tennis;
  121.         visible = true;
  122.     }
  123.    
  124.     public void paint(Graphics g){
  125.         g.setColor(changeColor);
  126.         g.fillOval(x, y, 30, 30);
  127.     }
  128.    
  129.     public boolean isVisible() {
  130.         return visible;
  131.     }
  132.  
  133.     public void setVisible(Boolean visible) {
  134.         this.visible = visible;
  135.     }
  136.    
  137.     void move(){
  138.         if (x + xMove == 0) {
  139.             changeColor = Color.black;
  140.             xMove = 1;
  141.         }
  142.         if (x + xMove == tennis.getWidth() - 20) {
  143.             changeColor = Color.black;
  144.             xMove = -1;
  145.         }
  146.         if (y + yMove == 0) {
  147.             yMove = 1;
  148.             changeColor = Color.black;
  149.         }
  150.         if (y + yMove == tennis.getHeight() - 60) {
  151.             yMove = -1;
  152.             changeColor = Color.black;
  153.         }
  154.        
  155.         if(collision()){
  156.             System.out.println("Hit");
  157.             //tennis.gameOver();
  158.             sizeBall++;
  159.             //setVisible(false);
  160.             changeColor = Color.red;
  161.         }
  162.        
  163.         x = x+xMove;
  164.         y = y+yMove;
  165.     }
  166.    
  167.     void remove(){
  168.         x=-20;
  169.         y=-20;
  170.     }
  171.    
  172.     Rectangle getPosition(){
  173.         return new Rectangle(x, y, 30, 30);
  174.     }
  175.    
  176.     boolean collision(){
  177.         return tennis.ball1.getPosition().intersects(getPosition());
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment