// file: SubPanel.java
//
//import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Insets;
import java.awt.Container;

class SubPanel extends JPanel { 
    
    public GridBagLayout gridbag = new GridBagLayout();
    
    // constructor for class
    // sets size, background properties, layout manager
    //
    SubPanel() {
	
	this.setLayout(gridbag);
	this.setBackground(Color.lightGray);
    }
    
    // method: constrain
    //
    // parameters: 
    //     Container container: (input) container to place the component
    //     Component component: (input) the component to be constrained
    //     int grid_x: (input) horizontal position of component
    //     int grid_y: (input) vertical position of component
    //     int grid_width: (input) number of horizontal grid spots to take
    //     int grid_height: (input) number of vertical grid spots to take
    //     int fill: (input) directions to fill when component space is resized
    //     int anchor: (input) anchor position when component space is resized
    //     double weight_x: (input)relative amount of horizontal space component
    //                      should take when its space is resized
    //     double weight_y: (input) relative amount of vertical space component
    //                      should take when its space is resized
    //     int top: (input) amount of inset space on top
    //     int left: (input) amount of inset space on left
    //     int bottom: (input) amount of inset space on bottom
    //     int right: (input) amount of inset space on right
    // 
    // this method creates the constraints of a component in a container
    // and then adds the component to the container.  it is based on 
    // code from "Java in a Nutshell" by David Flanagan. published by
    // O'Reilly and Associates incorporated.
    //
    static public boolean constrain(JPanel container, JComponent component, 
				    int grid_x, int grid_y, int grid_width, 
				    int grid_height, int fill, int anchor, 
				    double weight_x, double weight_y, int top,
				    int left, int bottom, int right) {
	
	// set the constraints based on the input variables
	//
	GridBagConstraints c = new GridBagConstraints();
	c.gridx = grid_x;
	c.gridy = grid_y;
	c.gridwidth = grid_width;
	c.gridheight = grid_height;
	c.fill = fill;
	c.anchor = anchor;
	c.weightx = weight_x;
	c.weighty = weight_y;
	
	// if any of the buffer insets are greater than zero, create a new
	// Insets object
	//
	if (top + bottom + left + right > 0)
	    c.insets = new Insets(top, left, bottom, right);
	
	// set the constraints of the component
	//
	((GridBagLayout) container.getLayout()).setConstraints(component, c);
	
	
	// add the component to the container
	//
	container.add(component);
	
	// exit gracefully
	//
	return true;
	
    }
    
} // end class SubPanel
