| How To Load an Image? [message #504931] |
Wed, 23 December 2009 05:13  |
Eclipse User |
|
|
|
HI,
I have imported an image of type .png to the source folder, but I can't get it to load.
Image is here
src
resource
image.png
the image, resource folder and .java are in the same package
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
* This class demonstrates how to load an Image from an external file
*/
public class LoadImageApp extends Component {
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public LoadImageApp() {
try {
[B] img = ImageIO.read(new File("/src/res/clockface.png"));[/B]
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
}
}
|
|
|
| Re: How To Load an Image? [message #504988 is a reply to message #504931] |
Thu, 24 December 2009 00:36  |
Eclipse User |
|
|
|
waski2@telus.net wrote:
> HI,
>
> I have imported an image of type .png to the source folder, but I can't
> get it to load.
>
> Image is here
>
> src
> resource
> image.png
>
> the image, resource folder and .java are in the same package
>
I'm not sure what your question has to do with Eclipse?? Maybe you should be
posting it to a Java discussion forum.
One thing I noticed is that you are trying to load the file
"/src/res/clockface.png". Is that really what you mean? Having a directory
called "src" at the root of your filesystem seems very unusual.
|
|
|
Powered by
FUDForum. Page generated in 0.08928 seconds