GridImageCanvaspublic class GridImageCanvas extends JPanel GridImageCanvas - a rectangular grid of images |
Fields Summary |
---|
protected LayoutManager | lmThe LayoutManager. We provide it, not the user | protected Vector | viThe list of Images | protected Vector | vsThe name of each Image | Panel | gridThe Panel, to manage the grid | Label | statusThe 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.Component | add(java.awt.Component c)dummy add(), to ensure add is NOT called directly.
throw new IllegalArgumentException("add not allowed here");
| public void | addImage(java.awt.Image i, java.lang.String s)
vi.addElement(i);
vs.addElement(s);
| public void | doLayout()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 void | main(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 void | showStatus(java.lang.String s)
status.setText(s);
|
|