|      | If you cannot see the graphical display on the left, try the following changes with your browser.
Open your Internet Explorer, click on "Tools" and select "Internet Options".
Click on "Advanced" and check the box "Use Java ..... for <applet>"
under Java (Sun). Restart your computer and reload this page. For Netscape user, click on "Edit" and select "Preferences". Click on "Advanced" in the Category window. Check the box "Enable Java" on the right. |
Zejlko Heimer
//********************************************************************
// FiveStars.java Author: K. Tam
// Oct 18, 2003
// Demonstrates a simple applet to draw stars.
//********************************************************************
import java.applet.Applet;
import java.awt.*;
public class FiveStars extends Applet
{
public void drawStar(Graphics page, int xpos, int ypos, int d, double offset)
{
double incre = 2*Math.PI*72/360; //Math.toRadians() not working
int [] sx = new int [5];
int [] sy = new int [5];
double radius= d/2;
int xcent = (int) (xpos + radius);
int ycent = (int) (ypos + radius);
for (int i=0; i<5; i++) //define vertices of star
{
sx[i]=(int) (xcent + radius*Math.sin(offset+i*incre));
sy[i]=(int) (ypos + radius - radius*Math.cos(offset+i*incre));
}
int [][] tx = new int [5][3]; //easier to draw 5 triangles than a decagon!
tx[0] = new int[]{sx[0], sx[2], xcent};
tx[1] = new int[]{sx[1], sx[3], xcent};
tx[2] = new int[]{sx[2], sx[4], xcent};
tx[3] = new int[]{sx[3], sx[0], xcent};
tx[4] = new int[]{sx[4], sx[1], xcent};
int [][] ty = new int [5][3];
ty[0] = new int[]{sy[0], sy[2], ycent};
ty[1] = new int[]{sy[1], sy[3], ycent};
ty[2] = new int[]{sy[2], sy[4], ycent};
ty[3] = new int[]{sy[3], sy[0], ycent};
ty[4] = new int[]{sy[4], sy[1], ycent};
for (int i=0; i<5; i++)
{
page.fillPolygon(tx[i], ty[i], tx[i].length);
}
}
public void paint (Graphics page)
{
page.setColor(Color.red);
int height = 600;
page.fillRect(0, 0, height*3/2, height);
int cell=height/20;
page.setColor(Color.yellow);
drawStar(page, 2*cell, 2*cell, 6*cell, 0);
drawStar(page, 9*cell, 1*cell, 2*cell, Math.atan(5.0/3)-2*Math.PI*36/360);
drawStar(page, 11*cell, 3*cell, 2*cell, Math.atan(7.0/1)-2*Math.PI*36/360);
drawStar(page, 11*cell, 6*cell, 2*cell, 2*Math.PI*144/360-Math.atan(7.0/3));
drawStar(page, 9*cell, 8*cell, 2*cell, 2*Math.PI*72/360-Math.atan(5.0/4));
}
}