FileDocCategorySizeDatePackage
GridImageCanvas.javaAPI DocExample4246Sun Mar 11 21:03:32 GMT 2001None

GridImageCanvas

public class GridImageCanvas extends JPanel
GridImageCanvas - a rectangular grid of images

Fields Summary
protected LayoutManager
lm
The LayoutManager. We provide it, not the user
protected Vector
vi
The list of Images
protected Vector
vs
The name of each Image
Panel
grid
The Panel, to manage the grid
Label
status
The label, for showStatus
Constructors Summary
GridImageCanvas()
Construct a GridImageCanvas


	    
	 
		setBackground(Color.red);
		grid = new Panel();
		status = new Label("Status here");
		setLayout(new BorderLayout());
		add(BorderLayout.CENTER, grid);
		add(BorderLayout.SOUTH, status);
	
Methods Summary
public java.awt.Componentadd(java.awt.Component c)
dummy add(), to ensure add is NOT called directly.

exception
java.lang.IllegalArgumentException This class does its own adding; use addImage() instead.

			throw new IllegalArgumentException("add not allowed here");
	
public voidaddImage(java.awt.Image i, java.lang.String s)

		vi.addElement(i);
		vs.addElement(s);
	
public voiddoLayout()
We do all the Layout setup here, then call Panel.doLayout()

		// doLayout called more than once?
		if (lm != null) {
			super.doLayout();
			return;
		}
		int l = vi.size();
		if (l == 0) {
			throw new IllegalArgumentException("doLayout before addImage");
		}
		if (l < 4) {
			throw new IllegalArgumentException("doLayout with <4 images");
		}
		double d = Math.sqrt((double)l);
		int w;
		if (d%1 != 0.0){
			w = ((int)d)+1;
		} else
			w = (int)d;
System.out.println("N="+l+";sqrt="+d+";gridLayout("+w+","+w+");");
		grid.setLayout(lm = new GridLayout(w, w));
		for (int i=0; i<l; i++) {
			ImageCanvas ic = new ImageCanvas((Image)vi.elementAt(i),
					(String)vs.elementAt(i));
			grid.add(ic);
		}
		super.doLayout();
	
public static voidmain(java.lang.String[] argv)
Main program to allow interactive use

		System.out.println("GridImageCanvas demo starting...");
		if (argv.length == 0)
			throw new IllegalArgumentException("Usage: GridImageCanvas image...");
		final Frame f = new Frame("GridImageCanvas");
		f.setLayout(new FlowLayout());
        f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				f.setVisible(false);
				f.dispose();
				System.exit(0);
			}
		});
		GridImageCanvas gic;
		f.add(gic = new GridImageCanvas());
		for (int i=0; i<argv.length; i++) {
			Image im = Toolkit.getDefaultToolkit().getImage(argv[i]);
			gic.addImage(im, argv[i]);
		}
		gic.doLayout();
		f.pack();
		f.show();
	
public voidshowStatus(java.lang.String s)

		status.setText(s);