
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class FigureGroup extends Figure {
    Vector _figures;
    int _width;
    int _height;

    public Vector figures() {
	return _figures;
    }

    void computeBoundingBox() {
	// Get bounding box
	Figure first = (Figure) _figures.get(0);
	int minX = first.getMinX();
	int maxX = first.getMaxX();
	int minY = first.getMinY();
	int maxY = first.getMaxY();
	
	for ( int i = 0; i < _figures.size(); i++ ) {
	    Figure f = (Figure) _figures.get( i );

	    if ( f.getMinX() < minX ) { 
		minX = f.getMinX();
	    }
	    if ( f.getMinY() < minY ) { 
		minY = f.getMinY();
	    }
	    if ( f.getMaxX() > maxX ) { 
		maxX = f.getMaxX();
	    }
	    if ( f.getMaxY() > maxY ) { 
		maxY = f.getMaxY();
	    }
	}

	_x0 = minX;
	_y0 = minY;
	_width = maxX - minX;
	_height = maxY - minY;
    }

    FigureGroup( Vector figures ) {
	super("Black", 0,0);

	_figures = figures;
	computeBoundingBox();
    }    

    // Set position of figure
    public void setPosition( int x0, int y0 ) {
	_x0 = x0;
	_y0 = y0;
    }
    
    public void resize( int x0, int y0, int x1, int y1 ) {
    }

    public void paint( Graphics g ) {

	for ( int i = _figures.size()-1; i >= 0; i-- ) {
	    Figure f = (Figure) _figures.get( i );
	    f.paint( g );
	}
    }

    public String toString() {
	String s = "FigureGroup|" + _figures.size() + "\n";
	for ( int i = 0; i < _figures.size(); i++ ) {
	    s += _figures.get(i).toString();
	}
	return s;
    }

    FigureGroup( StringTokenizer t, BufferedReader in, BoilerDraw bd ) 
	throws IOException 
    {
	super("Black", 0,0);

	Integer i = new Integer( t.nextToken() );
	int size = i.intValue();
	
	_figures = new Vector();

	// Read all figures
	for ( int j = 0; j < size; j++ ) {
	    String s = in.readLine();
	    _figures.add( bd.readOneFigure( s, in ));
	}

	computeBoundingBox();
    }

    // Return true if figure is selected using these coordinates
    public boolean isSelected( int x0, int y0 ) {
	for ( int i = 0; i < _figures.size(); i++ ) {
	    Figure f = (Figure) _figures.get( i );
	    if ( f.isSelected( x0, y0) ) {
		return true;
	    }
	}
	return false;
    }

    // Return true if figure is selected using these coordinates
    public boolean isInBox( int x0, int y0, int width, int height ) {
	if ( x0 <= _x0 && x0 + width >= _x0 + _width && 
	     y0 <= _y0 && y0 + height >= _y0 + _height ) {
	    return true;
	}
	else {
	    return false;
	}
    }

    public void paintSelected( Graphics g ) {
	g.setColor( Color.blue.brighter() );
	g.drawRect( _x0-2, _y0-2, 4, 4 );
	g.drawRect( _x0+_width-2, _y0+_height-2, 4, 4 );
	g.drawRect( _x0+_width-2, _y0-2, 4, 4 );
	g.drawRect( _x0-2, _y0+_height-2, 4, 4 );
    }
    
    public void changePosition( int dx, int dy ) {
	for ( int i = 0; i < _figures.size(); i++ ) {
	    Figure f = (Figure) _figures.get( i );
	    f.changePosition( dx, dy );
	}
	computeBoundingBox();
    }

    public int getMinX() {
	return _x0;
    }

    public int getMaxX() {
	return _x0 + _width;
    }

    public int getMinY() {
	return _y0;
    }

    public int getMaxY() {
	return _y0 + _height;
    }
}
