Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Loading Images and Displaying them in NetBeans

  Asked By: Adelisa    Date: Aug 19    Category: Java    Views: 5041

This may seem a bit trivial, but it's been driving me nuts. I know
it should work, but it doesn't.

I've been reading "Learn Java In a Weekend". The author has a
picture displayed with his code, but for some reason, it's not
working when I build it using NetBeans.

First off, in NetBeans I put all my code in the java package named
FrameTest. In FrameTest, I have one java class called GUIFrame, and
a java main class called ImageTest. Ok, here's the code.

* GUIFrame.java
* Created on December 19, 2003, 9:53 AM

package FrameTest;
import java.awt.*;
import java.awt.event.*;

public class GUIFrame extends Frame implements WindowListener {

public GUIFrame(String title) {


/* Centers the Frame when setVisible(true) is called */
public void setVisible(boolean visible) {
if (visible) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getWidth())/2,
(d.height - getHeight())/2);

public void windowClosing(WindowEvent p1) {

public void windowDeactivated(WindowEvent p1) {}
public void windowClosed(WindowEvent p1) {}
public void windowDeiconified(WindowEvent p1) {}
public void windowOpened(WindowEvent p1) {}
public void windowIconified(WindowEvent p1) {}
public void windowActivated(WindowEvent p1) {}


* ImageTest.java
* Created on December 22, 2003, 10:47 AM

package FrameTest;
import java.awt.*;

* @author Student
public class ImageTest extends GUIFrame{

/** Creates a new instance of ImageTest */
public ImageTest() {
super("Image Test");
setSize(464, 288);

* @param args the command line arguments
public static void main(String[] args) {
new ImageTest().setVisible(true);

public void paint(Graphics g){
Image img = Toolkit.getDefaultToolkit().getImage
g.drawImage(img, 0, 0, 464, 288, this);

Here, I'm trying to load a file called FirstWorld.bmp. The file is
situated in my java package folder FrameTest. So its just in the
same folder as the GUIFrame and the ImageTest. Anyone see a problem
why the image cannot be displayed?



1 Answer Found

Answer #1    Answered By: Albert Ellis     Answered On: Aug 19

As far as I know, getImage() is supporting certain format only.
(Read the API). If you want to display the BMP format, I think
you need to get additional codec classes. (http://java.sun.

Try to change the bmp file to any JPG. It sure will work.

Didn't find what you were looking for? Find more on Loading Images and Displaying them in NetBeans Or get search suggestion and latest updates.