Advertisement
mikhail_dvorkin

Untitled

Feb 18th, 2017
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.awt.GridLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7. @SuppressWarnings("serial")
  8. public class GameGUIView extends JPanel implements ActionListener {
  9.     GameModel model;
  10.     GameController controller;
  11.     int height, width;
  12.     JCheckBox[][] checkBoxes;
  13.    
  14.     public GameGUIView(GameModel model, GameController controller) {
  15.         this.model = model;
  16.         this.controller = controller;
  17.         boolean[][] field = model.getField();
  18.         height = field.length;
  19.         width = field[0].length;
  20.         this.setLayout(new GridLayout(height, width));
  21.         checkBoxes = new JCheckBox[height][width];
  22.         for (int i = 0; i < height; i++) {
  23.             for (int j = 0; j < width; j++) {
  24.                 checkBoxes[i][j] = new JCheckBox();
  25.                 this.add(checkBoxes[i][j]);
  26.                 checkBoxes[i][j].addActionListener(this);
  27.             }
  28.         }
  29.         redraw();
  30.     }
  31.    
  32.     public void redraw() {
  33.         boolean[][] field = model.getField();
  34.         for (int i = 0; i < height; i++) {
  35.             for (int j = 0; j < width; j++) {
  36.                 checkBoxes[i][j].setSelected(field[i][j]);
  37.             }
  38.         }
  39.     }
  40.  
  41.     @Override
  42.     public void actionPerformed(ActionEvent e) {
  43.         for (int i = 0; i < height; i++) {
  44.             for (int j = 0; j < width; j++) {
  45.                 if (e.getSource() == checkBoxes[i][j]) {
  46.                     controller.receiveAction(i, j);
  47.                 }
  48.             }
  49.         }
  50.         redraw();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement