Introduction to the Figure Package - Response to keypresses
Sample Code in which a figure responds to keypresses
Here is a sample figure just containing a set of axes, a label and
a subfigure consisting of a rose with three petals. Pressing the key "b"
causes the rose petals to rotate backwards. Pressing any other key
casues them to rotate forwards.
/* File: keyRose.java */
import java.applet.* ;
import java.awt.* ;
import figPac.* ;
public class keyRose extends Applet {
Figure canvas ;
fSubFigure subfig ;
public void init() {
canvas = new Figure(this,6,6) ;
this.add(canvas) ;
canvas.limits(-300,-300,300,300) ;
canvas.showCoords = true ;
canvas.useZoom = true ;
canvas.useDrag = true ;
canvas.append(new fAxes("x axis","y axis") ) ;
subfig= new fSubFigure() ;
subfig.append(new fCurve(new rose(200,3),
0,Math.PI,
fCurve.CLOSED+fCurve.FILLED)) ;
canvas.append(new fEnv("fillColor", Color.yellow)) ;
canvas.append(subfig) ;
canvas.append(new fLabel(100,50,"cc","Petal")) ;
canvas.toScreen() ;
}
public boolean keyDown(Event e, int key) {
if ( key-96 == 2 ) subfig.rotate(-Math.PI/8) ;
else subfig.rotate(Math.PI/8) ;
canvas.toScreen() ;
return true ;
}
}
class rose implements S2V {
double radius ;
int noPetals ;
public rose(double radius, int noPetals) {
this.radius = radius ;
this.noPetals = noPetals ;
}
public double[] map(double t) {
double[] out = new double[2] ;
out[0] = radius*Math.cos(t)*Math.sin(noPetals*t) ;
out[1] = radius*Math.sin(t)*Math.sin(noPetals*t) ;
return out ;
}
}
To execute this you would have to save the above program to a file named
keyRose.java and you would also have to create a file called keyRose.html and
containing
<HEAD><TITLE>Figure Test Program</TITLE></HEAD>
<BODY>
<H1>Figure Test Program </H1>
<P>
<APPLET code="keyRose.class" width=600 height=600></APPLET>
</BODY>
You then compile and execute the program using
javac keyRose.java
appletviewer keyRose.html &