Archived

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

raziel725

Displaying pictures in Java

Recommended Posts

Here's the partial code:

    
public class Mine extends Applet {
Image in;
public void init() {
//load image from applets directory through the URL

in=loadImage(getDocumentSource()+"myimage.gif")
//put the image into mediatracker

MediaTracker t=new MediaTracker(this);
t.addImage(in,1);
try {
t.waitForAll();
}
catch (InterruptedException e) {
System.out.println("error "+e);
}
}

public void paint(Graphics g) {
g.drawImage(in,0,0,this);
}
}


The image can either be a 'gif' or a 'jpg'. You use MediaTracker to make sure the image has loaded before trying to display it. The paint() method will be called somewhat regularly and this is where you paint your image.

PS. You should check out The Java Tutorial. It is one of the most comprehensive tutorials I have ever seen and its free. I also includes this information that you are looking for. You can do it online, or you can download it and use it at your convenience. I would also recommend downloading the Java Platform docs. This will be a good reference, once you finish the tutorial.

---
Make it work.
Make it right.
Make it fast.

Edited by - CaptainJester on December 12, 2001 2:18:20 PM

Share this post


Link to post
Share on other sites