
/*
  This applet demonstrates how to do your own layout.  The layout
  manager for the applet is set to null.  The setBounds() method
  of each component is called to set the size and position of the
  component.
  
  It is assumed that this applet is 330 pixels wide and 240 pixels high!
  If you want to deal with other sizes, you should implement the
  ComponentEvent interface, put the setBounds() commands,
  and compute the sizes and positions of the components in terms
  of the actual width and height of the applet.
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class NullLayoutDemo extends Applet implements ActionListener {

   SimpleCheckerboardCanvas board;  // A checkerboard.  This class is
                                    //   defined later in this file.
                                    
   Button resignButton;      // Two buttons.
   Button newGameButton;
   
   Label message;   // A label for displaying messages to the user.


   public void init() {
   
      setLayout(null);  // I will do the layout myself.
   
      setBackground(new Color(0,150,0));  // Dark green background.
      
      /* Create the components and add them to the applet.  If you
         don't add them to the applet, they won't appear, even if
         you set their bounds! */

      board = new SimpleCheckerboardCanvas();
      add(board);

      newGameButton = new Button("New Game");
      newGameButton.setBackground(Color.lightGray);
      newGameButton.addActionListener(this);
      add(newGameButton);

      resignButton = new Button("Resign");
      resignButton.setBackground(Color.lightGray);
      resignButton.addActionListener(this);
      add(resignButton);

      message = new Label("Click \"New Game\" to begin a game.", Label.CENTER);
      message.setForeground(Color.green);
      message.setFont(new Font("Serif", Font.BOLD, 14));
      add(message);
      
      /* Set the position and size of each component by calling
         its setBounds() method. */

      board.setBounds(20,20,164,164);
      newGameButton.setBounds(210, 60, 100, 30);
      resignButton.setBounds(210, 120, 100, 30);
      message.setBounds(0, 200, 330, 30);
   }
   
   public void actionPerformed(ActionEvent evt) {
          // In this demo applet, clicking on a button just
          // changes the displayed message.
      message.setText(evt.getActionCommand() + " button was pressed.");
   }

} // end class NullLayoutDemo



class SimpleCheckerboardCanvas extends Canvas {

     // This canvas displays a 160-by-160 checkerboard pattern with
     // a 2-pixel black border.  It is assumed that the size of the
     // canvas is set to exactly 164-by-164 pixels.

   public SimpleCheckerboardCanvas() {
      setBackground(Color.black);
   }
   
   public void update(Graphics g) {
         // The paint method redraws the entire canvas. Redefine update()
         // so it doesn't erase the canvas before calling paint();
      paint(g);
   }
   
   public void paint(Graphics g) {
        // Draw a 2-pixel black border around the edges of the board.
      g.setColor(Color.black);
      g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
      g.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
        // Draw  checkerboard pattern in gray and lightGray.
      for (int row = 0; row < 8; row++) {
         for (int col = 0; col < 8; col++) {
             if ( row % 2 == col % 2 )
                g.setColor(Color.lightGray);
             else
                g.setColor(Color.gray);
             g.fillRect(2 + col*20, 2 + row*20, 20, 20);
         }
      }
   }
   
   public Dimension getPreferredSize() {
      return new Dimension(164, 164);
   }

}  // end class SimpleCheckerboardCanvas
