This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Display image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Display image" Watch "Display image" New topic
Author

Display image

Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
I am trying the following code to display an image but i am not able to do it.Please help,
class Images extends JPanel
{
public void paintComponent(Graphics g)
{
Image image=new ImageIcon("C:\\sara.jpg").getImage();
g.drawImage(image,20,20,this);
}
}
Now in main class
JPanel panel=new JPanel();
Images im=new Images();
panel.add(im);
frame.getContentPane().add(panel);
Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
well the complete code is:
import javax.swing.*;
import java.util.*;
import java.awt.*;


class Images extends JPanel
{
public void paintComponent(Graphics g)
{
Image image=new ImageIcon("C:\\sara.jpg").getImage();
g.drawImage(image,20,20,this);
}
}
class hell
{
public static void main(String aa[])
{
hell h=new hell();
h.go();
}
public void go()
{
JFrame frame=new JFrame("welcome");
JPanel panel=new JPanel();
Images im=new Images();
panel.add(im);
frame.getContentPane().add(panel);
frame.setSize(700,600);
frame.setVisible(true);
}
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
The easiest solution is to add your image to a JLabel and add the label to the GUI. Read the Swing tutorial for working examples.



Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
Ok,now i am able to display the image but when i create a jar file and execute it directly my image is not displayed again.Can some one tell me what must i change in this line to make it work:
Image image=new ImageIcon("C:\\sara.jpg").getImage();
Also,my jar file contains all the class files,the mainifest file and the image file in it.
Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
If you put your image file into the jar file, then you can access it using something like this:

It's also possible to put the image into a subdirectory inside of the jar file; in that case the parameter would look like "/images/sara.jpg".


Ping & DNS - my free Android networking tools app
Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
thanks it worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display image
 
Similar Threads
Icons
Applet with images overlayed and each image with a menu.
JScrollPane help
getImageIcon() : how does it work?
Adding an Image to JPanel using NetBeans IDE