/* Copyright 1996 Rujith de Silva. Modified 2002-03-29. */ package rujith.pentominoes; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import java.awt.Button; import java.awt.Checkbox; import java.awt.Cursor; import java.awt.CheckboxGroup; import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Label; import java.awt.LayoutManager; import java.awt.Panel; /** * Controls of a pentominoes board. Includes the Board on which the * pieces move. */ public class BoardControls extends Frame { Checkbox cb3; Checkbox cb4; Checkbox cb5; Checkbox cb6; Button quit; Board board; LayoutManager layout; /** * Are the BoardControls still active? */ public boolean boardHere = true; /** * Create a new BoardControls. */ public BoardControls() { super("Pentominoes"); this.setBackground(Color.lightGray); // Create the check-boxes for the height of the target area CheckboxGroup cbg = new CheckboxGroup(); this.cb3 = new Checkbox("3", cbg, false); this.cb4 = new Checkbox("4", cbg, false); this.cb5 = new Checkbox("5", cbg, false); this.cb6 = new Checkbox("6", cbg, true); // Listen to changes in the checkbox selection ItemListener il = new ItemListener() { public void itemStateChanged(ItemEvent e) { BoardControls.this.setHeight ((Checkbox) e.getItemSelectable()); } }; this.cb3.addItemListener(il); this.cb4.addItemListener(il); this.cb5.addItemListener(il); this.cb6.addItemListener(il); GridBagLayout gbl = new GridBagLayout(); this.setLayout(gbl); this.layout = gbl; GridBagConstraints gbc = new GridBagConstraints(); gbc.ipadx = gbc.ipady = 1; Label height = new Label ("Target Height:"); gbc.anchor = GridBagConstraints.EAST; gbc.weightx = 1.0; gbl.setConstraints(height, gbc); this.add(height); gbc.weightx = 0.0; gbc.anchor = GridBagConstraints.WEST; gbl.setConstraints(this.cb3, gbc); this.add(this.cb3); gbl.setConstraints(this.cb4, gbc); this.add(this.cb4); gbl.setConstraints(this.cb5, gbc); this.add(this.cb5); gbl.setConstraints(this.cb6, gbc); this.add(this.cb6); // Button to quit/close the BoardControls this.quit = new Button("Quit"); this.quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BoardControls.this.quit(); }}); gbc.weightx = 1.5; gbc.ipadx = 5; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.CENTER; gbl.setConstraints(this.quit, gbc); this.add(this.quit); // The actual Board, occupying the majority of the space. The // BoardListener listens for changes in the Board to change // the cursor. this.board = new Board(new BoardListener() { public void changed(int status) { BoardControls.this.changed(status); }}); gbc.ipadx = 1; gbc.weightx = gbc.weighty = 1.0; gbc.anchor = GridBagConstraints.CENTER; gbl.setConstraints(this.board, gbc); this.add(board); // Show it all. gbl.layoutContainer(this); this.pack(); this.setSize(365, 430); } // Change the cursor according to the state of the board. void changed(int status) { int cursor = Cursor.DEFAULT_CURSOR; switch(status) { case 1: cursor = Cursor.MOVE_CURSOR; break; case 2: cursor = Cursor.HAND_CURSOR; break; default: cursor = Cursor.DEFAULT_CURSOR; break; } this.setCursor(Cursor.getPredefinedCursor(cursor)); } // Close the board void quit() { // Mark the board as closed, even though the Java object // continues to exist. this.boardHere = false; this.dispose(); } // Set the height of the target area. void setHeight(Checkbox cb) { if (cb == this.cb3) this.board.setHeight(3); else if (cb == this.cb4) this.board.setHeight(4); else if (cb == this.cb5) this.board.setHeight(5); else if (cb == this.cb6) this.board.setHeight(6); } /** * @return Preferred dimension */ public Dimension getPreferredSize() { return this.layout.minimumLayoutSize(this); } /** * @return Minimum dimension */ public Dimension getMinimumSize() { return this.getPreferredSize(); } }