How do I display a bmp on an AWT panel?

Posted on 2001-06-04
Medium Priority
Last Modified: 2013-11-19
How do I display a jpg on an AWT panel?
Question by:hmsjavateam
LVL 28

Expert Comment

ID: 6152733
Use A program such as MS Paint to convert to .gif or .jpg
then Java can accept it.

Expert Comment

ID: 6153983

You need to tell your program where to find the file.
I think that there are other image objects that you can use besides the icon object. It depends on what you are using.

Icon TrashIcon = new ImageIcon("c:\\TrashIcon.gif");

Then you add the Icon to the panel and set the size and location.


Expert Comment

ID: 6154963
Which one do you want to display, bmp or jpg?
Java can only display JPG and GIF files. As rrz said, you have to convert your bmp to jpg/gif first.

To display an jpg/gif image, you can do something like this in your public void paint method (assume you are in an applet)

Image myimg = getImage(getCodeBase(), "images/mypic.jpg");
public void paint(Graphics g)
   g.drawimage(myimg, x, y, this);
//where x, y are the coordinates on the applet


Author Comment

ID: 6156961
I need to know how do I display a jpg on an AWT panel running under an application (not an applet), or how to extend applet instead of panel, but load the pannel in an application.

Accepted Solution

rubenbouman earned 200 total points
ID: 6162706
the comment given by "Simon Say" is for an applet. This code is for an application:

Toolkit tk = Toolkit.getDefaultToolkit();
Image myimg = tk.getImage("c:/images/mypic.jpg");
public void paint(Graphics g)
  g.drawimage(myimg, x, y, this);
//where x, y are the coordinates on the panel

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question