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) {
super(title);
setBackground(SystemColor.control);
addWindowListener(this);
}
/* 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);
}
super.setVisible(visible);
}
public void windowClosing(WindowEvent p1) {
dispose();
System.exit(0);
}
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);
setBackground(Color.white);
}
/**
* @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
("FirstWorld.bmp");
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?