// Display an interactive dancing stick figure
//The "import" statements tell the Java compiler
//to add in the pre-built code that comes with Java.
//For example "import java.net.*;" makes sure all
//of the networking tools are available.
import java.awt.*;
import java.net.*;
import java.util.*;
//The web page which runs this applet
//calls it with an applet tag that looks like
//<applet>code=StickFigureApplet width=300 height=500</applet>
//The width and height are pretty self explanitory.
//The "code" part points to the class to start running
//and calls its start() function. StickFigureApplet
//does not have a start() function, so Java looks for
//GenericApplet's start() since that is the class
//StickFigureApplet is extended from
public class StickFigureApplet extends GenericApplet
{
//GraphicFigure is defined below
GraphicFigure f;
//Be careful with the render() functions! This render()
//function is derived from GenericApplet, while f.render()
//is derived from KFMFile
//damage is defined in Generic Applet as
//true in order to force the first render
public void render(Graphics g) {
if (damage) {
if (f == null)
//if it doesn't exist, create a new GraphicFigure
//passing in the URL and the string "StickFigure".
//getCodeBase() returns the current URL
f = new GraphicFigure(getCodeBase(), "StickFigure");
g.setColor(Color.white);
g.fillRect(0, 0, bounds().width, bounds().height);
g.setColor(Color.black);
//call the render function from GraphicFigure (below)
f.render(g);
}
}
double t = 0.;
//public boolean mouseDrag() is a "magic" function that is
//never explicitly called. Defining this function tells
//Java that whenever it detects the mouse being dragged,
//it is supposed to stop whatever it is doing and run
//this function
public boolean mouseDrag(Event e, int x, int y) {
//make sure the figure is drawn by setting damage
damage = true;
t += .2;
//cyle through all joints in the figure and rotate them
for (int i = 0 ; i < f.size() ; i++)
if (f.name(i) != null) { //only joints have names
f.matrix(i).rotateX(1.2*Math.cos( i + .678*t));
f.matrix(i).rotateY(1.2*Math.cos(.9*i + .890*t));
f.matrix(i).rotateZ(1.2*Math.cos(.8*i + t));
}
return true;
}
}
class GraphicFigure extends KVMFile
{
//This is the constructor for GraphicFigure and is automatically
//called any time a new GraphicFigure is created. The line
//"super(context, URLName)" calls the constructor of KVMFile
//since that is the class GraphicFigure is extended from
GraphicFigure(URL context, String URLName) { super(context, URLName); }
Graphics g;
//public void render is an inherited function that renders what
//the figure looks like. However, since this function overrides its parent
//render() function, the actual code must be reprogrammed or it
//is lost. To get around this, the render() of its parent can
//be called with super.render(). Also, make sure you don't
//confuse GraphicFigure's render() with StickFigureApplet's
//render(); they are derived from different places!
public void render(Graphics g) {
this.g = g;
super.render();
}
//This function actually draws the line for the figure
//(See the Java documentation for info on drawLine())
public void drawLine(double x1, double y1, double x2, double y2) {
g.drawLine((int)(3*x1), (int)(3*y1), (int)(3*x2), (int)(3*y2));
}
//Joints don't need to be drawn so the function is blank
public void drawJoint(String name, double x, double y) { }
}