[java] how do I show pictures in java?

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

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

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

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:&#47;&#47;deepsleep.web-gratis.net&#47;fantasy&#47;dragon_pics&#47;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 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 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

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.

1. 1
2. 2
3. 3
JoeJ
12
4. 4
5. 5

• 12
• 16
• 13
• 20
• 12
• Forum Statistics

• Total Topics
632176
• Total Posts
3004590

×