Skip to content

Commit

Permalink
GUI piece selection is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Mar 31, 2016
1 parent cfff108 commit 816c1ae
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 17 deletions.
85 changes: 76 additions & 9 deletions src/view/CheckersCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,46 @@
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import model.Board;

public class CheckersCanvas extends JPanel {
public class CheckersCanvas extends JPanel implements MouseListener {

public static final int BOARD_DIM = 8;

ArrayList<Square> blackSquares;
ArrayList<Square> redSquares;


private ArrayList<Square> blackSquares;
private ArrayList<Square> redSquares;

private Square moveDestination;
private Square moveSource;

/* Checker board representation */
Board board;
GridBagConstraints layoutConstraints;

public CheckersCanvas() {
super(new GridLayout(BOARD_DIM, BOARD_DIM));
this.blackSquares = new ArrayList<Square>();
this.redSquares = new ArrayList<Square>();
initSquares();
}

public void initSquares() {
for (int i = 0; i < BOARD_DIM; ++i) {
if (i % 2 == 0) {
for (int j = 0; j < BOARD_DIM/2; ++j) {
Square black = new Square(Color.BLACK, i, j*2);
black.add(new GUIPiece(Color.WHITE));
black.addMouseListener(this);
GUIPiece p = new GUIPiece(Color.WHITE);
black.setPiece(new GUIPiece(Color.WHITE));
Square red = new Square(Color.RED, i, j*2 + 1);
red.addMouseListener(this);
this.add(black);
blackSquares.add(black);
this.add(red);
Expand All @@ -42,7 +51,9 @@ public void initSquares() {
} else {
for (int j = 0; j < BOARD_DIM/2; ++j) {
Square black = new Square(Color.BLACK, i, j*2 + 1);
black.addMouseListener(this);
Square red = new Square(Color.RED, i, j*2);
red.addMouseListener(this);
this.add(red);
blackSquares.add(red);
this.add(black);
Expand All @@ -51,4 +62,60 @@ public void initSquares() {
}
}
}

@Override
public void mouseClicked(MouseEvent arg0) {


}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
Square square = (Square) e.getComponent();
if(square.hasPiece()) {
if (!square.isSelected()) {
square.setSelected(true);
if (moveSource != null)
moveSource.setSelected(false);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = null;
moveSource = square;
} else {
if (square == moveSource)
moveSource = null;
square.setSelected(false);
}
} else {

if (!square.isSelected()) {
square.setSelected(true);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = square;
} else {
if (square == moveDestination)
moveDestination = null;
square.setSelected(false);
}
}

}

@Override
public void mouseReleased(MouseEvent e) {

}
}
5 changes: 4 additions & 1 deletion src/view/CheckersWindow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package view;

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class CheckersWindow extends JFrame {
Expand All @@ -13,8 +15,9 @@ public CheckersWindow() {
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.getContentPane().add(new CheckersCanvas());
this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER);
this.setVisible(true);
this.setResizable(false);

//pack();
}
Expand Down
7 changes: 5 additions & 2 deletions src/view/GUIPiece.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.BorderFactory;
import javax.swing.JButton;

public class GUIPiece extends JButton {
Expand All @@ -15,6 +16,8 @@ public GUIPiece(Color color) {
this.color = color;
this.setContentAreaFilled(false);
this.setBorderPainted(false);
this.setFocusable(false);
this.setOpaque(false);
}

protected void paintComponent(Graphics g) {
Expand All @@ -29,8 +32,8 @@ protected void paintComponent(Graphics g) {
hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(hints);
g2.fillOval(0, 0, getSize().width,getSize().height-1);
g2.fillOval(5, 5, getSize().width-10,getSize().height-10);
super.paintComponent(g2);
}
}

}
66 changes: 61 additions & 5 deletions src/view/Square.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class Square extends JButton {
public class Square extends JPanel implements MouseListener {
private Color color;
private int row;
private int column;
private GUIPiece piece;
private boolean selected;

public Square(Color color, int row, int column) {
super("");
super();
this.color = color;
this.setRow(row);
this.selected = false;
this.setColumn(column);
this.setBackground(color);
this.setLayout(new BorderLayout());
//this.setPreferredSize(new Dimension(100, 100));
}

public int getRow() {
Expand All @@ -44,6 +47,59 @@ public GUIPiece getPiece() {

public void setPiece(GUIPiece piece) {
this.piece = piece;
this.add(piece);
piece.addMouseListener(this);
this.validate();
}

public boolean isSelected() {
return selected;
}

public void setSelected(boolean val) {
if (val) {
System.out.println("setting border");
this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
} else {
this.setBorder(null);
}
this.selected = val;
}

public boolean hasPiece() {
return piece != null;
}

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
MouseEvent newE = new MouseEvent(this, e.getID(), e.getWhen(), e.getModifiers(),
e.getClickCount(), e.getX(), e.getY(), e.isPopupTrigger());
this.dispatchEvent(newE);

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}


Expand Down

0 comments on commit 816c1ae

Please sign in to comment.