1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Putting an Image on your Java applications.

Discussion in 'Java' started by Systemerror, Jan 11, 2008.

  1. Systemerror

    Systemerror New Member

    Joined:
    Jan 11, 2008
    Messages:
    18
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    I often hear people asking on how to put an image on Java applications, thus here is an example of a painted panel that has an image in the same directory as the application *note* you cant use windows bmp with Java, but once you start creating your own images it's a good time to note that their are many "free image converters" available.

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PaintPanel extends JFrame
    {
     
     public PaintPanel()
     {
      super("painted panel example");
      setSize(640, 400);
      setResizable(false);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);                              //create frame
    
      Container contentArea = getContentPane();      //create container
    
      CustomPanel panel = new CustomPanel();
      contentArea.add(panel);
      setContentPane(contentArea);                   //add components
     }
    
      class CustomPanel extends JPanel
      {
       public void paintComponent (Graphics painter)
       {
    
        Image pic = 
              Toolkit.getDefaultToolkit().getImage("gui1.gif");
       if(pic != null) painter.drawImage(pic, 15, 5, this);     //create image
      
      }
    }
     
      public static void main(String [] args)
      {PaintPanel eg = new PaintPanel();}
    } 
    Hope i've helped.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  4. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
  5. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    i m new to java ,when i try this program it gives some errors ---exception handle what is this
     
  6. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
    Have you tried for run this program. It's not working in my system. May be some problem in my system
     
  7. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
    Have you tried for run this program. It's not working in my system. May be some problem in my system
     
  8. elec.shabnam

    elec.shabnam New Member

    Joined:
    Feb 13, 2008
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    its not working, exceptions are there
     
  9. elec.shabnam

    elec.shabnam New Member

    Joined:
    Feb 13, 2008
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    can you please recheck it and post it back again
     
  10. elec.shabnam

    elec.shabnam New Member

    Joined:
    Feb 13, 2008
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    i really need this , i want to learn java
     
  11. elec.shabnam

    elec.shabnam New Member

    Joined:
    Feb 13, 2008
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    please explain why the exceptions are coming
     
  12. Systemerror

    Systemerror New Member

    Joined:
    Jan 11, 2008
    Messages:
    18
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    Guys, i've just checked over the code, compiled and run it on three different machines so it works fine.
     
  13. geebee

    geebee New Member

    Joined:
    Apr 23, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Business Analyst
    Location:
    India
    This code is working all fine.
     
  14. tiwvinay

    tiwvinay New Member

    Joined:
    May 7, 2011
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    thnaks for give me a coding for submit a image.
     
  15. tiwvinay

    tiwvinay New Member

    Joined:
    May 7, 2011
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    thanks for give me a coding for putting a image.
     

Share This Page