Final Project Applet
/**
* import statements
**/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
public class Spiro extends Applet implements ActionListener
{
// declare variables
private Label lblOrder, lblAngle, lblIterate, lblUnit, lblInst1;
private Label lblInst2, lblInst3, lblInst4, lblInst5, lblInst6, lblInst7;
private TextField txtOrder, txtAngle, txtIterate, txtUnit;
private Button instruct, make;
int order, angle;
int border=0, bangle=0, unit;
//here the strings which make the instruction set are initialized
String message="A Spirolateral is a type of path specified by a certain type of recursive algorithm. This program uses an algorithm";
String message2="based on right turns, though figures with lefts, or lefts and rights, are also possible. The variables involved ";
String message3="are the angle of the turn, the number of turns to take(the `order'), the unit length, and the number of iterations";
String message4="to perform. Each time a turn is made, the next line is increased in length by one more unit. Some combinations";
String message5="of values will spill off the screen, or trail in unexpected directions. There is no known method to prove before-";
String message6="hand whether a given spirolateral will be a closed form, or not. Many interesting results can be found using";
String message7="angles of 30, 45, 60, 72, 90, 108, 120, 135, and 150...";
// the init method; it inits
public void init()
{
lblUnit=new Label("Unit size: ");
txtUnit=new TextField(15);
lblOrder=new Label("Order: ");
txtOrder = new TextField(15);
lblInst1=new Label(message);
lblInst2=new Label(message2);
lblInst3=new Label(message3);
lblInst4=new Label(message4);
lblInst5=new Label(message5);
lblInst6=new Label(message6);
lblInst7=new Label(message7);
lblAngle=new Label("Angle: ");
txtAngle=new TextField(15);
lblIterate=new Label("Iterations: ");
txtIterate=new TextField(15);
make=new Button("Make a Spirolateral!");
add(lblInst1);
add(lblInst2);
add(lblInst3);
add(lblInst4);
add(lblInst5);
add(lblInst6);
add(lblInst7);
add(lblUnit);
add(txtUnit);
add(lblOrder);
add(txtOrder);
add(lblAngle);
add(txtAngle);
add(lblIterate);
add(txtIterate);
add(make);
// if any clarification is needed for the above code, you
// should go read a java book. next line adds and actionlistener
// to make
make.addActionListener(this);
// sets some defaults to avoid runtime errors
txtUnit.setText("3");
txtOrder.setText("27");
txtAngle.setText("135");
txtIterate.setText("8");
}
public void actionPerformed ( ActionEvent event )
{
//this is simple; repaints, using whatever the new values are
repaint();
}//end actionPerformed
public void paint(Graphics foo)
{
int length=50;
int firstx= 320, firsty=350;
int unit=3, bunit, biterate, iterate;
foo.setColor(Color.gray);
foo.fillRect(5, 230, 690, 550);
// this catches any case where a non-integer or too large a number is in the TextField(s)
// will also catch blank (empty) strings of non-numeric characters.
try {
border = Integer.parseInt(txtOrder.getText());
bangle = Integer.parseInt(txtAngle.getText());
bunit = Integer.parseInt(txtUnit.getText());
biterate = Integer.parseInt(txtIterate.getText()); }
catch (NumberFormatException e){
border=0; bangle=0; bunit=0;biterate=0; }
// this block initializes some default values, to make one of the prettier spirolaterals
if (border > 0)
order=border;
else
order=27;
if (bunit > 0)
unit=bunit;
else
unit=3;
if (biterate > 0)
iterate=biterate;
else
iterate=9;
int x=firstx;
int y=firsty;
if (bangle > 0)
angle=bangle;
else
angle=135;
int nangle=0;
// this loop does the work. it calls getX and getY methods to find
// endpoints for lines...
for (int j=1; j<=iterate; j++)
{
for (int i=1; i<=order; i++)
{
newColor(i, foo);
nangle=nangle+angle;
int nx=getX(i * unit, nangle, x);
int ny=getY(i * unit, nangle, y);
foo.drawLine(x, y, nx, ny);
x=nx;
y=ny;
}
}
}
// the next two methods find the x,y points respectively of a line of length
// length, and angle angle. If this doesn't make sense, try to modify them,
// compile and attempt to run. You should see what they do soon enough
public int getX(int length, int angle, int x)
{
return x + (int)(length * Math.cos(angle * Math.PI / 180));
}
public int getY(int length, int angle, int y)
{
return y + (int)(length * Math.sin(angle * Math.PI / 180));
}
// this method simply picks a new color, cycling through a list each time.
// could be done better, but i'm tired and it works fine
public void newColor(int n, Graphics foo)
{
if (n>24)
n=n-24;
else if (n>16)
n=n-16;
else if (n>8)
n=n-8;
switch(n)
{
case 1:
foo.setColor(Color.blue);
break;
case 2:
foo.setColor(Color.red);
break;
case 3:
foo.setColor(Color.green);
break;
case 4:
foo.setColor(Color.orange);
break;
case 5:
foo.setColor(Color.cyan);
break;
case 6:
foo.setColor(Color.yellow);
break;
case 7:
foo.setColor(Color.pink);
break;
case 8:
foo.setColor(Color.magenta);
break;
default:
foo.setColor(Color.black);
break;
}
}
}