|
Newbie
Join Date: Apr 2005
Posts: 11
Rep Power: 0 
|
I've scrapped the idea of using images, now i've painted pieces on as icons. The problem i'm having now is coming up with an algorithm that will put the pieces in the right squares. I tried using an array of booleans but I kept getting ArrayOutOfBounds Exceptions (or something of the sort)
Here is my code now. It works as it is but its kinda yucky :-p
Piece.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Piece implements Icon
{
public int player;
protected Dimension dim = new Dimension(45, 45);
public boolean isKing;
public Piece(int player)
{
this.player = player;
this.isKing = false;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
if (player == 1)
{
g.setColor(Color.red);
}
else
{
g.setColor(Color.white);
}
g.setColor(Color.white);
g.fillOval(x, x, dim.width, dim.height);
}
public int getIconWidth()
{
return dim.width;
}
public int getIconHeight()
{
return dim.height;
}
public void setKing()
{
this.isKing = true;
}
}
MyBoard.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyBoard extends JFrame implements ActionListener
{
private static int BOARD_SIZE;
private BoardButton[][] boardSquare = new BoardButton[BOARD_SIZE][BOARD_SIZE];
//private int[][] p1Places = { {0, 1}, {0, 3}, {0, 5}, {0, 7}, {1, 0}, {1, 2}, {1, 4}, {1, 6}, {2, 1}, {2, 3}, {2, 5}, {2, 7} };
//private int[][] p2Places = { {5, 0}, {5 ,2}, {5, 4}, {5, 6}, {6, 1}, {6, 3}, {6, 5}, {6, 7}, {7, 0}, {7, 2}, {7, 4}, {7, 6} };
// col*row (3*8)
private static boolean[][] p1Places = { { false, true, false, true, false, true, false, true },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true } };
public MyBoard()
{
setTitle("Draughts");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cntnr = this.getContentPane();
cntnr.setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
for (int col = 0; col < BOARD_SIZE; ++col)
{
for (int row = 0; row < BOARD_SIZE; ++row)
{
boardSquare[row][col] = new BoardButton(row, col);
boardSquare[row][col].setPreferredSize(new Dimension(50,50));
boardSquare[row][col].addActionListener(this);
boardSquare[row][col].setBackground(setColour(row, col));
cntnr.add(boardSquare[row][col]);
}
}
resetPieces();
this.setSize(400, 450);
this.setVisible(true);
}
private Color setColour(int x, int y)
{
Color c;
// Stu Rules
if ((x + y) % 2 == 1)
{ c = Color.black; }
else
{ c = Color.red; }
return c;
}
private void resetBoard()
{
for (int y = 0; y < BOARD_SIZE; y++)
{
for (int x = 0; x < BOARD_SIZE; x++)
{
boardSquare[x][y].setBackground(setColour(x, y));
}
}
}
private void resetPieces()// Dont put resetBoard and resetPieces together - see actionPerformed
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < BOARD_SIZE; x++) // For the time being
{
if ((x + y) % 2 == 1) // This works for now
// but i'm trying to get this to work: if (p1Places[x][y])
{
putPiece(boardSquare[x][y], 1); //put pieces in right places
}
}
}
}
public void putPiece(BoardButton button, int player)
{
button.setIcon(new Piece(player));
}
//clearPiece
private BoardButton buttonClicked;
public void actionPerformed(ActionEvent evt)
{
resetBoard();
buttonClicked = (BoardButton)evt.getSource();
if ((buttonClicked.getRow() + buttonClicked.getCol()) % 2 == 1)
{
buttonClicked.setBackground(Color.magenta);
}
System.out.println("\nButton clicked properties: \n" + evt.getSource().toString());
}
public static void main(String[] args)
{
BOARD_SIZE = 8;
MyBoard DBoard = new MyBoard();
}
}
BoardButton.java
import javax.swing.JButton;
public class BoardButton extends JButton
{
protected int row, col;
protected boolean occupied;
public BoardButton()
{
super();
}
public BoardButton(int row, int col)
{
super();
this.row = row;
this.col = col;
}
public boolean isOccupied()
{
return occupied;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
public String toString()
{
return "Row=" + row + ",Col=" + col + " \n" + super.toString();
}
}
I'm trying to use the "p1Places"" array to determine whether a piece should be placed in a particualar square. is there a way to do this that is independant of the baord size? if not, i'm happy with keeping the board 8*8
My project supervisor wants to see good java code so i'd love some suggestions on how i can make it look a bit prettier
Thanks 
|