Advertisement
Guest User

Untitled

a guest
Sep 10th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Graphics2D;
  3. import java.awt.RenderingHints;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6.  
  7. @SuppressWarnings("serial")
  8. public class Game extends JPanel {
  9.  
  10.     int x = 0;
  11.     int y = 0;
  12.     int xa = 1;
  13.     int ya = 1;
  14.  
  15.     private void moveBall() {
  16.         if (x + xa < 0)
  17.             xa = 1;
  18.         if (x + xa > getWidth() - 30)
  19.             xa = -1;
  20.         if (y + ya < 0)
  21.             ya = 1;
  22.         if (y + ya > getHeight() - 30)
  23.             ya = -1;
  24.        
  25.         x = x + xa;
  26.         y = y + ya;
  27.     }
  28.  
  29.     @Override
  30.     public void paint(Graphics g) {
  31.         super.paint(g);
  32.         Graphics2D g2d = (Graphics2D) g;
  33.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  34.                 RenderingHints.VALUE_ANTIALIAS_ON);
  35.         g.fillOval(x, y, 30, 30);
  36.  
  37.     }
  38.  
  39.     public static void main(String[] args) throws InterruptedException {
  40.         JFrame frame = new JFrame("Mini Tennis");
  41.         Game game = new Game();
  42.         frame.add(game);
  43.         frame.setSize(300, 400);
  44.         frame.setVisible(true);
  45.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.        
  47.         while (true) {
  48.             game.moveBall();
  49.             game.repaint();
  50.             Thread.sleep(10);
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement