zukakirinku

Pong

Nov 21st, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class Pong
  7. {
  8.   public static void main(String args[])
  9.   {
  10.     JFrame frame1 = new Pong1();
  11.     frame1.setVisible(true);
  12.   }
  13. }
  14. class Pong1 extends JFrame
  15. {
  16.   private JPanel panelArea;
  17.  
  18.   public Pong1()
  19.   {
  20.     JPanel panelInferior;
  21.    
  22.     setSize(300,300);
  23.     setTitle("Exemplo Thread...");
  24.    
  25.     addWindowListener(
  26.       new WindowAdapter()
  27.       {
  28.         public void windowClosing(WindowEvent we)
  29.         {  System.exit(0);  }
  30.       });
  31.      
  32.     Container contentPane = getContentPane();
  33.     panelArea = new JPanel();
  34.     contentPane.add(panelArea,"Center");
  35.     panelInferior = new JPanel();
  36.  
  37.     addBtn(panelInferior,"Iniciar",
  38.       new ActionListener()
  39.       {
  40.         public void actionPerformed(ActionEvent ae)
  41.         {
  42.             BolaThreadExemplo b = new BolaThreadExemplo(panelArea);
  43.             b.start();
  44.         }
  45.       });
  46.  
  47.     addBtn(panelInferior,"Fechar",
  48.       new ActionListener()
  49.       {
  50.         public void actionPerformed(ActionEvent ae)
  51.         {  System.exit(0);  }
  52.       });
  53.    
  54.     contentPane.add(panelInferior,"South");
  55.   }
  56.  
  57.   public void addBtn(Container c, String tit, ActionListener a)
  58.   {
  59.     JButton btn = new JButton(tit);
  60.     c.add(btn);
  61.     btn.addActionListener(a);
  62.   }
  63. }
  64.  
  65. class BolaThreadExemplo extends Thread
  66. {
  67.   private JPanel caixa;
  68.   private static final int LARG = 10;
  69.   private static final int ALT  = 10;
  70.   private int pos_x = 0;
  71.   private int pos_y = 0;
  72.   private int desloc_x = 2;
  73.   private int desloc_y = 2;
  74.  
  75.   public BolaThreadExemplo(JPanel p)
  76.   {  caixa = p;  }
  77.  
  78.   public void desenhar()
  79.   {
  80.     Graphics g = caixa.getGraphics();
  81.     g.fillOval(pos_x,pos_y,LARG,ALT);
  82.     g.dispose();
  83.   }
  84.  
  85.   public void movimentar()
  86.   {
  87.     Graphics g = caixa.getGraphics();
  88.     g.setColor(caixa.getBackground());
  89.     g.fillOval(pos_x,pos_y,LARG,ALT);
  90.     pos_x += desloc_x;
  91.     pos_y += desloc_y;
  92.    
  93.     Dimension dim = caixa.getSize();
  94.  
  95.     if (pos_x < 0)
  96.     {  
  97.       pos_x = 0;
  98.       desloc_x = -desloc_x;
  99.     }
  100.     if (pos_x+LARG >= dim.width)
  101.     {
  102.       pos_x=dim.width-LARG;
  103.       desloc_x = -desloc_x;
  104.     }
  105.    
  106.     if (pos_y < 0)
  107.     {
  108.       pos_y = 0;
  109.       desloc_y = -desloc_y;
  110.     }
  111.     if (pos_y+ALT >= dim.height)
  112.     {
  113.       pos_y=dim.height-ALT;
  114.       desloc_y = -desloc_y;
  115.     }
  116.     g.setColor(Color.BLUE);
  117.     g.fillOval(pos_x,pos_y,LARG,ALT);
  118.     g.dispose();
  119.   }
  120.  
  121.   public void run()
  122.   {
  123.     try
  124.     {
  125.       desenhar();
  126.       while (true)
  127.       {
  128.         movimentar();
  129.         sleep(10);
  130.       }
  131.     }
  132.     catch (InterruptedException exc) {}
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment