Skip to main content



      Home
Home » Newcomers » Newcomers » How To Load an Image?(Image Resource)
How To Load an Image? [message #504931] Wed, 23 December 2009 05:13 Go to next message
Eclipse UserFriend
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 Go to previous message
Eclipse UserFriend
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.
Previous Topic:Eclipse Galileo - EclipseLink - OC4J
Next Topic:Commands to link file system
Goto Forum:
  


Current Time: Sat Nov 08 04:26:44 EST 2025

Powered by FUDForum. Page generated in 0.08928 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top