This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display image

 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks it worked
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic