Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab4.gui;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.util.Observable;
- import java.util.Observer;
- import javax.swing.JPanel;
- import lab4.data.GameGrid;
- /**
- * A panel providing a graphical view of the game board
- */
- public class GamePanel extends JPanel implements Observer{
- private final int UNIT_SIZE = 20;
- private GameGrid grid;
- int[][] gameBoard;
- /**
- * The constructor
- *
- * @param grid The grid that is to be displayed
- */
- public GamePanel(GameGrid grid){
- this.grid = grid;
- gameBoard = new int[grid.getSize()][grid.getSize()];
- grid.addObserver(this);
- Dimension d = new Dimension(grid.getSize()*UNIT_SIZE+1, grid.getSize()*UNIT_SIZE+1);
- this.setMinimumSize(d);
- this.setPreferredSize(d);
- this.setBackground(Color.WHITE);
- }
- /**
- * Returns a grid position given pixel coordinates
- * of the panel
- *
- * @param x the x coordinates
- * @param y the y coordinates
- * @return an integer array containing the [x, y] grid position
- */
- public int[] getGridPosition(int x, int y){
- int xSquare = (int)Math.ceil(x/UNIT_SIZE); // checks witch square in x-direction was clicked
- int ySquare = (int)Math.ceil(y/UNIT_SIZE); // checks witch square in y-direction was clicked
- return new int[] {xSquare,ySquare}; // returns array with x- and y-coordinate for the square that was clicked
- }
- /**
- * Makes sure that the board gets repainted when something has changed
- */
- public void update(Observable arg0, Object arg1) {
- this.repaint();
- }
- /**
- * Paints the board or game grid and the moves made by calling paintBoard with the graphics
- * @param g the graphics
- */
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- paintBoard(g);//drawing of the board
- }
- /**
- * Paints the board or game grid together with the moves made.
- * Filled oval in blue represents player me and filled oval in pink represents player other.
- * @param g the graphics
- */
- private void paintBoard(Graphics g){ //drawing of the board
- int x = 0; // creates integer x witch is the top left corners x coordinate and sets it to 0
- int y = 0; // creates integer y witch is the top left corners y coordinate and sets it to 0
- for(int i = 0; i < grid.getSize();i++) {
- for (int n = 0; n < grid.getSize(); n++){
- g.drawRect(x, y, UNIT_SIZE, UNIT_SIZE);
- if (this.grid.board[i][n] == this.grid.ME){
- g.setColor(Color.BLUE); //sets color for the making of the oval
- g.fillOval(i*UNIT_SIZE+UNIT_SIZE/4, n*UNIT_SIZE+UNIT_SIZE/4, UNIT_SIZE/2, UNIT_SIZE/2);
- //creates a small circle in the middle of the square in question. is ONLY (fuck!) drawn on ME:s board
- g.setColor(Color.black); // resets the color back to default
- x = x + UNIT_SIZE;
- }
- if (this.grid.board[i][n] == this.grid.OTHER){
- g.setColor(Color.PINK);
- g.fillOval(i*UNIT_SIZE/2, n*UNIT_SIZE/2, UNIT_SIZE, UNIT_SIZE);
- g.setColor(Color.black);
- x = x + UNIT_SIZE;
- }
- else {
- x = x + UNIT_SIZE;
- }
- }
- x = 0;
- y = y + UNIT_SIZE;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement