You are on page 1of 3

// Program: JBonusPentagon

// by Tje Bouma
// Beginning Java, Ventura College, March 26, 2009
// Notes: drawing of a pentagon...colored along the slope of each
side...resizes...with text.

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

public class JBonusPentagon extends Applet


{
public void paint(Graphics Draw)
{

int ax,ay,bx,by,cx,cy,dx,dy,ex,ey, height, r, offset,n, Q, side,


shiftA, shiftB, drop, lift, centerx, centery, row, col;
int [] x = new int [6];
int [] y = new int [6];

String S;
FontMetrics measure;
int typesize;

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

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

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 + shiftB;
y[1] = centery - lift;
x[2] = centerx + shiftA;
y[2] = centery + drop;
x[3] = centerx - shiftA;
y[3] = y[2];
x[4] = centerx - shiftB;
y[4] = y[1];
x[5] = x[0];
y[5] = y[0];

Draw.setColor(new Color(20,20,20));
Draw.fillRect(0,0,row,col);
Draw.setColor(new Color(200,200,200));
Draw.drawOval(centerx-r, centery-r, r*2, r*2);

// Shading the pentagon:


//UPPER L
n=0;
Q=100;
for(n=0; n < shiftB; n++)
{
Q = Q + 6;
if(Q>240) Q=100;
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))) ) ;
}
//UPPER R
n=0;
Q=100;
for(n=0; n < shiftB; n++)
{
Q = Q + 6;
if(Q>240) Q=100;
Draw.setColor(new Color(198,50,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 R
n=0;
Q=0;
for(n=0; n < (drop + lift); n++)
{
Q = Q + 10;
if(Q>240) Q=0;
Draw.setColor(new Color(102,Q,Q) );
Draw.drawLine(centerx,centery,x[1] - (int)(n/
Math.tan(Math.toRadians(72))), y[1] + n) ;
Draw.drawLine(centerx,centery,x[1] - (int)(n/
Math.tan(Math.toRadians(72))) - 1, y[1] + n ) ;
}
//LOWER L
n=0;
Q=0;
for(n=0; n < (drop + lift); n++)
{
Q = Q + 10;
if(Q>240) Q=0;
Draw.setColor(new Color(Q,102,102) );
Draw.drawLine(centerx,centery,x[4] + (int)(n/
Math.tan(Math.toRadians(72))), y[4] + n) ;
Draw.drawLine(centerx,centery,x[4] + (int)(n/
Math.tan(Math.toRadians(72))) + 1, y[4] + n ) ;
}
// BOTTOM / CENTER
n=0;
Q=0;
for(n=0; n < side; n++)
{
Q = Q + 10;
if(Q>240) Q=0;
Draw.setColor(new Color(240,240,Q) );
Draw.drawLine(centerx,centery,x[3] + n, y[3]) ;
}

Draw.setColor(new Color(250,250,200));
Draw.setFont(new Font("sansserif",Font.ITALIC,typesize));
S = "PENTAGON";
measure = Draw.getFontMetrics();
Draw.drawString(S,centerx - (measure.stringWidth(S)/2), centery +
typesize/2);

}
}

You might also like