Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Applet not displaying images

843789May 21 2010 — edited May 21 2010
Hey guys,

For my computer science class, I was suppose to make this code work. It runs, the applet opens, but the images aren't displayed. I've tried printing the image object, and it comes up with this (similar text for each image):
sun.awt.image.ToolkitImage@b8f8eb
The tutorials I've looked at don't help as they do basically the thing. Any help is appreciated.
package warmups;
// The "ImageExample" class.
import java.applet.*;
import java.awt.*;

public class ImageExample extends Applet
{
    // Place instance variables here
    private static int NIMAGES = 4;
    private Image image[] = new Image [4];
    private int imageNum = 0;

    public void init ()
    {
    	image [0] = getImage (getCodeBase (), "cartman0.jpg");
    	//Load images.  the getCodeBase() is a URL that Java looks for the image file. 
    	image [1] = getImage (getCodeBase (), "cartman1.gif");
    	image [2] = getImage (getCodeBase (), "cartman2.gif");
    	image [3] = getImage (getCodeBase (), "cartman3.gif");

    } // init method


    public void paint (Graphics g)
    {
    	g.drawImage (image [imageNum], 1, 1, this);
    	System.out.println (image[imageNum].toString ());
    	//the line above is how we draw the images.  The first parameter is an Image,
    	//the second two are co-ordinates.  The fourth is which applet the images will be shown  
		
    	imageNum = (imageNum + 1) % NIMAGES;
    	System.out.println (imageNum);
    	//this line of code cycles through the 4 loaded images.  How/why?
	
    	//catch IOException that the delay may throw 
    	try
    	{
    		Thread.sleep (300);
    	}
    	catch (InterruptedException ie)
    	{
    		System.out.println (ie.toString ());
    	}
    	repaint();
    	
    } // paint method
} // ImageExample class
Thanks,
Coh3n

Oh, as a side note, I didn't actually write this code, my teacher did. I was just told to modify it to make the images move across the screen, but that's tough considering I can't get them to display. :p

Edited by: Coh3n on May 21, 2010 7:20 AM

Edited by: Coh3n on May 21, 2010 7:20 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 18 2010
Added on May 21 2010
2 comments
313 views