• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Display image

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks it worked
 
What are you doing in my house? Get 'em tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic