Can't get my first applet to work

Discussion in 'Java' started by Michael Hesse, Jul 22, 2005.

  1. Hi,

    I am following an example from Chapter 1 of a book, and I created an HTML
    file and a class file.

    The HTML includes this line which pops up a window.

    <applet code = "first1.class" width = 300 height = 200>

    Then I get this error:

    java.lang.ClassFormatError: first1 (Bad magic number)

    Here is the contents first1.class (copied carefully from the book.)
    ==================================================
    import javax.swing.JApplet;
    import java.awt.Graphics;

    public class first1 extends JApplet
    {
    public void paint(Graphics g)
    {
    g.drawString("To climb a ladder, start at the bottom run", 20, 90);
    }
    }
    ==================================================
    Any ideas about what is causing the problem?

    Thanks,

    Michael
     
    Michael Hesse, Jul 22, 2005
    #1
    1. Advertisements

  2. Michael Hesse

    Eric Sosman Guest

    This looks like it should be "first1.java", from which
    the javac compiler will generate a "first1.class" file.
    Class files contain "binary garbage," not Java source code.
     
    Eric Sosman, Jul 22, 2005
    #2
    1. Advertisements

  3. The book wasn't clear about that.

    Thanks for helping me get started.
     
    Michael Hesse, Jul 22, 2005
    #3
  4. Andrew Thompson, Jul 22, 2005
    #4
  5. Michael Hesse

    Roedy Green Guest

    Normally that would look like this:

    <applet code="First1.class" width="300" height="200">

    The most crucial thing in classes begin with a capital letter.


    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 23, 2005
    #5
  6. Michael Hesse

    Roedy Green Guest

    I would try a different book. This guy is leading you to bad habits.

    the contents of First1.java should be:

    import javax.swing.JApplet;
    import java.awt.Graphics;

    public class First1 extends JApplet
    {
    public void paint(Graphics g)
    {
    g.drawString("To climb a ladder, start at the bottom rung.",
    20,90);
    }
    }

    then you compile tha with

    javac First1.java
    which creates

    First1.class which looks like gibberish, but it is what <applet wants.

    See http://mindprod.com/jgloss/gettingstarted.html

    Your book is skipping over the basics.



    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 23, 2005
    #6
  7. .

    This breaks the paint mechanism.
    Applet -> paint()
    JApplet -> paintComponent()

    [ See the GUI FAQ or further details. (q 5.9 of your version ) ]
     
    Andrew Thompson, Jul 23, 2005
    #7
  8. No, JApplet is not a JComponent subclass. It doesn't have a
    paintComponent() method (it has an unrelated paintComponents()).
    Then I would like to see a quote from the FAQ which states this. Because
    I can't remember that there is something like this in the FAQ :)

    /Thomas
     
    Thomas Weidenfeller, Jul 25, 2005
    #8
  9. Well that would make me (and pretty much everything else
    I wrote after that) - wrong*.

    My bad. Sorry for the misinformation.

    * Is not the first time - will not be the last.
     
    Andrew Thompson, Jul 25, 2005
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.