Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package project;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Label;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9.  
  10. import javax.swing.JPanel;
  11. import javax.swing.text.LayoutQueue;
  12.  
  13. public class Panel extends JPanel implements KeyListener{
  14.    
  15.     int x;
  16.     int y;
  17.     final static int width = 20;
  18.     final static int height = 20;
  19.     int [][]array = new int[20][20];
  20.     int smerX;
  21.     int smerY;
  22.     final static int rychlost = 20;
  23.    
  24.     public Panel(){
  25.         this.setBackground(Color.WHITE);
  26.         this.setPreferredSize(new Dimension(600,400));
  27.         this.setVisible(true);
  28.     //  fillArray();
  29.         /*
  30.         Label l1 = new Label();
  31.         l1.setText("Piskvorky 1.0");
  32.         l1.setVisible(true);
  33.         l1.setBounds(500, 500, 30, 30);
  34.         this.add(l1);
  35.         */
  36.     }
  37.    
  38.     public void fillArray(){
  39.        
  40.         array[5][3] = 1;
  41.         array[12][1] = 1;
  42.         array[0][0] = 1;
  43.         array[11][3] = 2;
  44.         array[2][1] = 2;
  45.         array[0][7] = 2;
  46.         array[14][14] = 2;
  47.     }
  48.    
  49.    
  50.     public void checkRowforWin(){
  51.         for (int i = 0; i < 20; i++) {
  52.             for (int j = 0; j < 20; j++) {
  53.                
  54.             }
  55.         }
  56.     }
  57.    
  58.     public void paint(Graphics g){
  59.         for (int i = 0; i < 20; i++) {
  60.             for (int j = 0; j < 20; j++) {
  61.                 if(array[i][j] == 1){
  62.                 g.setColor(Color.RED);
  63.                 g.fillRect(x+i*20, y+j*20, width, height);
  64.                 }
  65.                
  66.                 else if(array[i][j] == 2){
  67.                     g.setColor(Color.BLUE);
  68.                     g.fillRect(x+i*20, y+j*20, width, height);
  69.                     }
  70.                 else{
  71.                     g.setColor(Color.WHITE);
  72.                     g.fillRect(x+i*20, y+j*20, width, height);
  73.                 }
  74.                 g.setColor(Color.BLACK);
  75.                 g.drawRect(x+i*20, y+j*20, width, height);
  76.                
  77.             }
  78.         }
  79.     }
  80.  
  81.     @Override
  82.     public void keyTyped(KeyEvent e) {
  83.         // TODO Auto-generated method stub
  84.        
  85.     }
  86.  
  87.     @Override
  88.     public void keyPressed(KeyEvent e) {
  89.         // TODO Auto-generated method stub
  90.           int klavesa = e.getKeyCode();
  91.           if (klavesa == KeyEvent.VK_LEFT) {
  92.                 smerX = -rychlost;
  93.           } else if (klavesa == KeyEvent.VK_UP) {
  94.                 smerY = -rychlost;
  95.           } else if (klavesa == KeyEvent.VK_RIGHT) {
  96.                 smerX = rychlost;
  97.           } else if (klavesa == KeyEvent.VK_DOWN) {
  98.                 smerY = rychlost;
  99.           } else if (klavesa == KeyEvent.VK_SPACE){
  100.               array[5][5] = 1;
  101.           }
  102.     }
  103.        
  104.  
  105.     @Override
  106.     public void keyReleased(KeyEvent e) {
  107.         // TODO Auto-generated method stub
  108.         int klavesa = e.getKeyCode();
  109.         if (klavesa == KeyEvent.VK_LEFT) {
  110.             smerX = 0;
  111.         } else if (klavesa == KeyEvent.VK_UP) {
  112.             smerY = 0;
  113.         } else if (klavesa == KeyEvent.VK_RIGHT) {
  114.             smerX = 0;
  115.         } else if (klavesa == KeyEvent.VK_DOWN) {
  116.             smerY = 0;
  117.          
  118.         } else if (klavesa == KeyEvent.VK_SPACE){
  119.               array[5][5] = 1;
  120.         }
  121.     }
  122.  
  123.        
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement