Can't get my first applet to work

M

Michael Hesse

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
 
E

Eric Sosman

Michael said:
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?

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.
 
R

Roedy Green

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

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
 
R

Roedy Green

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);
}
}

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
 
A

Andrew Thompson

.
the contents of First1.java should be: ....
public class First1 extends JApplet
{
public void paint(Graphics g)


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

[ See the GUI FAQ or further details. (q 5.9 of your version ) ]
 
T

Thomas Weidenfeller

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

No, JApplet is not a JComponent subclass. It doesn't have a
paintComponent() method (it has an unrelated paintComponents()).
[ See the GUI FAQ or further details. (q 5.9 of your version ) ]

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
 
A

Andrew Thompson

No, JApplet is not a JComponent subclass.
...D'Oh!

...It doesn't have a
paintComponent() method (it has an unrelated paintComponents()).

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top