• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

drawImage to Panel

 
Roy Tri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can anyone give idea how to draw a jpeg or gif image to a panel ?
Thanks,
Roy
 
Martin webster
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use "this" in the drawImage call... Panel implements ImageObserver...

-Nate
 
Roy Tri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




-Nate
 
Roy Tri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gosh ... how could I do a silly mistake like that, Thanks Nate.
Originally posted by Nathan Pruett:
[B]



-Nate[/B]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic