Sign in to follow this  
escudo825

[java] how do I show pictures in java?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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://deepsleep.web-gratis.net/fantasy/dragon_pics/dragons.jpg") );
}catch( MalformedURLException mue ) {
System.err.println( mue.getMessage() );
}
}

public void paint(Graphics g) {
g.drawImage(img,10,10,null);
}

public boolean imageUpdate(Image img,int infoflags,int x, int y, int width, int height) {
return true;
}

}



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 on 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.

I thought the pic was cool to.

Share this post


Link to post
Share on other sites
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 object
Toolkit tk; //private member object

imgLoad(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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this