can't get images to load in applet

Discussion in 'Mac Programming' started by dancks, Aug 9, 2011.

  1. dancks macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #1
    I tried everything. I ask this question on Yahoo! Answers and I got basically what I saw everywhere else:
    http://answers.yahoo.com/question/i...pnNOlGXsy6IX;_ylv=3?qid=20110807164512AAM4c68

    in quotes:
    answer:

    I got his Year.java file to work but not the HelloApplet.java and HelloWord.html

    It says The page “Hello-World Applet” attempted to load Java content, but Java is unavailable or not installed.

    On my mom's iMac the page did appear to laod correctly but all that appeared was a black box.

    I'm guessing the problem is a uninstalled java something or other but I have no idea what that would be since I have a lot of other java programs that work on my computer just not this kind of thing.

    I'm using 10.5 and the highest version java available for my mac is 1.4.3.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    What browser was running when the message appeared?

    Was Java enabled in the browser? It's often a separately enabled and disabled feature.
    The location of the control depends on which browser.

    Screen shot? Which iMac model? Which OS version?

    Give a specific example. Name one of the "other java programs" on your computer.

    What model is your computer? Is it 32-bit or 64-bit?

    I have a Mac mini with 10.5.8 on it, and it has Java 1.5. I think it was around 10.5.2 that the first Java 5 release came out for it. I also think it required a manual download and install at the time. Software Update would NOT see it or its subsequent updates until AFTER the first manual install. I could be misremembering, however.


    First, identify exactly which version of Leopard you have, on which machine. If it's not 10.5.8, update to that first. If you have a reason not to update to 10.5.8, please explain that reason.

    Run the "Java Preferences.app" program, which resides in the Utilities folder. Post what it shows for the available applet and application Java versions, under the General tab.

    Next, run Software Update and post the exact list of available updates it shows you. Leave nothing out.

    Then run Terminal.app and paste this line into a window:
    Code:
    java -version;  java -d64 -version
    
    Copy and paste the output and post it.


    To find the manually downloadable Java updates, go here:
    http://support.apple.com/downloads/

    Enter the following keywords into the keywords box.
    Code:
    java update 10.5
    
    Press return to search.

    You should see the following Java updates for 10.5:
    Java for Mac OS X 10.5 Update 4
    Java for Mac OS X 10.5 Update 5
    Java for Mac OS X 10.5 Update 10

    Download and install Update 4. Then run "Java Preferences.". Post what it shows for the available applet and application Java versions, under the General tab.

    After Update 4 is installed and confirmed in "Java Preferences", rerun Software Update, and see if it sees an available Java update. If so, have it download and install it. Repeat this (with any intervening restarts) until after Update 10 has been successfully installed.

    If you don't see Java 5 listed in "Java Preferences" after installing Update 4, post again. If Software Update sees no updates, post again. If it doesn't update all the way to Update 10, post again.

    URLs:
    Java for 10.5, update 4: http://support.apple.com/kb/DL848
    Java for 10.5, update 5: http://support.apple.com/kb/DL924
    Java for 10.5, update 10: http://support.apple.com/kb/DL1359
     
  3. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #3
    ok here are the requested pics:

    iMac 10.6.8:
    Screen shot 2011-08-10 at 10.01.16 PM.png

    macbook(mine): 10.5.8
    Picture 1.png

    downloaded updates 8, 9, 10. 9 didn't need to be installed because a newer version existed apparently.

    the result:
    Picture 2.png


    and yes java is enabled for safari:
    Picture 3.png

    So we got the black box to show up, so one mystery solved, although I could've sworn that didn't happen right after I downloaded the last update. Maybe I needed to restart the computer. I'm gonna download the safari update and see if that changes anything and I'll post back.

    As far as the other java stuff I meant java examples in this book I bought where it had java gifs show up in a JFrame.
     
  4. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #4
    no it didn't change anything. Its exactly like the picture right above this post
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    I downloaded the mediafire examples, and the applet example is not going to work. Period.
    It may have worked on some platforms at some time in the past, but it's not going to work on Mac OS X now. Period.

    The reason it won't work is because it naively assumes it has access to the file-system. This is not allowed except for signed applets, and it isn't signed. Signing requires a jar-file, and it isn't in a jar-file.

    One approach to loading images in an applet uses a MediaTracker, but only if the image is stored as a separate file on the HTTP server or the file-system. There is no MediaTracker in the example.

    An alternative to a MediaTracker is to package images in a jar with the compiled classes. Then the classes can load the image from the jar without a MediaTracker. As already noted, the example has no jar.

    So basically, the example applet has no possible chance of working as given.


    Here's an example Java applet that use MediaTracker:
    http://www.realapplets.com/tutorial/ImageExample.html

    I found it by googling: java applet image example

    Here's another example, found by googling: java getimage
    http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html


    Both MediaTracker and jar-bundled image resources should be explained in any competent book or tutorial. What book or tutorial are you learning from, that it hasn't explained these? Be specific. Give title and author, or the URL of an online tutorial.

    Also, give the publication date of the book, or the last date an online tutorial was updated. Things have changed a lot since the early days of Java, so it could be you're just using an outdated learning resource.


    What is the title and author of the book? Is this the book you're using to learn how to write applets that load images?
     
  6. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #6
    The book was Java: Software Solutions by Lewis and Loftus. Its actually a really basic book and it didn't go this far in depth it just showed various java.awt functions but not applet. So I've been searching the internet for code examples to learn from. I think I prefer to learn that way instead of reading a book but if you got a good title I'll check it out. I'll check the O'Reilly series for something I guess.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    It should be easy to find examples of applets. Or tutorials on applets. Example search terms:
    java tutorial
    java applet tutorial

    If you have problems finding examples, post the search terms you're using.


    You can download all the examples from "Java Examples in a Nutshell". There are 5 or 6 different editions of the book, as it evolved over time and different Java versions. Find the edition you want, and download the code.
    http://oreilly.com/oreilly/cs/examples-faq.html

    You can probably find a used paper-and-ink copy of "Java in a Nutshell", which is the companion explanation for "Java Examples in a Nutshell". It's also a fairly common book to find at your local public library. Or at a university, high school, or community college library.


    A simple search tells me there are at least 6 editions of "Java: Software Solutions by Lewis and Loftus". Be more specific about exactly which book you have. Accuracy and specificity is important in programming.
     
  8. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #8
    I tried the Media Tracker example you provided. It doesn't work. Here's the java console:
    I looked at the code it looks almost exactly like the other stuff I tried. I'm telling you Its gotta be the java version or the way java is set up on my machine. I just need a hint at what to check out to get things going. Is there a good "java on mac" tutorial or book or something out there?

    I don't want to be this to be a "solve all of my problems" thread but I'm really over my head here.
     
  9. chown33, Aug 12, 2011
    Last edited: Aug 12, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    This part of the error:
    Code:
    java.lang.UnsupportedClassVersionError: Bad version number in .class file
    
    tells me it's a class version-number problem. This usually indicates a Java version is older than the class it's trying to run. There can be other causes, but that's the most likely.

    So the next step is to determine exactly what version of Java is running in your browser. If you saw a "Java Console" window in your browser, when the error trace was produced, it should identify the Java version your browser is using. Look there and post it.

    If you don't see a Java Console window, enable it with:
    Java Preferences.app
    Advanced tab
    Java Console item in list
    Show console radio button
    Also, please identify exactly which browser you're using, and which version. Be specific.

    Then, identify if the browser is running in 32-bit or 64-bit. You can do that using "Activity Monitor.app". The row will show your browser name, and the column labeled "Kind" will show 64 bit or not.

    Finally, use Java Preferences to rearrange the order of Java versions so 1.5 64-bit is above 1.6 64-bit. Or disable 1.6 64-bit and 1.4 32-bit.

    EDIT
    I uploaded a simple Java applet that identifies the Java version and OS version currently in use. It's compiled for Java 1.2 or later, so it should work on any Java version that was ever released on any Mac OS X version.

    I may not leave it up there for long, so try it while you can.
     
  10. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #10
    Im using safari

    and:

    Picture 2.png

    Picture 1.png

    Picture 3.png

    so I'm guessing from what I see there its 32 bit, since only java preferences is listed as 64 bit and in parenthesis.

    and:

    Picture 4.png

    So I think thats what you wanted me to do but its still giving me the same result.
     
  11. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #11
    I tried it on the iMac. Couldn't get it to work either. I wonder if it might have something to do with the applet code and the way java is being used or something? I used a straightforward applet code <applet code:"myapplet.class ... that kind of thing. And I do get a little java cup to show up. On the iMac when it says error nothing appears in the java console, unlike my macbook shown above.

    Also, I'm keeping this thread alive until I solve the problem. Didn't mean to double-post though.
     
  12. rdo macrumors newbie

    Joined:
    May 24, 2011
    #12
    Hi. If you are running an applet (not a stand alone app) then you need to run it using a web server... according to your safari path, it appears that you are trying to access it directly from the hard drive. (ie. file://)

    After setting up a web server you would access it like:
    http://localhost/myapp.html
     
  13. rdo, Aug 15, 2011
    Last edited: Aug 15, 2011

    rdo macrumors newbie

    Joined:
    May 24, 2011
    #13
    Aww, my bad... when I open the html via the browser it does run correctly after all...

    I used this code in my html:
    <code>
    <APPLET archive="TheApp.jar" code="theApp.MainClass.class" width=440 height=350>
    </code>

    sorry to lead you down a wrong path, haven't used java in a while.
     
  14. rdo macrumors newbie

    Joined:
    May 24, 2011
    #14
    Modified the java code because I was also getting a black screen like you had from the code you specified in the dl link:

    Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.*;
    
    //I think you need to go to the Advanced tab of Internet Options
    // and enable "Allow Active Content to run in files on my computer"
     
    
    public class HelloApplet extends Applet
    {
        public void paint (Graphics g)
        {
    		try
    		{
    			setBackground (Color.black);
    
    			MediaTracker mediaTracker = new MediaTracker(this);
    
    			String s = String.format("year.jpg");
    			Image image = getImage(getCodeBase(), s);
    			mediaTracker.addImage(image, 0);
    			mediaTracker.waitForAll();
    
    			g.drawImage(image, 60, 70, null);
    		}
    		catch(Exception ex)
    		{
    		}
    		/*
    		Image pic = Toolkit.getDefaultToolkit().getImage("year.jpg");
    		if (pic != null)
    		{
    	    	g.drawImage (pic, 60, 70, this);
    		}
    		*/
        }
    }
    
    html
    Code:
    <html>
    <head>
      <title>Hello-World Applet</title>
    </head>
    <body>
      <p>Applet to display a picture</p>
      <applet code="HelloApplet.class" width="400" height="300">
      </applet>
    </body>
    </html>
    
     

    Attached Files:

    • year.jpg
      year.jpg
      File size:
      24.3 KB
      Views:
      786
  15. dancks, Aug 15, 2011
    Last edited: Aug 15, 2011

    dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #15
    hey it worked. Thanks RDO!

    well, it worked on the iMac 10.6, not my macbook 10.5
     
  16. rdo macrumors newbie

    Joined:
    May 24, 2011
    #16
    I don't know what else could be the problem then...
    You followed chown33's advice on the 10.5 pc too, right?

    You might try clearing the cache and then reloading the applet...
     
  17. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #17
    I did everything he told me to do. I'm starting school in less than a week I'll ask some of the smart people hanging around there what I'm doing wrong. I'm sure they'll have a good laugh at my expense first but its all worth it. I'll look around and try different stuff when I finally have it figured out I'll post the solution
     
  18. dancks thread starter macrumors member

    dancks

    Joined:
    Nov 8, 2009
    #18
    I put the image on my desktop on the 10.5 and it worked like a charm. The dumbass award goes to yours truly.

    I'm just curious why that would matter. Would it only be because that's where I saved the java code?
     
  19. rdo macrumors newbie

    Joined:
    May 24, 2011
    #19
    lol. it happens sometimes...

    yes, wherever the compiled applet is located; getCodeBase() -
    Gets the base URL. This is the URL of the directory which contains this applet.
     

Share This Page