Hey there,
new to Java, so excuse my noobness, but I'm having problem creating a simple method within my Draughts game. atm I have the entire board spawning a 8x8 board, half of them being white and the other half being darker.
Now what I need to do is make it so when the mouse goes over the darker squares, it 'highlights' them turning them yellow. I have done everything I can, I have implemented the mouselistener and a method which changes the "DARK_SQURES" to "SELECTED_DARK_SQURES", although it doesn't seem to work, I feel Im missing something very simple.
So I am wondering if I can get some insight into what Im doing wrong.
Board.java
package draughts; import java.awt.*; import java.util.ArrayList; import javax.swing.*; import java.awt.event.*; public class Board extends JPanel implements MouseListener { private Square[][] squares; private ArrayList<DraughtsPiece> lightPieces; private ArrayList<DraughtsPiece> darkPieces; public Board(int numRows) { super(new GridLayout(numRows, numRows)); squares = new Square[numRows][numRows]; lightPieces = new ArrayList<DraughtsPiece>(); darkPieces = new ArrayList<DraughtsPiece>(); setupBoard(numRows); setupPieces(numRows); allocatePieces(); } private void setupBoard(int numRows) { boolean lightSquare = true; for (int row = 0; row < numRows; row++) { for (int col = 0; col < numRows; col++) { if (lightSquare) { squares[row][col] = new Square(Square.LIGHT_SQUARE_COLOUR, row + 1, col + 1); } else { squares[row][col] = new Square(Square.DARK_SQUARE_COLOUR, row + 1, col + 1); squares[row][col].addMouseListener(this); } add(squares[row][col]); lightSquare = !lightSquare; } lightSquare = !lightSquare; } } private void setupPieces(int numRows) { int numPieces = ((numRows * numRows) - (2 * numRows)) / 4; for (int i = 0; i < numPieces; i++) { DraughtsPiece p = new DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR); lightPieces.add(p); p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR); darkPieces.add(p); } } private void allocatePieces() { int row = squares.length - 1; int col = 0; for (DraughtsPiece p : lightPieces) { squares[row][col].setPiece(p); col += 2; if (col >= squares[0].length) { col = row % 2; row--; } } row = 0; col = squares[0].length - 1; for (DraughtsPiece p : darkPieces) { squares[row][col].setPiece(p); col -= 2; if (col < 0) { row++; col = squares[0].length - 1 - (row % 2); } } } public void mouseEntered(MouseEvent e) { ((Square)e.getSource()).enter(); } @Override public void mouseClicked(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mousePressed(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseReleased(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseExited(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } }
Square.java
package draughts; import java.awt.*; import javax.swing.*; /** * a class called Square which extends to JPanel, allowing the programmer to * access JPanel functions * @author Tim */ public class Square extends JPanel { /** * represents the colour of the light square on the board */ public static final Color LIGHT_SQUARE_COLOUR = new Color(0xdfdfdf); /** * represents the colour of the dark square on the board */ public static final Color DARK_SQUARE_COLOUR = new Color(0x333333); /** * represents the colour of the selected dark square on the board */ public static final Color SELECTED_DARK_SQUARE_COLOUR = Color.YELLOW; private Color background; private int row, column; private Color selectedBackground; private DraughtsPiece piece; private boolean selected = false; /** * the constructor for the Square class * @param c - represents Color as c, this is the colour of the square * @param row - represents row as an int, this is how many squares there are * going in a row * @param col - represents col as an int, this is how many squares there are * going in a column */ public Square(Color c, int row, int col) { super(new BorderLayout()); background = c; this.row = row; this.column = col; setBackground(background); if (background == DARK_SQUARE_COLOUR) { selectedBackground = SELECTED_DARK_SQUARE_COLOUR; } piece = null; } /** * a public int method called getRow * @return - returns the int representing row */ public int getRow() { return row; } /** * a public int method called getColumn * @return - returns the int representing column */ public int getColumn() { return column; } /** * a public void method called setPiece * @param piece - Calls in the class DraughtsPiece and represents a variable * called piece */ public void setPiece(DraughtsPiece piece) { if (piece == null && this.piece != null) { remove(this.piece); this.piece.setSquare(null); this.piece = null; } else if (piece != null && this.piece == null) { this.piece = piece; piece.setSquare(this); add(piece); } } /** * a public method calling the DraughtsPiece class, called getPiece * @return - returns the piece variable called from the DraughtsPiece class */ public DraughtsPiece getPiece() { return piece; } /** * a protected method called enter, which doesn't return anything as it's void * but sets the background which is selected by the mouse. Making it yellow */ protected void enter() { setBackground(selectedBackground); } /** * a protected method called exit, which doesn't return anything as it's void * but sets the background back to black when the mouse leaves. */ protected void exit() { setBackground(background); } /** * a protected method called setSelected, which doesn't return anything as it's * void. This method changes the background * @param b - a boolean value set as 'b' */ protected void setSelected(boolean b) { selected = b; setBackground(b == false ? background : selectedBackground); } /** * a protected method called isSelected, which returns a boolean value * @return - returns a boolean value called selected */ protected boolean isSelected() { return selected; } }
Sorry about the javaDoc!
Thanks,
Xooch