GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes drawImage to Panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "drawImage to Panel" Watch "drawImage to Panel" New topic
Author

drawImage to Panel

Roy Tri
Greenhorn

Joined: Nov 13, 2001
Posts: 4
Hi All,
Can anyone give idea how to draw a jpeg or gif image to a panel ?
Thanks,
Roy
Martin webster
Greenhorn

Joined: Jun 20, 2001
Posts: 20
Hi,
One approach is to place a JLabel within the content of the panel and then set the icon on the label...
Another approach, which I prefer, is to override the paintComponent method...This is best used for creating a background image for your panels...
public class ImagePnl extends JPanel
{
ImageIcon ic = new ImageIcon("montage.jpg");
public ImagePnl()
{
}
public void paintComponent(Graphics g)
{
g.drawImage(ic.getImage() ,0,0,Color.red, null);
}
}
I use imageIcon waits until the image is loaded, which would occur during construction...

Martin
Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
class panel extends Panel
{
Image image;
public void panel()
{
image = Toolkit.getDefaultToolkit().getImage("gif/Image.gif"); //init image
}
public void paint(Graphics g)
{
g.drawImage(1,1,image,this);
}
}
Hope this helps. It is possible that there are some mistakes, I can't test it with a compiler.
Roy Tri
Greenhorn

Joined: Nov 13, 2001
Posts: 4
Thanks for Martin and Brain for the reply.
But I just can't implement your method, due to the following reasons :
1. In Martin's method : I can't use any of the swing component such as JPanel, for compatibility consideration (I actually develop an applet which contains panel with image on it).
2. In Brain's method : I can't use "this" keyword in drawImage(1,1,img,this) since it's neither an ImageObserver nor it's subclass.
Any further help would be greatly appreciated, thanks again for extending my knowledge

[This message has been edited by Roy Tri (edited November 14, 2001).]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can use "this" in the drawImage call... Panel implements ImageObserver...

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Roy Tri
Greenhorn

Joined: Nov 13, 2001
Posts: 4
Originally posted by Nathan Pruett:
You can use "this" in the drawImage call... Panel implements ImageObserver...

-Nate

Yes yes yes ... sorry about the confusion in my last email, Panel implements ImageObserver , but why is my code below doesn't work :
<code>
import java.awt.*;
public class MyPanel extends Panel {
Image img;
public MyPanel(){
img = Toolkit.getDefaultToolkit().getImage("myimg.jpg");
}
public void paint(Graphics g){
g.drawImage(1,1,img,this);
}
}
</code>
I got compilation error : method drawImage(int,int,java.awt.Image,MyPanel) not found in class java.awt.Graphics.
Is there anything wrong ?
Big Thanks,
Roy
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121





-Nate
Roy Tri
Greenhorn

Joined: Nov 13, 2001
Posts: 4
Gosh ... how could I do a silly mistake like that, Thanks Nate.
Originally posted by Nathan Pruett:
[B]



-Nate[/B]

 
GeeCON Prague 2014
 
subject: drawImage to Panel