|
-
Nov 3rd, 2002, 11:39 PM
#1
Thread Starter
Addicted Member
HELP!!! Image Class
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...
To protect time is to protect everything...
-
Nov 4th, 2002, 01:37 PM
#2
I could only find this quote from the docs
The image must be obtained in a platform-specific manner.
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).
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 4th, 2002, 01:41 PM
#3
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).
I think either System or Toolkit has a getDefaultToolkit() method which is static. That is how to get a Toolkit instance.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 4th, 2002, 01:42 PM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 4th, 2002, 04:28 PM
#5
Hyperactive Member
This procedure will load your image for you.
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;
}
To display it on a Panel, you'll need to create a paint method for the panel similar to this.
Code:
public void paint(Graphics graphics) {
super.paintComponents (graphics);
graphics.drawImage(loadImage("Hello.gif"), 140, 330, null);
-
Jan 30th, 2003, 04:42 PM
#6
Member
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??
-
Jan 30th, 2003, 04:59 PM
#7
Member
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!
-
Jan 30th, 2003, 06:20 PM
#8
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...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 31st, 2003, 06:31 PM
#9
Thread Starter
Addicted Member
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.
To protect time is to protect everything...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|