Advertisement
Guest User

Untitled

a guest
May 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package GridBasics;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5.  
  6. public class GraphicsPanel extends JPanel{
  7.     private GridData data;
  8.  
  9.     public GraphicsPanel(GridData d){
  10.         setPreferredSize(Utilities.PANEL_SIZE);
  11.         data=d;
  12.     }
  13.  
  14.     @Override
  15.     public void paintComponent(Graphics g){
  16.         for(int i=0; i<data.getGrid().length; i++){
  17.             for(int j=0; j<data.getGrid()[i].length; j++){
  18.                 if(data.getGrid()[i][j]) {
  19.                     g.fillRect(j * Utilities.CELL_SIZE, i*Utilities.CELL_SIZE, Utilities.CELL_SIZE, Utilities.CELL_SIZE);
  20.                 }
  21.             }
  22.         }
  23.  
  24.     }
  25.  
  26. }
  27.  
  28.  
  29. package GridBasics;
  30.  
  31. import javax.swing.*;
  32.  
  33. public class GameFrame extends JFrame {
  34.     GridData data;
  35.     public GameFrame(GridData d){
  36.         data=d;
  37.         GraphicsPanel panel=new GraphicsPanel(data);
  38.         add(panel);
  39.  
  40.  
  41.  
  42.  
  43.  
  44.         pack();
  45.         setVisible(true);
  46.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  47.  
  48.     }
  49. }
  50.  
  51.  
  52. package GridBasics;
  53.  
  54. public class MainClass {
  55.  
  56.     public static void main(String[]args){
  57.         GridData data=new GridData();
  58.         GameFrame frame=new GameFrame(data);
  59.     }
  60. }
  61.  
  62.  
  63. package GridBasics;
  64.  
  65. public class GridData {
  66.     private boolean[][] grid;
  67.  
  68.     public GridData(){
  69.         grid=new boolean[Utilities.NUM_ROWS][Utilities.NUM_COLS];
  70.         loadGrid();
  71.         printGrid();
  72.     }
  73.  
  74.     public void loadGrid(){
  75.  
  76.         for(int i=0; i<grid.length; i++){
  77.             boolean cellValue=false;
  78.             if(i%2==0) {
  79.                 cellValue = true;
  80.             }
  81.             for(int j=0; j<grid[i].length; j++){
  82.                 grid[i][j]=cellValue;
  83.                 cellValue=!cellValue;
  84.             }
  85.         }
  86.     }
  87.  
  88.     public boolean[][] getGrid() {
  89.         return grid;
  90.     }
  91.  
  92.     public void setGrid(boolean[][] grid) {
  93.         this.grid = grid;
  94.     }
  95.  
  96.     public void printGrid(){
  97.         for(boolean b[]:grid){
  98.             for(boolean bool: b){
  99.                 System.out.print(bool);
  100.             }
  101.             System.out.println();
  102.         }
  103.     }
  104. }
  105.  
  106.  
  107. package GridBasics;
  108.  
  109. import java.awt.*;
  110.  
  111. public class Utilities {
  112.     public static final int PANEL_HEIGHT=600, PANEL_WIDTH=600;
  113.     public static final int CELL_SIZE=50;
  114.     public static final int NUM_ROWS=PANEL_HEIGHT/CELL_SIZE;
  115.     public static final int NUM_COLS=PANEL_WIDTH/CELL_SIZE;
  116.     public static final Dimension PANEL_SIZE=new Dimension(PANEL_WIDTH, PANEL_HEIGHT);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement