public abstract class Widget { public abstract void accept(Visitor v); } public class TextBlock extends Widget { public String contents; public int fWidth; // average width of a character public int fHeight; // display height of a character public int bWidth; // width of the display area /** * @param contents text string contents of the block * @param blockWidth pixels width for layout * @param fontWidth pixels average width of the font to be used * @param fontHeight pixels height of the font to be used */ public TextBlock(String contents, int blockWidth, int fontWidth, int fontHeight){ this.contents = contents; fWidth = fontWidth; fHeight = fontHeight; bWidth = blockWidth; } public void accept(Visitor v) { v.visitTextBlock(this); } } import java.awt.*; public class FillerBlock extends Widget { public int preferredWidth; public int preferredHeight; public Color fillColour; /** * @param fillColour colour for filling * @param width of block in pixels * @param height of block in pixels */ public FillerBlock(Color fillColour, int width, int height){ preferredWidth = width; preferredHeight = height; this.fillColour = fillColour; } public void accept(Visitor v) { v.visitFillerBlock(this); } } import java.util.Vector; public class HorizontalContainer extends Widget { public Vector children; /* * construct empty horizontal container */ public HorizontalContainer() { children= new Vector(); } /* * add another child to the container */ public void add(Widget w){ // add another widget children.addElement(w); } public void accept(Visitor v){ v.visitHorizontalContainer(this); } } import java.util.Vector; public class VerticalContainer extends Widget { public Vector children; public VerticalContainer() { children= new Vector(); } public void add(Widget c){ children.addElement(c); } public void accept(Visitor v){ v.visitVerticalContainer(this); } } import java.awt.*; public class gowidget { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // test operation of the protoWidgets // construct an example // bottom up String mainContent = "Lorem Ipsum dummy text"; FillerBlock g = new FillerBlock(Color.RED,200,200); TextBlock a = new TextBlock("now is the time for a long story", 300,10, 10); FillerBlock h = new FillerBlock(Color.GREEN, 350, 100); VerticalContainer m = new VerticalContainer(); m.add(g); m.add(a); m.add(h); TextBlock r = new TextBlock(mainContent, 800, 12, 14); HorizontalContainer k = new HorizontalContainer(); k.add(m); k.add(r); // determine the width and height of the main frame k BoundingBox bbv = new BoundingBox(); k.accept(bbv); System.out.println("Bounding box for the whole container is [" + bbv.width + "x" + bbv.height+ "]"); // report bounds of constituents System.out.println("---------------------------------"); BoundingBox left = new BoundingBox(); m.accept(left); System.out.println("Bounding box for the left (vertical) container is [" + left.width + "x" + left.height+ "]"); BoundingBox right = new BoundingBox(); r.accept(right); System.out.println("Bounding box for the right textblock is [" + right.width + "x" + right.height+ "]"); } }