Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Punx

[java] Simple Java Graphics Problem...

This topic is 6082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This may seem stupid but for the entire day today I've been trying to draw a small image to an application using Java and I can't get it to work. The examples I have been learning from are all for applets and I'm wanting this for an application. If someone could please tell me how to draw a .gif to an application or better yet show an example I would be most thankful. Edited by - Punx on February 23, 2002 11:35:01 PM

Share this post


Link to post
Share on other sites
Advertisement
to load an image:

  
public Image loadImage(String fileName) {
Image loadedImage = null;
try {
InputStream in = this.getClass().getResourceAsStream(fileName);
ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
byte[] currentBytes = new byte[2048];
int bytesRead = 0;
while ((bytesRead = in.read(currentBytes)) != -1) {
imageByteStream.write(currentBytes,0,bytesRead);
}
loadedImage = Toolkit.getDefaultToolkit().createImage(imageByteStream.toByteArray());
}
catch (Exception e) {
System.out.println("Exception attempting to load image: "+fileName);
System.exit(0);
}
return loadedImage;
}


and then to draw it

  
public void paint(Graphics g) {
g.drawImage(theImage, x, y, null);
}

public void update(Graphics g) {
paint(g);
}

// note: you should use double buffering or else it''ll flicker alot


Share this post


Link to post
Share on other sites
Is there any advantage to doing it that way as opposed to:
Image i = Toolkit.getDefaultToolkit().createImage(String filename)?

(Also, I don''t know whether you meant you need a Canvas as in java.awt.Canvas, or as in something to draw on but just to point out that you can draw on a JPanel and possibly on any subclass of Component, though I haven''t checked that).

Enigma
------
...uncaring Father. ...

Share this post


Link to post
Share on other sites
quote:
Original post by Enigma
(Also, I don''t know whether you meant you need a Canvas as in java.awt.Canvas, or as in something to draw on but just to point out that you can draw on a JPanel and possibly on any subclass of Component, though I haven''t checked that).



Yeah, that''s right.

- Kaijin

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!