Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Applet code from Sam's book doesn't work

2 views
Skip to first unread message

Murthy Gandikota

unread,
Feb 5, 2003, 5:15:47 PM2/5/03
to
I am attaching a piece of code from Sams Teach Yourself Java 2 By
Laura Lemay and Rogers Cadenhead. I get no compilation errors. When I
run the applet in a browser I get the error

java.lang.NoClassDefFoundError: PetePanel

PetePanel is defined in the code below!! By the way I run the applet
in a microsoft browser <applet code="PeteApplet.class" height=150
width=345> </applet>

All the GIF files called by the applet are available. Thanks a bunch
for your help.

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class PeteApplet extends JApplet {
PetePanel pete = new PetePanel();

public void init() {
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 1, 15, 15));
pane.add(pete);
setContentPane(pane);
}
}

class PetePanel extends JPanel implements Runnable {
Thread runner;
Image petePics[] = new Image[6];
Image back;
int current = 0;
int x = -10;
int y = 30;

PetePanel() {
super();
setBackground(Color.black);
String peteSrc[] = { "right1.gif", "right2.gif",
"right3.gif", "stop.gif", "blink.gif",
"wave.gif" };
Toolkit kit = Toolkit.getDefaultToolkit();
for (int i=0; i < petePics.length; i++) {
petePics = kit.getImage(peteSrc);
}
back = kit.getImage("backdrop.gif");
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}

public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
if (back != null)
comp2D.drawImage(back, 0, 0, this);
comp2D.setColor(Color.black);
comp2D.fillRect(0, 30, 450, 30);
if (petePics[current] != null)
comp2D.drawImage(petePics[current], x, y, this);
}

public void run() {
while (true) {
walk(-10, 275);
look();
blink(3);
wave(4);
walk(x, getSize().width + 10);
pause(1500);
}
}

public void walk(int start, int end) {
int showpic = 0;
for (int i = start; i < end; i += 5) {
x = i;
// swap images
current = showpic;
repaint();
pause(150);
showpic++;
if (showpic > 2)
showpic = 0;
}
}

public void blink(int numtimes) {
for (int i = numtimes; i > 0; i--) {
current = 4;
repaint();
pause(200);
current = 3;
repaint();
pause(1000);
}
}

public void wave(int numtimes) {
for (int i = numtimes; i > 0; i--) {
current = 3;
repaint();
pause(600);
current = 5;
repaint();
pause(1100);
}
}

public void look() {
current = 3;
repaint();
pause(1000);
}

public void pause(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) { }
}
}

Chris Smith

unread,
Feb 5, 2003, 5:45:23 PM2/5/03
to
Murthy Gandikota wrote ...

> java.lang.NoClassDefFoundError: PetePanel
>
> PetePanel is defined in the code below!! By the way I run the applet
> in a microsoft browser <applet code="PeteApplet.class" height=150
> width=345> </applet>
>
> All the GIF files called by the applet are available. Thanks a bunch
> for your help.

What about PetePanel.class? Is it available? That's what's being
complained about... not the GIF files.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Murthy Gandikota

unread,
Feb 6, 2003, 8:32:13 AM2/6/03
to
When I renamed PeteApplet.class, the code complained about
PeteApplet.class not found. That means, it is not the problem with
class file not being found. PetePanel is defined with default access
class within the PeteApplet.class. Please note that there is no
PetePanel.class because there is no PetePanel.java. There is only
PeteApplet.java that contains PetePanel class.
Hope I am clear enough.
Regards
Murthy
Chris Smith <cds...@twu.net> wrote in message news:<MPG.18ab41ef2...@news.altopia.com>...

Chris Smith

unread,
Feb 6, 2003, 9:12:58 AM2/6/03
to
Murthy Gandikota wrote ...

> When I renamed PeteApplet.class, the code complained about
> PeteApplet.class not found. That means, it is not the problem with
> class file not being found. PetePanel is defined with default access
> class within the PeteApplet.class. Please note that there is no
> PetePanel.class because there is no PetePanel.java. There is only
> PeteApplet.java that contains PetePanel class.

Sorry, but no you're wrong.

When you compile PeteApplet.java, it should produce both
PeteApplet.class and PetePanel.class. You need both of them to run the
applet. Not sure where you got the idea it would be otherwise, or why
you didn't get both classes... but that's the way it should be.

So, again, the problem is that you're missing PetePanel.class. It won't
do any good to rename PeteApplet.class to that; you need the real
PetePanel.class that's produced by javac.

Murthy Gandikota

unread,
Feb 6, 2003, 3:28:46 PM2/6/03
to
Chris: you are right. The compiler produced 2 class files and I
overlooked the PetePanel.class. Let's say I am running JSP from
/usr/local/tomcat/jsp directory. I have the applet wrapper called
test.jsp in that directory along with the classes PetePanel.class and
PeteApplet.class. Where should the GIF file reside? I am getting these
errors at run time

java.security.AccessControlException: access denied
(java.io.FilePermission right1.gif read)

at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)

at java.security.AccessController.checkPermission(AccessController.java:401)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

at java.lang.SecurityManager.checkRead(SecurityManager.java:887)

at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:486)

at sun.awt.SunToolkit.getImage(SunToolkit.java:500)

at PetePanel.<init>(PeteApplet.java:32)

at PeteApplet.<init>(PeteApplet.java:6)


I checked to make sure that the GIF files have read-write permissions
to everyone (chmod 777). Your kind help is appreciated.

Regards
Murthy

0 new messages