sharon
Dec 4th, 2000, 07:45 AM
How do i add an image to the NORTH border of a border layout
According to my findings:
Strangely, when you load an Image, a call to get the Height or Width of this Image claims that the size is not yet known (that doesn't make sense to me). But as best I can tell, the call to either getWidth or getHeight is required anyway and the ImageObserver will be notified after this call (getWidth or getHeight) and a call to pack(). If you know the size of the image, you might want to use setSize, but I prefer to have Java figure it out (not to be hard-coded).
After the first call to pack(), the methods of class Image (getWidth and getHeight) "know" the size of the image. Then I set the preferred size of my panel so I only use required space and BorderLayout can default to the correct "PreferredSize". java.awt.Panel doesn't allow me to set the preferred size, so I used javax.swing.JPanel which does.
I thought the only required part to this post would be about overridding the paint method of Panel or JPanel, but the above issues came up.
import java.awt.*;
import javax.swing.*;
public class AnImageViewer extends Frame{
private Image anImage;
private MyPanel northPanel;
public AnImageViewer(){
setTitle("An Image Viewer");
//Substitute the path to your image.
//You have to 'escape' the file separator '\' with another '\'.
anImage = Toolkit.getDefaultToolkit().getImage("..\\chess\\chessmen\\Wking.gif");
//Create my special panel.
northPanel = new MyPanel(anImage);
//Adding to NORTH border of a border layout of class AnImageViewer which is itself a class Frame
add(northPanel, BorderLayout.NORTH);
//Added to show different positions of the BorderLayout
add(new Button("South"), BorderLayout.SOUTH);
add(new Button("East"), BorderLayout.EAST);
add(new Button("West"), BorderLayout.WEST);
add(new Button("Center"), BorderLayout.CENTER);
//LVALUE not needed, just the call. During testing I used
//System.out.println(northPanel.image.getWidth(northPanel));
northPanel.image.getWidth(northPanel);
//I suppose this triggers the notification to northPanel.
pack();
//Now I can set the required preferredSize of this panel.
northPanel.setMyPreferredSize(new Dimension(northPanel.image.getWidth(northPanel),
northPanel.image.getHeight(northPanel)));
//Now I really get the correct sizing or packing.
pack();
show();
}
public static void main(String[] args){
new AnImageViewer();
}
}
//I created my own (J)Panel to override the paint method.
//I used JPanel instead of Panel so that I can setPreferredSize.
//Panel does not allow this.
class MyPanel extends JPanel{
//Could make private and use setter/getter methods above.
public Image image;
public MyPanel(Image i){
image = i;
}
//class Image does not tell us its size until after pack() in the AnImageViewer constructor.
//The JPanel (MyPanel) need only be this large.
public void setMyPreferredSize(Dimension d){
setPreferredSize(d);
}
//Here is the only piece of code I thought you'd need,
//but this turned into a project about finding the right size.
public void paint(Graphics g){
g.drawImage(image, 0, 0, this);
}
}
You can use Ctrl-C from the DOS shell to exit
[Edited by VirtuallyVB on 12-04-2000 at 06:21 PM]