import java.awt.*;
public class TrivialApplication {

	public static void main(String args[]) {
		System.out.println( "Hello World!" );
		Experiment f= new Experiment();
		Experiment g= new Experiment();
		
		try {
			System.in.read(); // prevent console window from going away
		} catch (java.io.IOException e) {}
	}
}


// A square of the game Checkers
public class CheckersSquare extends Canvas {
static public final int EMPTY= 0; 	// Constants used to indicate
static public final int RED= 1; 	// what is on a square.
static public final int BLACK= 2; 	// Meaning: obvious

Rectangle b; 		// Containing rectangle of this square
int col;			// column number of this square on board
int row;			// row number of this square on board
private int fill= EMPTY;
				
// Constructor: An initially empty square that belongs in
// column c, row r of the board
public CheckersSquare(int c, int r) {
	super();
	col= c; row= r;
	if ((c+r)%2 == 1) setBackground(Color.gray);
	else setBackground(Color.pink);
	resize(42,42);
	b= bounds();
	repaint();
	}

// Paint the square (with its piece, if any), using Graphics g
public void paint (Graphics g) {
	Color save= g.getColor(); // Save the color, to be reset at end
	if (fill==RED) 	g.setColor(Color.red);
	else 			g.setColor(Color.black);
	g.fillOval(b.x+b.width/5, b.y+b.width/5,
		       b.width/2, b.height/2);
	g.setColor(save);
	}
}



// Frame to illustrate the awt.
public class Experiment extends Frame {
// Components that go in the frame
	CheckersSquare sq;
	Label lab;
    TextField textf;
	TextArea texta;
// Titles for buttons
	String[] buttons= {"new game", "quit"};
// The layout and constraint variables for this frame
	GridBagLayout gbl;
	GridBagConstraints gbc;

	final int xweight= 100; // Weights for all the components
	final int yweight= 100; // when they are resized

// Constructor: a frame with varioius components
public Experiment() {
	super("Experiment");
	gbl= new GridBagLayout();
	gbc= new GridBagConstraints();
	setFont(new Font("Dialog", Font.PLAIN, 10));
	setLayout(gbl);
	
	gbc.fill= gbc.BOTH;
	sq= new CheckersSquare(0,0);
	add(sq,gbl,gbc,0,0,1,1,xweight,yweight);

	gbc.fill= gbc.BOTH;
	lab= new Label("A label at (1,1)");
	add(lab, gbl,gbc,1,1,1,1,xweight,yweight);

    textf= new TextField("A TextField at (1,2)");
	add(textf,gbl,gbc,1,2,1,1,xweight,yweight);

	texta= new TextArea("A TextArea\nat (1,3)");
	add(texta,gbl,gbc,1,3,1,1,xweight,yweight);

	add(new Button(buttons[0]),gbl,gbc, 2,0, 1,1, xweight,yweight);
	
	add(new Button(buttons[1]),gbl,gbc, 3,0, 1,1, xweight,yweight);

	pack();
	move(150,50);
	show();
	setResizable(false);
	}

// Add component c to gbl with constraints gbc at position (x,y).
// Component c takes w columns and r rows, and is weighted (wx, wy).
private void add(Component c, GridBagLayout gbl,
				GridBagConstraints gbc,
				int x, int y, int w, int h, int wx, int wy)
	{gbc.gridx= x;
	gbc.gridy= y;
	gbc.gridwidth= w;
	gbc.gridheight= h;
	gbc.weightx= wx;
	gbc.weighty= wy;
	gbl.setConstraints(c, gbc);
	add(c);
	}
	
// If a button was pressed, process it; otherwise, let
// method action in the superclass process it.
public boolean action(Event e, Object arg) {
	if (arg.equals(buttons[0])) {
		// Handle press of "New game" and return true
			System.out.println("\"new game\" pressed"); 
			return true;
		}
	if (arg.equals(buttons[1])) {
		// Handle press of "Quit" and return true
			System.out.println("\"Quit\" pressed");
			System.out.println("TextField is: \"" + textf.getText() + "\"");
			System.out.println("selection is: \"" + textf.getSelectedText() + "\"");
			return true;
		}
	return super.action(e,arg);
	}

// Process press of WINDOW_DESTROY or mouse up in a CheckersSquare
public boolean handleEvent(Event e) {
	//System.out.println("" + e);
	if (e.id == Event.WINDOW_DESTROY) {
		System.out.println("Window destroy clicked");
		//dispose();
		return true;
		}
	if (e.id == Event.MOUSE_UP &&
		e.target instanceof CheckersSquare) {
		System.out.println("Mouse up in an CheckersSquare");
		return true;
		}
	if (e.id == Event.MOUSE_MOVE) {
		System.out.println("Mouse move");
		return true;
		}	
	return super.handleEvent(e);
	}
	

}

