[java] how do I show pictures in java?

Started by
8 comments, last by escudo825 18 years, 10 months ago
I'm trying to make a hangman game in my programming class with java. I've got it to work as a text thing. and I'm going to alter that to a bare class, and use it pretty much like a game engine. but I also think it would be easier to show pre-done pictures for the hangman stuff, than having to do abunch of draw-poly's.
Advertisement
Easiest is to load in via ImageIO, which can read png, gif and I think jpeg. Then you can just draw that onto a Graphics2D. A Canvas or Window is ideal for a generic drawing surface for doing your own stuff.
is this another class that someone made? if it is where can I find it?

note: I'm working in jdk 1.2.2 probably should have pointed that out earlier.
Do you have to use 1.2.2 for that class? I'm sure you do, else you would have d/l one of the newer versions. The class mentioned above comes with the java sdk, but I'm not sure with that early of a version. It seems like the early versions of java are a lot less suited for game development. Though I'm sure you will figure out how to make it work.

-Jake
ok.I'll just use draw poly for now. and download 1.4.2 (or what ever the newer one is) when this semester is over. but all I wanted to do was take pre-drawn hangman pictures. but I've looked in several help files and I haven't seen a good way to do it that isn't any less difficult than using the 'system.out' commands to do text :p . I'll just finish it and change it if I find a way to do what I want.
Yeah, ImageIO first appeared in Java 1.4 so its no use to you. I'd suggest you download the documentation for 1.2 from Sun's site and check for the Image class there. I've checked in some old tutorial and apparently in older versions of Java you could do this:
 Image alien = getImage(new URL("http://www.somewhere.com/images/alien.gif")); 
Check it out [wink]
ok. I will when I get home(I'm not on a computer that I can write java on, here at school). I'll give a report when I do so.
Well 1.2 is the first appearance of Swing so you would use the ImageIcon class to load the image.

like this:
ImageIcon pic = new ImageIcon("somepic.jpg");

and from the image icon in your paint(Graphics g) method
you would do something like:

g.drawImage(pic.getImage(),x,y,null);

if you wanted to do it using jdk 1.1 you'd have to do something like the following.

import java.awt.*;import java.awt.image.*;import java.applet.*;import java.net.*;public class WhatPic extends Applet implements ImageObserver {		Image img = null;		public void init() {		try {			img = this.getImage(new URL("http:&#47;&#<span class="java-number">47</span>;deepsleep.web-gratis.net&#<span class="java-number">47</span>;fantasy&#<span class="java-number">47</span>;dragon_pics&#<span class="java-number">47</span>;dragons.jpg"</span>) );<br>		}<span class="java-keyword">catch</span>( MalformedURLException mue ) {<br>			System.err.println( mue.getMessage()  );<br>		}<br>	}<br><br>	<span class="java-visibilitymodifier">public</span> <span class="java-primitives">void</span> paint(Graphics g) {<br>		g.drawImage(img,<span class="java-number">10</span>,<span class="java-number">10</span>,<span class="java-keyword">null</span>);<br>	}<br>	<br>	<span class="java-visibilitymodifier">public</span> <span class="java-primitives">boolean</span> imageUpdate(Image img,<span class="java-primitives">int</span> infoflags,<span class="java-primitives">int</span> x, <span class="java-primitives">int</span> y, <span class="java-primitives">int</span> width, <span class="java-primitives">int</span> height) {<br>		<span class="java-keyword">return</span> <span class="java-keyword">true</span>;<br>	}<br><br>}<br><br></pre></div><!--ENDSCRIPT--><br><br>It's always good to know how things used to work in Java cause you do come across comatibility issues and old programs. For instance, I had trouble loading applets with any version of Java past 1.1 so I converted back in order to work &#111;n my Applets and since Java is backwards compatible it works fine aside from some Deprecation Warnings which are the most worthless thing aside from telling the person who wrote the program that their using deprecated classes or methods.<br><br>I thought the pic was cool to.
One way I found to do that is to use the Toolkit class to retrieve an image, then draw it onto a canvas using drawImage:

public class imgLoad {Image img;     //private member objectToolkit tk;    //private member objectimgLoad(String filename) {    //constructor  tk = Toolkit.getDefaultToolkit();  img = tk.getImage(filename);}Image getImg() {  return img;}}//Then in main, etc.imgLoad il = new imgLoad("myimage.lol");Image image = il.getImg();g.drawImage(image,x,y,null);   //should probably go into the canvas.paint method
well the project is due today. I might not be able to make it an applet(wasn't required but I thought it would be nice). but after all the checks and stuff that it takes to protect stupid users the text versions code is about 6kb (200 lines), which I think is pretty big for just being a text program. if I can't make it an applet today though then I will eventually. then I'll work on making full blown games since I'll have the baisic knowledge needed.

This topic is closed to new replies.

Advertisement