This applet from Sam's book doesn't work
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   murthy_gandikota
Posted On:   Wednesday, February 5, 2003 02:19 PM

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

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

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

   <<Less

Re: This applet from Sam's book doesn't work

Posted By:   murthy_gandikota  
Posted On:   Wednesday, February 5, 2003 02:22 PM

Apparently JAva Guru's haven't figured out how to display code with pre tags. What a mess!
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

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) { }
}
}
About | Sitemap | Contact