Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 22508
122
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: 3248
30
  • 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: 43027
76
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic