Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.09 KB | None | 0 0
  1. package lab4.gui;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.util.Observable;
  7. import java.util.Observer;
  8. import javax.swing.JPanel;
  9. import lab4.data.GameGrid;
  10.  
  11. /**
  12.  * A panel providing a graphical view of the game board
  13.  */
  14.  
  15. public class GamePanel extends JPanel implements Observer{
  16.  
  17.     private final int UNIT_SIZE = 20;
  18.     private GameGrid grid;
  19.     int[][] gameBoard;
  20.    
  21.     /**
  22.      * The constructor
  23.      *
  24.      * @param grid The grid that is to be displayed
  25.      */
  26.     public GamePanel(GameGrid grid){
  27.         this.grid = grid;
  28.         gameBoard = new int[grid.getSize()][grid.getSize()];
  29.         grid.addObserver(this);
  30.         Dimension d = new Dimension(grid.getSize()*UNIT_SIZE+1, grid.getSize()*UNIT_SIZE+1);
  31.         this.setMinimumSize(d);
  32.         this.setPreferredSize(d);
  33.         this.setBackground(Color.WHITE);
  34.     }
  35.  
  36.     /**
  37.      * Returns a grid position given pixel coordinates
  38.      * of the panel
  39.      *
  40.      * @param x the x coordinates
  41.      * @param y the y coordinates
  42.      * @return an integer array containing the [x, y] grid position
  43.      */
  44.     public int[] getGridPosition(int x, int y){
  45.         int xSquare = (int)Math.ceil(x/UNIT_SIZE); // checks witch square in x-direction was clicked
  46.         int ySquare = (int)Math.ceil(y/UNIT_SIZE); // checks witch square in y-direction was clicked
  47.         return new int[] {xSquare,ySquare}; // returns array with x- and y-coordinate for the square that was clicked
  48.     }
  49.     /**
  50.      * Makes sure that the board gets repainted when something has changed
  51.      */
  52.     public void update(Observable arg0, Object arg1) {
  53.         this.repaint();
  54.     }
  55.    
  56.     /**
  57.      * Paints the board or game grid and the moves made by calling paintBoard with the graphics
  58.      * @param g the graphics
  59.      */
  60.     public void paintComponent(Graphics g){
  61.         super.paintComponent(g);
  62.         paintBoard(g);//drawing of the board
  63.         }
  64.    
  65.     /**
  66.      * Paints the board or game grid together with the moves made.
  67.      * Filled oval in blue represents player me and filled oval in pink represents player other.
  68.      * @param g the graphics
  69.      */
  70.     private void paintBoard(Graphics g){ //drawing of the board
  71.         int x = 0; // creates integer x witch is the top left corners x coordinate and sets it to 0
  72.         int y = 0; // creates integer y witch is the top left corners y coordinate and sets it to 0
  73.         for(int i = 0; i < grid.getSize();i++) {
  74.             for (int n = 0; n < grid.getSize(); n++){
  75.                 g.drawRect(x, y, UNIT_SIZE, UNIT_SIZE);
  76.                 if (this.grid.board[i][n] == this.grid.ME){
  77.                     g.setColor(Color.BLUE); //sets color for the making of the oval
  78.                     g.fillOval(i*UNIT_SIZE+UNIT_SIZE/4, n*UNIT_SIZE+UNIT_SIZE/4, UNIT_SIZE/2, UNIT_SIZE/2);
  79.                     //creates a small circle in the middle of the square in question. is ONLY (fuck!) drawn on ME:s board
  80.                     g.setColor(Color.black); // resets the color back to default
  81.                     x = x + UNIT_SIZE;
  82.                 }
  83.                  if (this.grid.board[i][n] == this.grid.OTHER){
  84.                     g.setColor(Color.PINK);
  85.                     g.fillOval(i*UNIT_SIZE/2, n*UNIT_SIZE/2, UNIT_SIZE, UNIT_SIZE);
  86.                     g.setColor(Color.black);
  87.                     x = x + UNIT_SIZE;
  88.                 }
  89.                 else {
  90.                     x = x + UNIT_SIZE;
  91.                 }  
  92.             }
  93.             x = 0;
  94.             y = y + UNIT_SIZE;
  95.         }
  96.        
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement