Guys,
I am developing a chess manager program(not an interactive game)!
I have the following classes in my model package
ChessPieces
ChessBoard contains Squares in an ArrayList and chessPieces in an ArrayList
Square
Also in my view package i have a chessboardview
Its a JPanel ill post the code below.
Is there a way the get all the 64 squares in my JPanel with ImageIcon?
CHESSBOARD
package model; import java.util.Set; import java.util.ArrayList; import javax.swing.ImageIcon; public class ChessBoard { private Set<ChessPiece> chessPieces; private ArrayList<Square> squares; private static final String[] LOCATIONS = { "A1","B1","C1","D1","E1","F1","G1","H1", "A2","B2","C2","D2","E2","F2","G2","H2", "A3","B3","C3","D3","E3","F3","G3","H3", "A4","B4","C4","D4","E4","F4","G4","H4", "A5","B5","C5","D5","E5","F5","G5","H5", "A6","B6","C6","D6","E6","F6","G6","H6", "A7","B7","C7","D7","E7","F7","G7","H7", "A8","B8","C8","D8","E8","F8","G8","H8", }; public ChessBoard(){ squares = new ArrayList<Square>(); addLocations(); setSquareColors(); } private void addLocations(){ for(int i = 0; i < LOCATIONS.length; i++){ squares.add(new Square(LOCATIONS[i])); } } private void setSquareColors(){ for(int i = 0; i < LOCATIONS.length; i++){ squares.get(i).setColor("BROWN"); } for(int i = 1; i < LOCATIONS.length-1; i++){ squares.get(i).setColor("WHITE"); } } public Square getSquare(int i){ return squares.get(i); } public String getSquareColor(int i){ return squares.get(i).getColor(); } public ArrayList<Square> getAllSquares(){ return squares; } public void setSquareImage(int i, ImageIcon image){ getSquare(i).setImage(image); } }
SQUARE
package model; import view.assets.*; import java.util.ArrayList; import javax.swing.ImageIcon; public class Square{ private String location; private String squareColor; private ImageIcon image; private ArrayList<ChessPiece> chessPieces; private boolean empty; public Square(String location){ this.location = location; empty = true; } public String getLocation(){ return location; } public void setImage(ImageIcon image){ this.image = image; } public ImageIcon getImage(){ return image; } public boolean isEmpty(){ return empty; } public void setColor(String color){ squareColor = color; } public String getColor(){ return squareColor; } }
CHESSBOARDVIEW
package view; import java.awt.*; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.JLabel; import controller.Controller; import model.ChessBoard; import model.Square; public class ChessBoardView extends JPanel { //intialize variables private final static ImageIcon DARK_BROWN = new ImageIcon (ChessBoardView.class.getResource("assets/sqb.gif")); private final static ImageIcon LIGHT_BROWN = new ImageIcon (ChessBoardView.class.getResource("assets/sqw.gif")); //intialize components private JPanel southPanel = new JPanel(); private JPanel westPanel = new JPanel(); //intialize arrays to hold panels and images of the board; private JLabel[] labels = new JLabel[64]; private ChessBoard chessboard; public ChessBoardView (Controller controller){ chessboard = new ChessBoard(); createGUI(); } private void createGUI() { Dimension boardSize = new Dimension(200, 200); setLayout( new GridLayout(8, 8) ); setPreferredSize( boardSize ); setBounds(10, 10, boardSize.width, boardSize.height); setBorder(BorderFactory.createLineBorder(Color.BLACK)); addImageToSquare(); addSquaresToContentPane(); } private void addImageToSquare() { for(int i = 0; i < 64; i++){ if (chessboard.getSquare(i).getColor().equals("WHITE")){ chessboard.getSquare(i).setImage(LIGHT_BROWN); } else if (chessboard.getSquare(i).getColor().equals("BROWN")){ chessboard.getSquare(i).setImage(DARK_BROWN); } } } private void addSquaresToContentPane(){ } }