You are on page 1of 5

// Program: JBonusPagan

// by Tje Bouma
// Beginning Java, Ventura College, March 26, 2009
// Notes: drawing of a star...colored with slope, resizes, with text.

import javax.swing.*;
import java.awt.*;
import java.applet.*;

public class JBonusPagan extends Applet


{
public void paint(Graphics Draw)
{
int height, r, offset,row, col, n, Q,centerx, centery;

int shiftA, shiftB, drop, lift, side /*phi*/;


int [] X = new int [6]; //coordinates for
the star points.
int [] Y = new int [6];

int smallshiftA, smallshiftB, smalldrop, smalllift, smallside, smallr;


int [] x = new int [6];
int [] y = new int [6];
//coordinates for the small inner pentagon.

Dimension rowcol;
rowcol = getSize();
row = rowcol.width;
col = rowcol.height;

String S;
FontMetrics measure;
int typesize;

if (col>row)
{
offset = row/32;
height = (row - 2 * offset)/2;
typesize = row/16;
}
else
{
offset = col/32;
height = ( col - 2 * offset) / 2;
typesize = col/16;
}
centerx = row/2;
centery = col/2;

//Setting up the star's coordinates.


r = height;
shiftA = (int) (r * Math.sin(Math.toRadians(36)));
drop = (int) (r * Math.cos(Math.toRadians(36)));
shiftB = (int) (r * Math.sin(Math.toRadians(72)));
lift = (int) (r * Math.cos(Math.toRadians(72)));
side = shiftA * 2;

X[0] = centerx;
Y[0] = centery - r;
X[1] = centerx + shiftA;
Y[1] = centery + drop;
X[2] = centerx - shiftB;
Y[2] = centery - lift;
X[3] = centerx + shiftB;
Y[3] = Y[2];
X[4] = centerx - shiftA;
Y[4] = Y[1];
X[5] = X[0];
Y[5] = Y[0];

// Setting up the pentagon's coordinates.


// To get the proportions of the inside pentagon:
// phi = b / a = [1 + sqrt(5)] / 2 so
// x' = (int) ( 2 * x / (1 + Math.sqrt (5.0) ) );
// where x is the original length and x' is one step smaller.
// golden ratio!

smallshiftA = (int) ( 4 * shiftA / (1 + Math.sqrt(5.0)) / (1 +


Math.sqrt(5.0)) );
smallshiftB = (int) ( 4 * shiftB / (1 + Math.sqrt(5.0)) / (1 +
Math.sqrt(5.0)) );
smalllift = (int) ( 4 * lift / (1 + Math.sqrt(5.0)) / (1 +
Math.sqrt(5.0)) );
smalldrop = (int) ( 4 * drop / (1 + Math.sqrt(5.0)) / (1 +
Math.sqrt(5.0)) );
smallside = (int) ( 4 * side / (1 + Math.sqrt(5.0)) / (1 +
Math.sqrt(5.0)) );
smallr = (int) ( 4 * r / (1 + Math.sqrt(5.0)) / (1 +
Math.sqrt(5.0)) );

x[0]= centerx;
y[0]= centery + smallr;
x[1]= centerx - smallshiftB;
y[1]= centery + smalllift;
x[2]= centerx - smallshiftA;
y[2]= centery - smalldrop;
x[3]= centerx + smallshiftA;
y[3]= y[2];
x[4]= centerx + smallshiftB;
y[4]= y[1];
x[5]= x[0];
y[5]= y[0];

Draw.setColor(new Color(240, 240, 200));


Draw.fillRect(0,0,row,col);
// ugh coloring...

// COLORING THE STAR SPIKES


//lower R spike
n=0;
Q=200;
for(n=0; n < smallshiftB; n++)
{
Q = Q + 5;
if(Q>240) Q=200;
Draw.setColor(new Color(Q,Q,250) );
Draw.drawLine(X[1],Y[1],x[4] - n, y[4] + (int)( n /
Math.tan(Math.toRadians(54)))) ;
Draw.drawLine(X[1],Y[1],x[4] - n - 1, y[4] + (int)( n /
Math.tan(Math.toRadians(54)))) ;
}

//lower L spike
n=0;
Q=200;
for(n=0; n < smallshiftB; n++)
{
Q = Q + 5;
if(Q>240) Q=200;
Draw.setColor(new Color(Q,250,Q) );
Draw.drawLine(X[4],Y[4],x[1] + n, y[1] + (int)( n /
Math.tan(Math.toRadians(54)))) ;
Draw.drawLine(X[4],Y[4],x[1] + n + 1, y[1] +(int)( n /
Math.tan(Math.toRadians(54)))) ;
}
//upper L spike
n=0;
Q=200;
for(n=0; n < smalllift + smalldrop; n++)
{
Q = Q + 5;
if(Q>240) Q=200;
Draw.setColor(new Color(Q,Q,112) );
Draw.drawLine(X[2], Y[2], x[2] - (int)( n /
Math.tan(Math.toRadians(72)) ), y[2] + n ) ;
Draw.drawLine(X[2], Y[2], x[2] - (int)( n /
Math.tan(Math.toRadians(72)) ) - 1, y[2] + n ) ;
}
//upper R spike
n=0;
Q=200;
for(n=0; n < smalllift + smalldrop; n++)
{
Q = Q + 5;
if(Q>245) Q=200;
Draw.setColor(new Color(Q,100,85) );
Draw.drawLine(X[3], Y[3], x[3] + (int)( n /
Math.tan(Math.toRadians(72)) ), y[3] + n ) ;
Draw.drawLine(X[3], Y[3], x[3] + (int)( n /
Math.tan(Math.toRadians(72)) ) + 1, y[3] + n ) ;
}
//top spike
n=0;
Q=200;
for(n=0; n < smallside; n++)
{
Q = Q + 5;
if(Q>245) Q=200;
Draw.setColor(new Color(245,Q,0) );
Draw.drawLine(X[0], Y[0], x[2] + n, y[2] ) ;
}
// lower left, inner pentagon.
n=0;
Q=200;
for(n=0; n < smallshiftB; n++)
{
Q = Q + 5;
if(Q>210) Q=150;
Draw.setColor(new Color(98,Q,Q) );
Draw.drawLine(centerx,centery,x[0] - n, y[0] - (int)( n /
Math.tan(Math.toRadians(54)))) ;
Draw.drawLine(centerx,centery,x[0] - n - 1, y[0] - (int)( n /
Math.tan(Math.toRadians(54))) ) ;
}
//lower right
n=0;
Q=200;
for(n=0; n < smallshiftB; n++)
{
Q = Q + 5;
if(Q>210) Q=150;
Draw.setColor(new Color(Q,Q,Q) );
Draw.drawLine(centerx,centery,x[0] + n, y[0] - (int)( n /
Math.tan(Math.toRadians(54)))) ;
Draw.drawLine(centerx,centery,x[0] + n + 1, y[0] - (int)( n /
Math.tan(Math.toRadians(54))) ) ;
}
//upperL
n=0;
Q=200;
for(n=0; n < smalllift + smalldrop; n++)
{
Q = Q + 5;
if(Q>210) Q=150;
Draw.setColor(new Color(Q,Q,75) );
Draw.drawLine(centerx, centery, x[2] - (int)( n /
Math.tan(Math.toRadians(72)) ), y[2] + n ) ;
Draw.drawLine(centerx, centery, x[2] - (int)( n /
Math.tan(Math.toRadians(72)) ) - 1, y[2] + n ) ;

}
//upper R
n=0;
Q=200;
for(n=0; n < smalllift + smalldrop; n++)
{
Q = Q + 5;
if(Q>210) Q=150;
Draw.setColor(new Color(Q,100,60) );
Draw.drawLine(centerx, centery, x[3] + (int)( n /
Math.tan(Math.toRadians(72)) ), y[3] + n ) ;
Draw.drawLine(centerx, centery, x[3] + (int)( n /
Math.tan(Math.toRadians(72)) ) + 1, y[3] + n ) ;
}
//top
n=0;
Q=200;
for(n=0; n < smallside; n++)
{
Q = Q + 5;
if(Q>210) Q=150;
Draw.setColor(new Color(255,Q,0) );
Draw.drawLine(centerx, centery, x[2] + n, y[2] ) ;
}

Draw.setColor(new Color(70,70,70));
Draw.drawPolyline(X,Y,6);
Draw.drawOval(centerx-r, centery-r, r*2, r*2);
Draw.setColor(Color.white);
Draw.setFont(new Font("sansserif",Font.ITALIC,typesize));
S = "I AM STAR";
measure = Draw.getFontMetrics();
Draw.drawString(S,centerx - (measure.stringWidth(S)/2), centery +
typesize/2);

}
}

You might also like