jpg background on JPanel is rendered on top of scrollbars...  
Author Message
M.Kamermans





PostPosted: 2005-1-25 22:05:00 Top

java-programmer, jpg background on JPanel is rendered on top of scrollbars... I have a rudimentary gui with a class that extends JPanel, which sets its
content to a jpg image using:

if (im != null) { g.drawImage(im,0,0,null); }

in the paint() method. However, when I create a JScrollPane that uses this
JPanel extension as its scrollable client, and call a validate, the
scrollbars get visuall overwritten with the jpg image.

Has anyone encountered this error before and knows how to fix it?

Regards,

Mike Kamermans
 
M.Kamermans





PostPosted: 2005-1-25 22:37:00 Top

java-programmer >> jpg background on JPanel is rendered on top of scrollbars... As it turns out the scrollbars aren't so much drawn on top of, as more
that java does set the bounds for the panel based on the size of the
image that is loaded as background.

As full code example to illustrate what I'm using now:

----------------- start of code -----------------

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class UI extends JFrame{
private static final String FILENAME = "image.jpg";

public static void main(String args[]) { new UI().show();}
private static final int sizex = 200;
private static final int sizey = 200;

public UI(){
try {
Image im = ImageIO.read(new File(FILENAME));
ImagePanel panel = new ImagePanel(im);
JScrollPane pane = new JScrollPane(panel);
pane.setBounds(0,0,sizex,sizey);
getContentPane().add(pane);
this.setSize(sizex,sizey);
} catch (IOException e) { System.exit(1);}
}

public class ImagePanel extends JPanel {
private Image im;
public ImagePanel(Image im) { super(); this.im = im; }
public void paint(Graphics g) { if (im != null) { g.drawImage
(im,0,0,this); } }
public void setBounds() { setBounds(0,0,im.getWidth
(this),im.getHeight(this)); }
}
}

----------------- end of code -----------------

Is there some way to make the panel know the size of the image that works
(unlike this), so that the scrollpane knows it should add scrollbars and
can scroll over its client area?

regards,

Mike Kamermans