Question
I would like to know how to to populate the board with game pieces that are pictures. As well as programming the computer's movement import
I would like to know how to to populate the board with game pieces that are pictures. As well as programming the computer's movement
import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.swing.border.*;
public class CTK {
private final JPanel gui = new JPanel(new BorderLayout(3, 3)); private JButton[][] Gameboard = new JButton[8][8]; private JPanel Board; private final JLabel message = new JLabel("Wet Land Adventure!"); private static final String COLS = "ABCDEFGH";
CTK() { initializeGui(); } public final void initializeGui() { // set up the main GUI gui.setBorder(new EmptyBorder(5, 5, 5, 5)); JToolBar tools = new JToolBar(); tools.setFloatable(false); gui.add(tools, BorderLayout.PAGE_START); tools.add(new JButton("P vs P")); // TODO - add functionality! tools.add(new JButton("P vs C")); // TODO - add functionality! tools.add(new JButton("P vs x4")); // TODO - add functionality! tools.addSeparator(); tools.add(new JButton("Restart")); // TODO - add functionality! tools.addSeparator(); tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
Board = new JPanel(new GridLayout(0, 9)); Board.setBorder(new LineBorder(Color.BLUE)); gui.add(Board);
// create the chess board squares Insets buttonMargin = new Insets(0,0,0,0); for (int ii = 0; ii < Gameboard.length; ii++) { for (int jj = 0; jj < Gameboard[ii].length; jj++) { JButton b = new JButton(); b.setMargin(buttonMargin);
ImageIcon icon = new ImageIcon( new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB)); b.setIcon(icon); if ((jj % 2 == 1 && ii % 2 == 1) //) { || (jj % 2 == 0 && ii % 2 == 0)) { b.setBackground(Color.GREEN); } else { b.setBackground(Color.BLACK); } Gameboard[jj][ii] = b; } }
//fill the chess board Board.add(new JLabel("")); // fill the top row for (int ii = 0; ii < 8; ii++) { Board.add( new JLabel(COLS.substring(ii, ii + 1), SwingConstants.CENTER)); } // fill the black non-pawn piece row for (int ii = 0; ii < 8; ii++) { for (int jj = 0; jj < 8; jj++) { switch (jj) { case 0: Board.add(new JLabel("" + (ii + 1), SwingConstants.CENTER)); default: Board.add(Gameboard[jj][ii]); } } } }
public final JComponent getCTK_Board() { return Board; }
public final JComponent getGui() { return gui; }
public static void main(String[] args) { Runnable r = new Runnable() {
@Override public void run() { CTK cb = new CTK();
JFrame f = new JFrame("Don't get snapped!"); f.add(cb.getGui()); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLocationByPlatform(true);
f.pack();
f.setMinimumSize(f.getSize()); f.setVisible(true); } }; SwingUtilities.invokeLater(r); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started