Can somone please tell me how to load an image file ( .jpg for example ) without extending the Applet class!!!! I know theres a way to get around this... Who has ideas?? Plz help...
Printable View
Can somone please tell me how to load an image file ( .jpg for example ) without extending the Applet class!!!! I know theres a way to get around this... Who has ideas?? Plz help...
I could only find this quote from the docs
And I could find the getImage/createImage(String) method of java.awt.Toolkit . But it is not static and Toolkit is abstract, so I don't know how to actually use it (besides Sun saying it should not be used directly).Quote:
The image must be obtained in a platform-specific manner.
I could find java.awt.Component.createImage, but those two functions take either a width and a height or an ImageProducer. No ImageProducer I found takes a filename as input.
I think either System or Toolkit has a getDefaultToolkit() method which is static. That is how to get a Toolkit instance.Quote:
Originally posted by CornedBee
And I could find the getImage/createImage(String) method of java.awt.Toolkit . But it is not static and Toolkit is abstract, so I don't know how to actually use it (besides Sun saying it should not be used directly).
:)
You could use javax.swing.ImageIcon to load the image, then use it's getImage method to get the image.
But that's not really great.
This procedure will load your image for you.
To display it on a Panel, you'll need to create a paint method for the panel similar to this.Code:private Image loadImage(String ImageName)
{
Image image = null;
Toolkit toolkit = Toolkit.getDefaultToolkit();
image = toolkit.getImage(ImageName);
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(image, 0);
try
{
mediaTracker.waitForID(0);
}
catch (InterruptedException ie)
{
System.err.println(ie);
System.exit(1);
}
return image;
}
Code:public void paint(Graphics graphics) {
super.paintComponents (graphics);
graphics.drawImage(loadImage("Hello.gif"), 140, 330, null);
I am absolutely going insane over this, I CANNOT load any images into my apps. I tried everthing, I get no errors, but nothing works.
I want to set the icon of my frame's window, I first tried this:
myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage( new File("./Images/Bomberman.gif").getCanonicalPath()));
It didn't work, then I tried the example that was last posted, still no such luck .... what the heck is going on??
Ok, I found this, and it seems to work just fine:
setIconImage(Toolkit.getDefaultToolkit().createImage(GameFrame.class.getResource("Images/icon.gif")));
Damn .... thats alot to load just one image!
You call one long line a lot for loading a gif-encoded image? You might want to try writing your own loading code.
Always those high-level programmers, hm, hm...
:D
The Toolkit.getDefaultToolkit() has many methods for opening images... However, I solved my problem by writing a class that loads images from an archived ( .zip ) file.