Results 1 to 12 of 12
  1. #1
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default Sun's SimpleApplet

    Greetings -

    I have just attempted my "second" example java program.

    I just got "Hello World working

    I'm attempting the "SimpleApplet" from Sun - their tutorial.

    I'm using Netbeans.

    Here is the code:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Color;
    
    /**
     *
     * @author chuck
     */
    public class SimpleApplet extends Applet {
    
        /**
         * Initialization method that will be called after the applet is loaded
         * into the browser.
         */
        String text = "I'm a simple applet";
    
      public void init() {
    	text = "I'm a simple applet";
    	setBackground(Color.cyan);
      }
    
      public void start() {
            System.out.println("starting...");
      }
    
      public void stop() {
            System.out.println("stopping...");
      }
    
      public void destroy() {
            System.out.println("preparing to unload...");
      }
    
       public void paint(Graphics g){
    	System.out.println("Paint");
    	g.setColor(Color.blue);
    	g.drawRect(0, 0,
    		   getSize().width -1,
    		   getSize().height -1);
    	g.setColor(Color.red);
    	g.drawString(text, 15, 25);
       }
    
    
        // TODO overwrite start(), stop() and destroy() methods
    }
    And I created an HTML file:
    Java Code:
    <!--
    To change this template, choose Tools | Templates
    and open the template in the editor.
    -->
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
        <BODY>
            <APPLET CODE = Simple.class WIDTH = 200 HEIGHT = 100>
           </APPLET>
    
    
      </BODY>
    </HTML>
    I have compiled the JAVA program.

    When I open IE and then select "open" and browse to my new html file - all I get is a box with a red x in it.

    Can you help a newbie?

    Thanks
    -Sparky

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Make sure the .class file is in the same folder as the .html file. It might help to put quotes in:

    Java Code:
    <APPLET CODE="Simple.class" WIDTH="200" HEIGHT="100">
    </APPLET>

  3. #3
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    pbrockway2 - Thanks

    So far - no luck -

    the html is inthe same dir as the html file.

    I have tried putting quotes around the name and w/o.

    I do notice that when I open the html in NetBeans - NetBeans underlines <HTML>

    (and)
    <BODY>

    are underlined in red - reads "unexpected tag expecting one of <HEAD>

    Thanks
    -Sparky

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    @OP

    In case you're wondering about Darryl's post, a lot of people don't take too kindly to cross posts that don't actually announce, upfront, that they are cross posts. The reasons vary - mine is that I see these threads as discussions and it's very hard to participate in a discussion if you're only aware of half of it. Others point (quite validly) to the fact that someone responding can end up wasting their time answering a question that has already been answered.

    So, if you do post something in multiple places, please let everyone know that. And provide links for them to follow. (And recognise that some people still won't respond! But they will have less to complain about.)

    Darryl has basically done that for you. But when the problem is solved you should let the other forum know (preferably with a link to the solution, or a comment about what the solution was).

    -------------------------

    The <HEAD> business is basically a red herring. (But you can see how things can get confusing: once my attention was drawn to the html file I noticed that the one you posted at OTN was different...)

    I opened your html file (the one from this forum!) in Firefox. And I got "Error. Click for details" which I did. Leading (eventually) to the following in the Java console:

    Java Code:
    load: class Simple.class not found.
    java.lang.ClassNotFoundException: Simple.class
    	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: java.io.FileNotFoundException: C:\Documents and Settings\administrator.GHS\My Documents\NetBeansProjects\forum\src\Simple\class.class (The system cannot find the path specified)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    	at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    	at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    	at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    	at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	... 9 more
    Exception: java.lang.ClassNotFoundException: Simple.class

    So it's a class not found exception.

    ---------------------------------------

    For one thing your applet is called SimpleApplet, so it will end up associated with a class file SimpleApplet.class and not Simple.class. You should change things so that the names are consistent. Also (although I'm repeating myself) do make sure the .class and .html files are in the same folder: NB may put the .class file in quite a different place from its .java file.

  6. #6
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    JAVA Forums members -

    My apologies for performing a cross post. I did not know this was a no-no.

    I am as new as one can get to JAVA and it was recommended that I participate in forums to get help from mentors and a community of programmers.

    This forum was a high hit in a Google search and I went to Sun's page and found their community.

    (I am going to post this same apology at the other forum.)

    For future needs - are you saying it's ok to cross post as long as it is announced or do some prefer people stay "loyal" to one forum?

    Again - sorry

    Sparky

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I am not aware of anyone promoting "loyalty" here. Personally I think it's fine if your forthright about cross posting.

    ---------------------------------

    But the applet... Did you rename it (or change the name in the .html file)? What happened?

  8. #8
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    pbrockway2 -

    Well this morning I had some of it working - I was getting a colored box with the text in it.

    Just now I went to Netbeans to open it and run it again to update you and now I'm getting a box stating: "Project Simple Applet does not have a main class set. Set the main class: <No main classes found>

    So I have less working than before I posted. I don't think I changed anything since this morning -

    And if I recall in setting up the applet last night -I didn't need a main - is this correct?

    Can you help with this?

    (Once this is worked out) Also this morning I was getting the same thing as last night - then it asked for an update to something - I agreed and then it worked - it was something with IE / Chrome - I disabled Chrome here at work but I don't know if that's related because I don't have Chrome at home.

    Either way during the biggest part of today I have not chased this -

    I am now picking it back up - and need to get past this "no main" box

    Thanks
    Sparky

  9. #9
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    And if I recall in setting up the applet last night -I didn't need a main - is this correct?

    Yes - that's quite correct.

    In general NB adds its own complexity and most people advise not using any IDE until you are clear about how to compile and run simple programs from the command line and using simple plain text editors.

    From NB with the SimpleApplet.java source showing you right click and select "Run file". NB responds by displaying the applet in an applet viewer. (Ie it completely ignores the html file.)

  10. #10
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    hey pbrockway2 -

    Perhaps this is a bug (granted I'm 2 days into this so who am I to say)

    I have changed nothing since yesterday - at my work machine (Windows 7)

    I opened NetBeans

    Clicked Open Project - Simple Applet

    Click "Run"

    Click "Compile" - No response whatsoever

    Click "Run"

    Got the same "Box" stating no main found - only option is "OK" .

    Clicked "Run" again and I get a small box with the applet running in it - cyan colored box with the text, "I am a simple applet" in it.

    ????

    -Sparky

  11. #11
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    OK -

    I'm an IDIOT!!!

    the green arrow is "run project" - which gives the prompt box regardnig "main"

    within the run menu is "run file" which runs the applet (w/o IE as you said)

    thanks for helping the new guy!!

  12. #12
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2010, 11:49 AM
  2. problem from sun's java to eclipse
    By doood123 in forum Eclipse
    Replies: 0
    Last Post: 12-18-2007, 05:38 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
  •