I
icepac
Salutations everybody. I want to load an image into a panel. I have
sub-classed the JPanel class in order to be able to override the pain
method, like this :
public class ImageCanvas
extends JPanel {
Image image;
public ImageCanvas() {
super();
this.setBackground(Color.white);
}
public void paint(Graphics g) {
if (image!=null)
{
g.drawImage(image, 0, 0, this);
}
}
public void SetImage(Image img)
{
image = img;
int iW = Math.min(image.getWidth(this),this.getWidth());
int iH = Math.min(image.getHeight(this), this.getHeight());
this.setSize(iW>>1,iH>>1);
//this.setSize(image.getWidth(this)>>1,image.getHeight(this)>>1);
setVisible(true);
this.repaint();
}
Now I use this code in order to load my image into a normal jPanel :
(this is the snippet of code that executes loading the image :
//Display images
Image img =
Toolkit.getDefaultToolkit().createImage(strFilePath);//strFilePath of
course contains the path where the "image.png" is located
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try
{
tracker.waitForAll();
}
catch (InterruptedException exI)
{
System.out.println("Error while waiting tracker"+
exI.getMessage());
}
jCanvas.setMaximumSize(jPanelImg.getSize());
jCanvas.SetImage(img);
jPanelImg.revalidate(); //jPanelImg (type = JPanel) contains
jCanvas which is of type //ImageCanvas
jPanelImg.repaint();
However, when I use this program it loads the image all right but the
background is full of trash. Also if I try to load multiple images
then they all get jumbled together...
How can I clean this up ?
Thanx alot !
Pascal
sub-classed the JPanel class in order to be able to override the pain
method, like this :
public class ImageCanvas
extends JPanel {
Image image;
public ImageCanvas() {
super();
this.setBackground(Color.white);
}
public void paint(Graphics g) {
if (image!=null)
{
g.drawImage(image, 0, 0, this);
}
}
public void SetImage(Image img)
{
image = img;
int iW = Math.min(image.getWidth(this),this.getWidth());
int iH = Math.min(image.getHeight(this), this.getHeight());
this.setSize(iW>>1,iH>>1);
//this.setSize(image.getWidth(this)>>1,image.getHeight(this)>>1);
setVisible(true);
this.repaint();
}
Now I use this code in order to load my image into a normal jPanel :
(this is the snippet of code that executes loading the image :
//Display images
Image img =
Toolkit.getDefaultToolkit().createImage(strFilePath);//strFilePath of
course contains the path where the "image.png" is located
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try
{
tracker.waitForAll();
}
catch (InterruptedException exI)
{
System.out.println("Error while waiting tracker"+
exI.getMessage());
}
jCanvas.setMaximumSize(jPanelImg.getSize());
jCanvas.SetImage(img);
jPanelImg.revalidate(); //jPanelImg (type = JPanel) contains
jCanvas which is of type //ImageCanvas
jPanelImg.repaint();
However, when I use this program it loads the image all right but the
background is full of trash. Also if I try to load multiple images
then they all get jumbled together...
How can I clean this up ?
Thanx alot !
Pascal