Putting an Image on your Java applications.

Systemerror's Avatar author of Putting an Image on your Java applications.
This is an article on Putting an Image on your Java applications. in Java.
Rated 5.00 By 2 users
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: Java
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.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Voting for article of the month for January 2008
imrantechi's Avatar, Join Date: Feb 2008
Ambitious contributor
nice one
imrantechi's Avatar, Join Date: Feb 2008
Ambitious contributor
i m new to java ,when i try this program it gives some errors ---exception handle what is this
debleena_doll2002's Avatar
Ambitious contributor
Have you tried for run this program. It's not working in my system. May be some problem in my system
debleena_doll2002's Avatar
Ambitious contributor
Have you tried for run this program. It's not working in my system. May be some problem in my system
elec.shabnam's Avatar, Join Date: Feb 2008
Ambitious contributor
its not working, exceptions are there
elec.shabnam's Avatar, Join Date: Feb 2008
Ambitious contributor
can you please recheck it and post it back again
elec.shabnam's Avatar, Join Date: Feb 2008
Ambitious contributor
i really need this , i want to learn java