>
High Pointing
High Pointing Club
Our Summits
Biking
Thornville 8-10-03
Banquet 2-15-06
St Patrick ride
My Rides
Sunrise - Sunset
Other
Activities
ePentecost
Object Oriented
Example of Composite Design Pattern
Styles
default
Pitt Scheme
Penn State Scheme
OSU Scheme
Home


Example of Composite Design Pattern


UML


Source Code

Composite.java

import java.applet.*;
import java.awt.*;

public class Composite extends Applet {
    Picture pict = new Picture();

    public void init() {
	pict.add(new Circle(new Point(10,10),50));
	pict.add(new Rectangle(10,80,60,140));
	pict.add(new Line(10,150,60, 170));
	pict.add(new Circle(new Point(100,80),20));
    }
    // This method is called whenever the applet needs to be drawn or redrawn

    public void paint(Graphics g) {
	pict.draw(g);

    }
}


shape.java
import java.awt.*;

public abstract class shape extends Object {
    Color fill, outline, textcolor;  // The various colors we use
    public shape() {
	// Initialize color resources.  Note the use of the Color() constructor
	// and the use of pre-defined color constants.
	fill = new Color(200, 200, 200); // Equal red, green, and blue == gray
	outline = Color.blue;            // Same as new Color(0, 0, 255)
	textcolor = Color.red;           // Same as new Color(255, 0, 0)
    }

    public void setFillColor(Color c) {
	fill = c;
    }

    public void setOutlineColor(Color c) {
	outline = c;
    }

    public void setTextColor(Color c) {
	textcolor = c;
    }

    public abstract void draw(Graphics g);
}

Picture.java

import java.awt.*;
import java.util.*;

public class Picture extends shape { // ISA relationship
    Vector list = new Vector(); // HASA relationship

    public void add(shape s) {
	list.addElement(s);
    }

    public void draw(Graphics g) {
	Iterator it = list.iterator();
	while (it.hasNext()){
	    it.next().draw(g);
	}
		
    }
}

Circle.java
import java.awt.*;

public class Circle extends shape {
	int radius;
	Point center;

	public Circle(Point c,int r) {
		center = c;
		radius = r;
	}

	public void draw(Graphics g) {
		g.setColor(fill);
		g.fillOval(center.x, center.y, radius, radius);
		g.setColor(outline);
		g.drawOval(center.x, center.y, radius, radius);
	}
};


Line.java
import java.awt.*;

public class Line extends shape {
    Point start,end;

    public Line(int sx,int sy,int ex,int ey) {
	start = new Point(sx,sy);
	end = new Point(ex,ey);
    }

    public void draw(Graphics g) {
	g.setColor(outline);
	g.drawLine(start.x,start.y,end.x,end.y);
    }
}

Rectangle.java
import java.awt.*;

public class Rectangle extends shape {
    Point ul,lr;

    public Rectangle(Point u,Point l) {
	ul = u;
	lr = l;
    }

    public Rectangle(int x1,int y1,int x2, int y2) {
	ul = new Point(x1,y1);
	lr = new Point(x2,y2);
    }

    public void draw(Graphics g) {
	g.setColor(fill);
	g.fillRect(ul.x, ul.y, lr.x - ul.x, lr.y - ul.y);
	g.setColor(outline);
	g.drawRect(ul.x, ul.y, lr.x - ul.x, lr.y - ul.y);
    }
}


Point.java
public class Point {
     public int x;
     public int y;
     public Point() {
	x = y = 0;
     }
     public Point(Point p) {
	x = p.x;
	y = p.y;
     }
     public Point(int i,int j) {
	x = i;
	y = j;
     }
}

build.xml
<?xml version="1.0" ?>
<project default="main">

	<target name="main" depends="compile, compress">
		<echo>
			Building the .jar file
		</echo>
	</target>

	<target name="compile">
		<javac srcdir="."/>
	</target>

	<target name="compress">
		<jar jarfile="c2.jar" basedir="." includes="*.class" />
	</target>

</project>