
import java.awt.*;
import java.awt.event.*;


public class BoardFrame  extends Frame {


  private class QuitListener  implements WindowListener {

    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
    public void windowOpened(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
  }
  
  
  private class NextButton  extends Button  implements ActionListener {
  
    public NextButton() {
    
      super("solve");
      addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e) {
    
      setLabel("next");
      boardView.solve();
    }
  }
  
  
  public static final int squareSize = 50;
  public static final int framePieceWidth = 20;
  public static final int buttonHeight = 30;
  
  private static final Image horizontal
    = ( Toolkit.getDefaultToolkit() ).getImage("images/horizontal.gif");
  private static final Image vertical
    = ( Toolkit.getDefaultToolkit() ).getImage("images/vertical.gif");
  
  
  private ChessboardView boardView = null;
  private NextButton solve = null;
  
  
  public BoardFrame(int size, String title, ChessboardView boardView) {
  
    this.boardView = boardView;
    
    int framePieceLength = size*squareSize + framePieceWidth;
    int frameSize = size * squareSize;
  
    setSize( frameSize + 2*framePieceWidth,
             frameSize + 2*framePieceWidth + buttonHeight );
    setTitle(title);
    setBackground(Color.lightGray);
    setLayout(null);
    
    WooddenItem frameTop = new WooddenItem(horizontal);
    WooddenItem frameBottom = new WooddenItem(horizontal);
    WooddenItem frameLeft = new WooddenItem(vertical);
    WooddenItem frameRight = new WooddenItem(vertical);
    
    frameTop.setBounds( 0, 0,
                       framePieceLength, framePieceWidth );
    frameBottom.setBounds( framePieceWidth, framePieceLength,
                           framePieceLength, framePieceWidth );
    frameLeft.setBounds( 0, framePieceWidth,
                         framePieceWidth, framePieceLength );
    frameRight.setBounds( framePieceLength, 0,
                          framePieceWidth, framePieceLength );
    add(frameTop);
    add(frameBottom);
    add(frameLeft);
    add(frameRight);
    
    solve = new NextButton();
    solve.setBounds( framePieceWidth, framePieceLength+framePieceWidth,
                     framePieceLength-framePieceWidth, buttonHeight );
    add(solve);

    boardView.setBounds( framePieceWidth, framePieceWidth,
                         frameSize, frameSize );
    add(boardView);
    
    addWindowListener(new QuitListener());
    setEnabled(true);
    setVisible(true);
  }
  
  
  public void reset() {
  
    solve.setLabel("solve");
  }

}
