/**
* DrawPanel does all of the drawing using the options in the previous panels
*
* @author Loren Segal
* @version 1.0
*/
import javax.swing.*;
import java.awt.*;
public class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
int left, top, width, height;
// Pull the options out of the main class
int faceWidth = DigitalPicasso.getFaceWidth();
int faceHeight = DigitalPicasso.getFaceHeight();
Color faceColor = DigitalPicasso.getFaceColor().getValue();
Color shadesColor = DigitalPicasso.getShadesColor().getValue();
Color hatColor = DigitalPicasso.getHatColor().getValue();
HatEnum hatStyle = DigitalPicasso.getHatStyle();
ShadesEnum shadesStyle = DigitalPicasso.getShadesStyle();
// Canvas size
Dimension size = getSize();
g.setColor(new Color(255, 255, 255));
g.fillRect(0, 0, size.width, size.height);
// Important reference points
int faceLeft = (int)((size.width - faceWidth) / 2);
int faceTop = (int)((size.height - faceHeight * 0.6) / 2);
// Draw face
g.setColor(faceColor);
g.fillOval(faceLeft, faceTop, faceWidth, faceHeight);
// Set color to magenta for facial features
g.setColor(Color.MAGENTA);
// Smile
width = (int)(faceWidth * 0.6);
height = (int)(faceHeight * 0.25);
left = faceLeft + (int)((faceWidth - width) / 2);
top = faceTop + (int)((faceHeight - height) / 1.6);
g.drawArc(left, top, width, height, -30, -120);
// Nose
width = (int)(faceWidth * 0.1);
height = (int)(faceHeight * 0.1);
left = faceLeft + (int)((faceWidth - width) / 2);
top = faceTop + (int)((faceHeight - height) / 2);
g.fillOval(left, top, width, height);
// Hat
g.setColor(hatColor);
width = (int)(faceWidth * 0.75);
height = (int)(faceHeight * 0.5);
left = faceLeft + (int)((faceWidth - width) / 2);
top = faceTop - (int)(height * 0.75);
int baseHeight = (int)(height * 0.2);
// Base
g.fillRect(left, top + height - baseHeight, width, baseHeight);
// Top part, depends on hat style
height = height - baseHeight;
left += (int)(width * 0.15);
width = (int)(width * 0.7);
switch (hatStyle)
{
case LINCOLN:
g.fillRect(left, top, width, height);
break;
case CHAPLIN:
// adjust height for half circle
g.fillArc(left, top, width, height * 2, 0, 180);
break;
case PARTY:
// custom triangle method
fillTriangle(g, left, top, width, height);
break;
}
// Shades
g.setColor(shadesColor);
// Middle frame
width = (int)(faceWidth * 0.43);
height = (int)(faceHeight * 0.2);
left = faceLeft + (int)((faceWidth - width) / 2);
top = faceTop + (int)(faceHeight * 0.23);
g.drawArc(left, top, width, height, 0, 180);
// Left and right frames
width = (int)(faceWidth * 0.25);
height = (int)(faceHeight * 0.25);
int leftFrame = faceLeft + (int)(faceWidth * 0.15);
int rightFrame = faceLeft + (int)(faceWidth * 0.59);
switch (shadesStyle)
{
case LENNON:
g.fillOval(leftFrame, top, width, height);
g.fillOval(rightFrame, top, width, height);
break;
case TERMINATOR:
g.fillRect(leftFrame, top, width, height);
g.fillRect(rightFrame, top, width, height);
break;
case PARTY:
// custom triangle method
fillTriangle(g, leftFrame, top, width, height);
fillTriangle(g, rightFrame, top, width, height);
break;
}
}
public void fillTriangle(Graphics g, int left, int top, int width, int height)
{
int[] xP = { left, left + (int)(width / 2), left + width };
int[] yP = { top + height, top, top + height };
g.fillPolygon(xP, yP, 3);
}
} Powered by
GeSHi Syntax Highlighting software.
Author of all (other) material unless otherwise specified:
Loren Segal. Copyright 2005.