Re: cannot load image
D.S.Whitworth@gmail.com wrote:
On Sep 18, 6:32 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
D.S.Whitwo...@gmail.com wrote:
I cannot get my program to load an image and display it. I have read
several forums and help sites and have used all the code regarding
mediatracker, but for some reason, it simply will not load my images.
I have also tried multiple combinations of how I insert the file
name. Below is the code I got from a website that seems like it
should work, but it won't. I have Windows Vista on my computer...is
it possible that is part of the problem? Below is the code I cannot
get to work that I got from a website. Any help you could give me
would be appreciated.
import java.awt.*;
import java.awt.event.*;
public class Viewer extends Frame {
private Image image;
public Viewer(String fileName) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
image = toolkit.getImage(fileName);
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(image, 0);
try {
mediaTracker.waitForAll();
} catch (InterruptedException ie) {
System.err.println(ie);
System.exit(1);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(image.getWidth(null), image.getHeight(null));
setTitle(fileName);
setVisible(true);
}
public void paint(Graphics graphics) {
graphics.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
new Viewer(args[0]);
}
}
D:
I don't see anything wrong with this and it works fine on my computer.
Check your image and make sure it doesn't have a problem.
I wouldn't use the MediaTracker, I would use the ImageIO class instead,
but this does work. Please see;
http://www.knutejohnson.com/
for some examples on how to load an view images with the three usual
methods.
You will find that it is easier if you draw the image on a component
other than the Frame/JFrame because when you set the size of frame it
includes the title bar and borders. So you will lose a small amount of
your image that way.
--
Knute Johnson
email s/nospam/knute/- Hide quoted text -
- Show quoted text -
I tried to use you're code but I didn't know how to use the URL to get
to a picture on my hard drive. Right now, I use Eclipse and the
pictures I'm trying to access are in a folder called "pics" which is
in the same "src" folder as all of my packages. I've tried different
combos of "pics/picture.jpg" and "picture.jpg" as a filename string,
but none of it seems to work. This is fristrating me because I've
gotten this to work before in another program, but for some reason,
using the same method doesn't work anymore. Here is how I changed
your code to use File instead of URL:
public class ImageIOExample extends JPanel {
BufferedImage image;
public ImageIOExample(String urlString) {
try {
File url = new File(urlString);
image = ImageIO.read(url);
setPreferredSize(new
Dimension(image.getWidth(),image.getHeight()));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawImage(image,0,0,null);
}
public static void main(final String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame("ImageIOExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIOExample iioe = new ImageIOExample("pics/
m20.gif");
f.add(iioe,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
It keeps saying it can't read input file. Please help.
Dan
I can't help you with Eclipse. I don't know anything about it.
As to the path for your files, from the command line the path is
relative to the current directory or if preceded by a / it is relative
to the root directory. Forward slashes work fine with windows so I
always suggest people use them if at all possible.
import java.io.*;
public class test {
public static void main(String[] args) throws Exception {
for (String fname : args) {
File file = new File(fname);
System.out.println(file.getCanonicalPath());
}
}
}
C:\Documents and Settings\Knute Johnson>java test .
C:\Documents and Settings\Knute Johnson
C:\Documents and Settings\Knute Johnson>java test ..
C:\Documents and Settings
C:\Documents and Settings\Knute Johnson>java test saturn.jpg
C:\Documents and Settings\Knute Johnson\saturn.jpg
C:\Documents and Settings\Knute Johnson>java test Desktop\lp.iso
C:\Documents and Settings\Knute Johnson\Desktop\lp.iso
C:\Documents and Settings\Knute Johnson>java test /favicon.ico
C:\favicon.ico
As to how you can possibly figure out what directory Eclipse is in,
write a simple program that creates the new File(".") and prints out its
canonicalPath.
A local to your hard drive file can be represented with a URL such as
file:saturn.jpg or file:/favicon.ico.
Why don't you copy my program as is from my website, remove the package
statement and compile it on the command line. Then try it with these
commands;
java ImageIOExample http://www.knutejohnson.com/images/saturn.jpg
and
java ImageIOExample file:the_path_to_your_image_file
If you are in /home/whitworth and the image is in
/home/whitworth/pics/m20.gif try
java ImageIOExample file:pics/m20.gif
--
Knute Johnson
email s/nospam/knute/