Hi,
I m trying Circle.java programme in the tools directory & its
workin fine ( compilation & running ). But whenever I m trying to add
the following import statement Its giving me error That class could not
be found though its compiling successfully.
package net.tinyos.tools;
import java.io.*;
import net.tinyos.packet.*;
import net.tinyos.util.*;
import net.tinyos.message.*;
*When ever I m trying to add above packages its
showing me NoclassDefFound exception.*
**
* So does it mean that these packages can not be
imported by other Applet programme in the same folder ( tools ) ?*
**
*Note : I haven't used any of the classes from above package, but just
imported them in my applet file.*
**
*Following is my simple applet programme imported with above packages
which gives me error.*
**
// What follows here is a special javadoc comment, which is
// used for automatic generation of documentation in HTML.
/**
* Simple applet of moving circle.
*
* @author Les Kitchen <[EMAIL PROTECTED]>
*
*
*/
// This is probabl! y a lot more heavily commented than necessary,
// for didactic purposes...
// Imported stuff for windowing, events, and applets...
package net.tinyos.tools;
import java.io.*;
import net.tinyos.packet.*;
import net.tinyos.util.*;
import net.tinyos.message.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
// Comment-embedded sample HTML code for invoking this applet
// So that the source code can be fed to appletviewer
// In the applet HTML tag, code is the .class file where the
// Java bytecode can be found, as a URL (in this case a relative URL),
// and width and height give the width and height of the applet.
// The param tags pass parameters to the applet (only one here).
/*
<applet code="Circle" width=300 height=200>
<param name=sleeptime value=100>
</applet>
*/
// This is a specialization of the Java Applet class, and it has
// to implement the KeyListener, MouseListener and Runnable interfaces
// so it can respectively handle key events, handle mouse events, and be
// run as a thread.
public class Circle extends Applet
implements KeyListener, MouseListener, Runnable {
// Encodings of directions -- "final" so they're in effect constants
// Even numbers so diagonal directions could be added later
// Multiples of 45 degrees anticlockwise from East
final int E = 0; // East
final int N = 2; // North
final int W = 4; // West
final int ! S = 6; // South
// Various bits of information for our applet's state
int direction = E; // movement direction
int cx = 10, cy = 10; // circle center
int diameter = 20; // circle diameter
Thread t = null; // Thread for moving the circle
boolean running = false; // Whether thread is running
// How long to sleep between circle movements, milliseconds.
int sleeptime = 100;
// The next four methods, init(), start(), stop(), and paint(),
// are part of Applet. We override them here to do what we want.
// There's also a destroy() method, but we don't need to worry about
// it.
// Called from outside when the applet is first created
public void init() {
System.out.println("This is in init ");
// Grab (as a string) the parameter named "sleeptime"
! // from the HTML applet's param tag
String sleepstring = getParameter("sleeptime");
// The sleeptime parameter may not have been provided
// (note null object not empty string), or may have been
// badly formatted, triggering a caught exception. In
// either case, stick with default.
try {
if( sleepstring != null ) {
sleeptime = Integer.parseInt(sleepstring); // Like atoi() in C
}
} catch(NumberFormatException e) {
// Do nothing if badly formatted number triggers exception
}
// Nominate this applet itself as the listener for key and mouse
// events.
addKeyListener(this);
addMouseListener(this);
requestFocus(); // Request input focus -- needed to get input at all
// Background color for the applet's window
setBackground(Color.WHITE);
}
// Called from outside whenever applet starts running -- whenever it
// becomes visible
public void start () {
System.out.println("This is in start ");
// All we do is create a new thread to run the run() method
// of this applet, and set it going.
t = new Thread(this);
running = true;
t.start();
}
// Called from outside whenever applet stops running -- whenever it
// becomes invisible. So we don't have to waste CPU cycles updating
// the graphics display unnecessarily.
public void stop () {
System.out.println("This is in stop ");
// All we do is mark that the thread should stop running,
// to be noticed by the run() method next time it wakes up.
running = false;
// Not strictly necessary, but if we no longer keep a reference
/! / to the thread, then it can get garbage collected sooner.
t = null;
}
// Called from outside whenever the applet should have its
// window display redrawn.
public void paint(Graphics g) {
System.out.println("This is in paint ");
// Gets passed the Graphics object on which to draw.
// All we do is pass it on to our own method.
g.setColor(Color.BLACK);
drawCircle(g, cx, cy, diameter );
}
// This is the one method required to implement the Runnable interface.
// It's called by a thread to do our work. This is the heart of
// the applet's action.
public void run () {
System.out.println("This is in run ");
// We loop (almost) forever, repainting, sleeping a bit, and
// calling our own method move() to change the posi! tion of
// the circle. The thread terminates when running gets set
// to false (presumably by stop()), so we have to keep polling that.
for(;;) {
try {
// This is the preferred way for an applet to ask for
// its window to be redrawn. Calling paint() directly
// would holdup execution while the (possibly lengthy)
// paint() method completed. Calling repaint() just
// tells the system to schedule a call to paint()
// sometime real soon now.
repaint();
if(!running) break;
Thread.sleep(sleeptime);
move();
} catch (InterruptedException e) {
// The thread might get interrupted. We have to
// catch that exception just to do nothing about it.
}
}
}
// The next three methods have to be here so our appl! et implements
// the KeyListener interface.
// Called from outside whenever a key is depressed.
public void keyPressed(KeyEvent ke) {
// Basically, get the key code from the event object passed in
// and do various things, like change size of the circle for
// PAGE_UP PAGE_DOWN, or change direction of motion for arrow
// keys. Note this just changes the internal parameters of
// the applet; the movement of the circle, and the drawing
// happens elsewhere.
int key = ke.getKeyCode();
switch(key) {
case KeyEvent.VK_PAGE_DOWN:
if( diameter > 1 ) --diameter;
break;
case KeyEvent.VK_PAGE_UP:
diameter++;
break;
case KeyEvent.VK_LEFT:
direction = W;
break;
case KeyEvent.VK_RIGHT:
direction = E;
break;
case KeyEvent.VK_UP:
direction = N;
break;
case KeyEvent.VK_DOWN:
direction = S;
break;
}
// Show what's happening in the status line.
// Note: + is Java's string concatenation operator, and a
// number in string context is automagically converted into
// its string representation.
showStatus("Diameter: " + diameter + ", Direction: " + direction);
}
// These two methods have to be defined (with null bodies) so we
// fully implement the KeyListener interface.
// We probably could just as easily have done our stuff instead in the
// keyTyped() method. Done the way we have here, the change happens
// as soon as the key is pressed, and doesn't wait for it to be
// released.
// Actually keyTyped() probably wouldn't do the job, since it
// just hand! les typed-in characters -- wouldn't handle arrow
// keys and such.
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
// The next five methods are necessary to fully implement
// the MouseListener interface, but only mouseClicked has
// a non-null body, since that's all we care about.
public void mouseClicked(MouseEvent me) {
// Set the circle center to be where the mouse clicked.
cx = me.getX(); cy = me.getY();
showStatus("Diameter: " + diameter + ", Direction: " + direction +
", Jumped to (" + cx + "," + cy + ")" );
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent! me) {
}
// This is our own method to draw a circle of given
// radius and center (in a given Graphics object).
// It just makes a call on the underlying drawOval()
// method, with the obvious geometric adjustments.
void drawCircle( Graphics g, int ccx, int ccy, int ccd ) {
System.out.println("This is in drawCircle ");
int r = diameter/2;
g.drawOval( ccx-r, ccy-r, ccd, ccd );
}
// This is our own method for updating the position of the circle
// each time step. Basically, it just moves one pixel in the
// appropriate direction, and "bounces" back off the edge of the
// window when it touches (or gets closer than touching, just in
// case it slipped by because of some subtlety that escapes me --
// ah yes, you might mouse-click very close to the edge).
void move() {
System.out.println("This is in move ");
int r = diameter/2;
Dimension d = getSize();
switch( direction ) {
case E:
if( cx+r >= d.width-1 ) {
cx = d.width-r;
direction = W;
} else {
cx++;
}
break;
case W:
if( cx-r <= 0 ) {
cx = r;
direction = E;
} else {
--cx;
}
break;
case S:
if( cy+r >= d.height-1 ) {
cy = d.height-r;
direction = N;
} else {
cy++;
}
break;
case N:
if( cy-r <= 0 ) {
cy = r;
direction = S;
} else {
--cy;
}
break;
}
}
}//end of Circle applet
**
*thanks*
------------------------------------------------------------------------
Yahoo! Photos – Showcase holiday pictures in hardcover
Photo Books
<http://us.rd.yahoo.com/mail_us/taglines/photobooks/*http://pa.yahoo.com/*http://us.rd.yahoo.com/mail_us/taglines/photos/evt=38088/*http://pg.photos.yahoo.com/ph//page?.file=photobook_splash.html>.
You design it and we’ll bind it!
------------------------------------------------------------------------
_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help