/* Copyright (C) 2002 J. M. Spivey */ import java.awt.*; import java.awt.event.*; /** The main class of the applet. * * Nothing very difficult here. */ public class PentoVision extends DemoFrame implements PentoWatcher { private int choice = 0; private int nsolutions; private PentoSearch searcher; private Thread solver = null; private boolean alive = true; private PentoBoard board = null; private Panel panel1 = new Panel(new BorderLayout()); private Panel panel2 = new Panel(); private Label solcount = new Label(); private Button reshapeButton, solveButton; private Button addButton(String name, Panel panel, ActionListener action) { Button b = new Button(name); b.addActionListener(action); panel.add(b); return b; } public PentoVision() { add(panel1, "South"); panel1.add(solcount, "West"); panel1.add(panel2, "East"); reshapeButton = addButton("Choose", panel2, new ActionListener () { public void actionPerformed(ActionEvent e) { reshape(); } }); solveButton = addButton("Solve", panel2, new ActionListener() { public void actionPerformed(ActionEvent e) { solve(); } }); addButton("Quit", panel2, new ActionListener() { public void actionPerformed(ActionEvent e) { die(); } }); setShape(0); } private void reshape() { Dialog chooser = new BoardChooser(this, choice); chooser.setVisible(true); } public void setShape(int choice) { this.choice = choice; setTitle("PentoVision - " + PentoSearch.menu[choice]); searcher = new PentoSearch(choice); searcher.addWatcher(this); if (board != null) remove(board); board = new PentoBoard(searcher.getBounds()); add(board, "Center"); pack(); Point squares[] = searcher.getSquares(); board.addPiece(Color.lightGray, squares, squares.length); board.setDirty(); solcount.setText(""); } private void solve() { reshapeButton.setEnabled(false); solveButton.setEnabled(false); nsolutions = 0; solcount.setText(""); solver = new Thread(searcher); solver.start(); } public void die() { if (solver != null) { searcher.kill(); solver.interrupt(); } super.die(); } // Methods of PentoWatcher interface public void solution(int count) { nsolutions = count; solcount.setText("Solution " + Integer.toString(count)); solcount.invalidate(); this.validate(); board.clear(); } public void place(char name, Color color, Point[] z, int n) { board.addPiece(color, z, n); } public void finish() { board.setDirty(); repaint(); Thread.yield(); try { Thread.sleep(100); } catch (InterruptedException e) {} } public void done() { if (nsolutions == 0) solcount.setText("No solutions!"); solcount.invalidate(); this.validate(); reshapeButton.setEnabled(true); solveButton.setEnabled(true); solver = null; } public static void main(String args[]) { PentoVision frame = new PentoVision(); frame.setVisible(true); } } class BoardChooser extends Dialog { private PentoVision parent; private Panel choices = new Panel(); private Panel buttons = new Panel(); private int choice = 0; public BoardChooser(PentoVision parent, int choice0) { super(parent, "Choose a board", true); this.parent = parent; this.choice = choice0; String menu[] = PentoSearch.menu; choices.setLayout(new GridLayout(menu.length, 1)); CheckboxGroup grp = new CheckboxGroup(); for (int i = 0; i < menu.length; i++) { final int ii = i; Checkbox cb = new Checkbox(menu[i], grp, i == choice); cb.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { choice = ii; } }); choices.add(cb); } addButton("Load", new ActionListener() { public void actionPerformed(ActionEvent e) { BoardChooser.this.parent.setShape(choice); BoardChooser.this.dispose(); } }); addButton("Cancel", new ActionListener() { public void actionPerformed(ActionEvent e) { BoardChooser.this.dispose(); } }); add(choices, "Center"); add(buttons, "South"); Point loc = parent.getLocation(); loc.translate(30, 30); setLocation(loc); pack(); } public void addButton(String label, ActionListener action) { Button b = new Button(label); if (action != null) b.addActionListener(action); buttons.add(b); } }