Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Applet Image Reading

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Applet Image Reading

    Hi! I'm new to these forums, and a little confused by some of the categories so please forgive me if this post is more suited to a different category.

    So I've been working on a platforming game for a while now, and when i run it inside eclipse (just using the little green play button) it runs just fine, but whenever i export it as a jar and try to run it in an html file it breaks. Sometimes i get an error saying that it cant find the class, and sometimes I just get a blank screen. I decided to test this with a pong game i made a year or so ago that worked just fine, but had no imported images in it, just simple graphics drawing (g.fillOval(x, y, 20, 20); kinda stuff). Well, when i replaced the ball with a small sprite, i found that the game would run just fine in eclipse, but when exported to a jar and run in an html file, all the simple drawing was fine, but the ball image wouldn't appear.

    I've tried a few different ways to import the image, but the way it is currently is as follows:

    To create the ball sprite:


    BufferedImage sprite;

    //Some Code omitted...

    try{
    sprite = ImageIO.read(new File("playerSprite.png"));
    }catch(Exception e){System.out.println("Oh No!");}


    and then the draw method is as follows:


    public void draw(Graphics g, ImageObserver imgOb){
    g.drawImage(sprite, x, y, 20, 20, imgOb);
    }


    and then the html code in a separate html file is:


    <applet code = "Pong.class" archive = "Pong2.jar" Width = 500 Height = 500>
    </applet>



    So just to clarify, when run from eclipse the ball appears just fine. When run in the html file, however, everything that is drawn NOT from an image shows correctly, only the ball does not; the ball is invisible. Any help would be appreciated. - Thanks!


    EDIT: Oh! I have the image inside a source folder i created inside the project.
    Last edited by 77times; May 29th, 2012 at 03:55 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: Applet Image Reading

    I don't believe this has much to do with your problem, but: If you plan on using this on a server, you cannot use File object. Instead, you have to use a URL to the image's location on the server.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Applet Image Reading

    Assuming the presence of a filesystem isn't great practice for portable Java. If you package your application in a Jar, you should distribute resources like images, sounds, etc along with it. The ClassLoader that loads your application classes offers support for getting access to bundled resources - read the API doc for ClassLoader.getResourceAsStream. Don't mistake resource paths for filesystem paths: they're not at all related (except sometimes by coincidence).

    Aussiemcgr's read(URL) suggestion should also work.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet Image Reading

    Oh, okay, i had no idea! I'll try that out and let you know if it works - no idea how to implement it yet but a simple google or two should do the trick. Thanks!

Similar Threads

  1. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  2. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 10:34 PM
  3. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  4. Replies: 2
    Last Post: February 14th, 2011, 04:36 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM