Advertisement
Lexolordan

Game

May 28th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class prog {
  7.     public static void main(String[] args) {
  8.         myFrame okno = new myFrame();
  9.     }
  10. }
  11.  
  12. class myFrame extends JFrame
  13. {
  14.     private int z = 0;
  15.     String z1 = Integer.toString(z);
  16.  
  17.     public myFrame()
  18.     {
  19.         setBounds(0,0,500,500);
  20.         setVisible(true);
  21.         Container cont = getContentPane();
  22.         myPanel pan = new myPanel();
  23.         cont.add(pan);
  24.        
  25.         JLabel lab = new JLabel(z1);
  26.         lab.setSize(10, 10);
  27.         lab.setLocation(0,0);
  28.         cont.add(lab);
  29.     }
  30. }
  31.  
  32. class myPanel extends JPanel
  33. {
  34.     private int x = 0,y = 0, napr=0;
  35.  
  36.    
  37.     private class myKey implements KeyListener
  38.     {
  39.         public void keyPressed(KeyEvent e)
  40.         {
  41.             int key_ = e.getKeyCode();
  42.             if ((key_ == 39)&&(x != 450))x=x+5;
  43.             if ((key_ == 37)&&(x != 0))x=x-5;
  44.             if ((key_ == 38)&&(y != 0))y=y-5;
  45.             if ((key_ == 40)&&(y != 450))y=y+5;
  46.         }
  47.         public void keyReleased(KeyEvent e){}
  48.         public void keyTyped(KeyEvent e){}
  49.     }
  50.    
  51.     public myPanel()
  52.     {
  53.        
  54.        
  55.         addKeyListener(new myKey());
  56.         setFocusable(true);
  57.        
  58.         Timer time = new Timer(1, new ActionListener() {
  59.             public void actionPerformed(ActionEvent e) {
  60.                 repaint();
  61.             }
  62.         });
  63.        
  64.     time.start();
  65.     }
  66.     public void paintComponent(Graphics gr)
  67.     {          
  68.         super.paintComponent(gr);
  69.         gr.setColor(Color.black);
  70.         gr.fillRect(x, y, 50, 50);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement