Displaying pictures in Java

Started by
0 comments, last by raziel725 22 years, 4 months ago
Con someone give me some example code that will simply display and image in a Java applet?
Advertisement
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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement