Advertisement
Lexolordan

Untitled

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