[java] Simple Java Graphics Problem...

Started by
5 comments, last by Punx 22 years, 1 month ago
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
~punx
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  
_______________________ http://mill.3dfxsweden.net
forgor to mention that you''ll need a Canvas to draw on
_______________________ http://mill.3dfxsweden.net
forgot to mention that you''ll need a Canvas to draw on
_______________________ http://mill.3dfxsweden.net
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. ...
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
Enigma: yah, it''ll work in an applet
_______________________ http://mill.3dfxsweden.net

This topic is closed to new replies.

Advertisement