Skip to content

Commit

Permalink
Adding event listener for the view
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 13, 2016
1 parent 54d4060 commit 2e193ed
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 23 deletions.
38 changes: 38 additions & 0 deletions src/view/BoardEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package view;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class BoardEventListener implements MouseListener {

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

}

@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) {
// TODO Auto-generated method stub

}

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

}

}
60 changes: 43 additions & 17 deletions src/view/Checker.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.*;

/**
* Represents a graphical checkers piece on the checkers canvas.
* @author john
*
*/
public class Checker extends JButton {

/* The color of the checker */
private Color color;

public Checker(Color color) {
super("");
this.color = color;
initButtonProperties();
}

/**
* Initializes the properties of the Checker as a JButton
*/
private void initButtonProperties() {
/* We want the JButton to actually be completely transparent */
this.setContentAreaFilled(false);
this.setBorderPainted(false);
this.setFocusable(false);
this.setOpaque(false);
}


/**
* Overrides the JButton's painComponent method to paint a circle which
* represents the checkers piece.
*/
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(hints);
g2.setColor(color);
g2.fillOval(5, 5, getSize().width-10,getSize().height-10);
super.paintComponent(g2);
/* Cast to a 2D graphics object */
Graphics2D g2 = (Graphics2D) g;

/* Add some hints from rendering */
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
hints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(hints);

/* Set the graphics object's color to the checker's color
* and paint an oval which represents the checker. */
g2.setColor(color);
g2.fillOval(5, 5, getSize().width-10,getSize().height-10);

/* Make a call to the super classes painComponent method */
super.paintComponent(g2);
}

}
38 changes: 32 additions & 6 deletions src/view/Square.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import model.Location;

/**
* Represents a square or cell on the checkers board. A square may contain a
* checker.
* @author john
*
*/
public class Square extends JPanel implements MouseListener {
private Color color;

/**
* The {@link Location} (row, col) of the square on the board.
*/
private final Location location;

/**
* The {@link Checker} object contained in this square. Null if there is none.
*/
private Checker piece;

/**
* A boolean value representing whether the square is selected for movement.
*/
private boolean selected;


/**
* Constructs a graphical square with the given color and location.
* @param color A {@link Color} object representing the square's color.
* @param location A {@link Location} object representing the row and column.
*/
public Square(Color color, Location location) {
super();
this.color = color;
this.location = location;
this.selected = false;
initSquare(color);
}


private void initSquare(Color color) {
this.setBackground(color);
this.setLayout(new BorderLayout());
}


public Location getCellLocation() {
return location;
}
Expand Down

0 comments on commit 2e193ed

Please sign in to comment.