Results 1 to 6 of 6
  1. #1
    David2010 is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default Applet won't start

    Ok I am kinda new to java and I am trying to load an image and display it to the screen. It compiles without errors but it refuses to load the image.
    Here is the code:

    Java Code:
    	import java.awt.Graphics;
    	import java.awt.Image;
    
    	public class MyPro extends java.applet.Applet
    
    {
    	Image img;
    	
    	public void init()
    		
    {
    	img = getImage(getCodeBase(), "Mario.bmp");	
    }
    
    	public void paint(Graphics g)
    		
    {
    	g.drawImage(img, 0, 0, this);	
    }
    
    }
    It just shows an empty screen without any errors.

    What am I doing wrong?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's probably not finding the image. If it were my money, I'd use a Swing JApplet rather than an AWT Applet, and I'd read the Sun applet tutorial which I believe will show you you how to do this:

    http://java.sun.com/docs/books/tutor...plet/data.html
    Last edited by Fubarable; 07-13-2009 at 07:49 PM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    img = getImage(getCodeBase(), "Mario.bmp");
    The getImage method will not read bmp images. Supported extensions are: gif, jpg and png.
    ImageIO will read bmp and wbmp image formats in j2se 1.5+
    Also, getImage returns immediately, ie, it doesn't block program execution to load the image data.
    To load the image data you need the help of a MediaTracker the api has sample code showing how to use it.
    MediaTracker also has methods you can use to inquire about the success/failure of the loading process. No exceptions are thrown if the image file is not found or if the data in it is unreadable/corrupted so you have to ask.
    This is another advantage of the newer ImageIO.read methods they throw (IllegalArgument) exceptions to let you know if the data was not successfully loaded.

  4. #4
    David2010 is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    The getImage method will not read bmp images. Supported extensions are: gif, jpg and png.
    Thanks I just changed the extension to jpg and it works great. xD

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hardwired -- the forum software won't let me "rep" you again, but thanks for catching the bmp issue.

  6. #6
    David2010 is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 03-20-2009, 10:04 PM
  2. [SOLVED] "start: applet not initialized"
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-24-2009, 02:10 AM
  3. how to start a Web Java applet without a webbrowser
    By racker79 in forum Java Applets
    Replies: 3
    Last Post: 09-22-2008, 06:30 PM
  4. How to start
    By sand.softnet1 in forum New To Java
    Replies: 5
    Last Post: 09-05-2008, 07:41 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •