View Single Post
  #3 (permalink)  
Old 04-27-2008, 08:51 PM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
The JPanel will report its preferredSize to its parent container. You can use the setPreferredSize method _or_ override the getPreferredSize method to set the preferredSize of the panel. If the panel is added to a container whose layout manager respects the preferredSize of its children, ie, tries to display the children at their preferredSize, the panel should appear at the same size as the image.
In java this could look something like this:
Code:
class Pseudo extends JPanel { BufferedImage image; Dimension size = new Dimension(); Pseudo(BufferedImage image) { this.image = image; size.setSize(image.getWidth(), image.getHeight()); // Option 1: use setPreferredSize method. setPreferredSize(new Dimension(image.getWidth(), image.getHeight())); } protected void paintComponent(Graphics g) { super.paintComponent(g); // just in case g.drawImage(image,0,0,this); } /** Option 2: override getPreferredSize method. */ public Dimension getPreferredSize() { return size; } }
Reply With Quote