• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

drawImage to Panel

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anyone give idea how to draw a jpeg or gif image to a panel ?
Thanks,
Roy
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




-Nate
 
Roy Tri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]


 
I found some pretty shells, some sea glass and this lovely tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic