Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package project;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6.  
  7. import javax.swing.JPanel;
  8.  
  9. public class Panel extends JPanel{
  10.    
  11.     int x;
  12.     int y;
  13.     final static int width = 20;
  14.     final static int height = 20;
  15.     int [][]array = new int[20][20];
  16.    
  17.     public Panel(){
  18.         this.setBackground(Color.WHITE);
  19.         this.setPreferredSize(new Dimension(600,400));
  20.         this.setVisible(true);
  21.         fillArray();
  22.     }
  23.    
  24.     public void fillArray(){
  25.         array[5][3] = 1;
  26.         array[12][1] = 1;
  27.         array[0][0] = 1;
  28.         array[11][3] = 2;
  29.         array[2][1] = 2;
  30.         array[0][7] = 2;
  31.     }
  32.    
  33.     public void paint(Graphics g){
  34.         for (int i = 0; i < 20; i++) {
  35.             for (int j = 0; j < 20; j++) {
  36.                 if(array[i][j] == 1){
  37.                 g.setColor(Color.RED);
  38.                 g.fillRect(x+i*20, y+j*20, width, height);
  39.                 }
  40.                
  41.                 else if(array[i][j] == 2){
  42.                     g.setColor(Color.BLUE);
  43.                     g.fillRect(x+i*20, y+j*20, width, height);
  44.                     }
  45.                 else{
  46.                     g.setColor(Color.WHITE);
  47.                     g.fillRect(x+i*20, y+j*20, width, height);
  48.                 }
  49.                 g.setColor(Color.BLACK);
  50.                 g.drawRect(x+i*20, y+j*20, width, height);
  51.                
  52.             }
  53.         }
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement