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.
Go4Expert Founder
19Feb2008,12:45   #3
shabbir's Avatar
Voting for article of the month for January 2008
Ambitious contributor
20Feb2008,11:10   #4
imrantechi's Avatar
nice one
Ambitious contributor
20Feb2008,11:12   #5
imrantechi's Avatar
i m new to java ,when i try this program it gives some errors ---exception handle what is this
Ambitious contributor
20Feb2008,11:34   #6
debleena_doll2002's Avatar
Have you tried for run this program. It's not working in my system. May be some problem in my system
Ambitious contributor
20Feb2008,11:48   #7
debleena_doll2002's Avatar
Have you tried for run this program. It's not working in my system. May be some problem in my system
Ambitious contributor
20Feb2008,12:21   #8
elec.shabnam's Avatar
its not working, exceptions are there
Ambitious contributor
20Feb2008,12:22   #9
elec.shabnam's Avatar
can you please recheck it and post it back again
Ambitious contributor
20Feb2008,12:22   #10
elec.shabnam's Avatar
i really need this , i want to learn java