From 2e193ed449b8473e95674ae86944444106b2285d Mon Sep 17 00:00:00 2001 From: jwb11006 Date: Wed, 13 Apr 2016 16:12:12 -0400 Subject: [PATCH] Adding event listener for the view --- src/view/BoardEventListener.java | 38 ++++++++++++++++++++ src/view/Checker.java | 60 +++++++++++++++++++++++--------- src/view/Square.java | 38 ++++++++++++++++---- 3 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 src/view/BoardEventListener.java diff --git a/src/view/BoardEventListener.java b/src/view/BoardEventListener.java new file mode 100644 index 0000000..5b8521c --- /dev/null +++ b/src/view/BoardEventListener.java @@ -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 + + } + +} diff --git a/src/view/Checker.java b/src/view/Checker.java index b4fed20..8754a1e 100644 --- a/src/view/Checker.java +++ b/src/view/Checker.java @@ -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); } } diff --git a/src/view/Square.java b/src/view/Square.java index 891c8bc..ffc39cf 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -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; }