package lab.games; import java.awt.GridLayout; import java.util.ArrayList; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * The view of the GUI. */ public class View extends JFrame { /** * The size of the images (in number of pixels). */ public static final int SIZE = 50; /* * Images for an empty slot, a slot with coin of the computer, and * a slot with coin of the player. */ private static final ImageIcon EMPTY = new ImageIcon("empty.jpg"); private static final ImageIcon COMPUTER = new ImageIcon("computer.jpg"); private static final ImageIcon PLAYER = new ImageIcon("player.jpg"); private List> slots; // redundant but convenient /** * Initializes this view. * * @param controller the controller of this view. * @pre. controller != null */ public View(Controller controller) { // set the title of the frame to "Connect 4" JPanel panel = new JPanel(); panel.setLayout(new GridLayout(Model.ROW + 1, Model.COLUMN)); for (int c = 0; c < Model.COLUMN; c++) { JButton button = new JButton("" + (c + 1)); button.setActionCommand("" + c); button.addActionListener(controller); panel.add(button); } List> slots = new ArrayList>(Model.ROW); for (int r = 0; r < Model.ROW; r++) { List row = new ArrayList(Model.COLUMN); for (int c = 0; c < Model.COLUMN; c++) { JLabel slot = new JLabel(View.EMPTY); row.add(slot); panel.add(slot); } slots.add(row); } this.setSlots(slots); this.add(panel); } /** * Sets the slots to the given list of lists. * * @param slots the slots. * @pre. slots != null */ private void setSlots(List> slots) { this.slots = slots; } /** * Sets the slot with the given row and column number to the * given player. * * @param row the row number. * @pre. row >= 0 && row < Model.ROW * @param column the column number. * @pre. column >= 0 && column < Model.COLUMN * @param player the player. * @pre. player == Model.PLAYER || player == Model.COMPUTER */ public void setSlot(int row, int column, int player) { // implement this method } }