CS 201 - Computer Science I - Fall 2008
Graphics


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Examples and Lecture Notes > Graphics

FrogApplet2.java

/*
<APPLET CODE="FrogApplet2.class" WIDTH=600 HEIGHT=600></APPLET>
*/

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

/**
 * An applet the draws a frog.  Sort of.  OK, it draws something that
 * looks like a frog in that it is green and has bulging eyes.  You want
 * good drawings, go to the fine arts department.
 *
 * This is an example of using the <CODE>Graphics</CODE> class to
 * draw things using <CODE>setColor</CODE>, <CODE>drawString</CODE>,
 * <CODE>drawLine</CODE>, <CODE>fillRect</CODE>, <CODE>drawOval</CODE>,
 * and especially <CODE>fillOval</CODE>.  Oh, yes, lots and lots of
 * <CODE>fillOval</CODE>.
 *
 * @author Jim Glenn
 * @version 0.2 10/13/2008 undoes hard-coded positions
 * @version 0.1 10/10/2008
 */

public class FrogApplet2 extends JApplet
{
    public void init()
    {
	setBackground(Color.LIGHT_GRAY);
    }

    public void paint(Graphics g)
    {
	// compute size of from from size of applet

	int width = getWidth();
	int height = getHeight();

	int minDim = Math.min(width, height);
	int frogSize = minDim / 2;

	int centerX = width / 2;
	int centerY = height / 2;

	int frogWidth = minDim / 2;
	int frogHeight = (int)(minDim * 0.6);
	int frogTop = centerY - (int)(frogHeight * 7 / 12.0);
	int frogBottom = frogTop + frogHeight;
	int frogLeft = centerX - frogWidth / 2;
	int frogRight = centerX + frogWidth / 2;

	// compute size and positions of parts of frog

	int bodySize = (int)(frogHeight * 0.75);
	int bellySize = (int)(bodySize * 5 / 9.0);

	int clawSize = (int)(frogSize * 0.03);

	int rearLegWidth = (int)(frogSize * 0.15);
	int rearLegHeight = (int)(frogSize * 0.625);
	int rearLegY = frogBottom - rearLegHeight - clawSize;
	int leftRearLegX = frogLeft;
	int rightRearLegX = frogRight - rearLegWidth;
	int leftRearLegCenterX = leftRearLegX + rearLegWidth / 2;
	int rightRearLegCenterX = rightRearLegX + rearLegWidth / 2;

	int frontLegSize = (int)(frogSize * 0.1);
	int frontLegY = frogBottom - frontLegSize - clawSize;
	int frontLegXOffset = (int)(frogSize * 0.25);
	int leftFrontLegX = centerX + frontLegXOffset - frontLegSize;
	int rightFrontLegX = centerX - frontLegXOffset;
	int leftFrontLegCenterX = leftFrontLegX + frontLegSize / 2;
	int rightFrontLegCenterX = rightFrontLegX + frontLegSize / 2;

	int eyeSize = (int)(frogSize * 0.1);
	int pupilSize = (int)(frogSize * 0.05);
	int eyeXOffset = (int)(frogSize * 0.3);
	int leftEyeX = centerX - eyeXOffset;
	int rightEyeX = centerX + eyeXOffset - eyeSize;
	int eyeY = frogTop;

	int headWidth = (int)(frogWidth * 0.8);
	int headHeight = (int)(frogWidth * 0.5);

	// clear applet

	g.clearRect(0, 0, width, height);

	// draw the front feet

	g.setColor(Color.WHITE);

	g.fillOval(leftFrontLegCenterX - (int)(clawSize * 11 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize); // left claws
	g.fillOval(leftFrontLegCenterX - clawSize / 2, frogBottom - clawSize, clawSize, clawSize);
	g.fillOval(leftFrontLegCenterX + (int)(clawSize * 5 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize);

	g.fillOval(rightFrontLegCenterX - (int)(clawSize * 11 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize); // right claws
	g.fillOval(rightFrontLegX + frontLegSize / 2 - clawSize / 2, frogBottom - clawSize, clawSize, clawSize);
	g.fillOval(rightFrontLegCenterX + (int)(clawSize * 5 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize);

	g.setColor(Color.GREEN);

	g.fillOval(leftFrontLegX, frontLegY, frontLegSize, frontLegSize); // left front foot
	g.fillOval(rightFrontLegX, frontLegY, frontLegSize, frontLegSize); // right front foot

	// draw the back legs

	g.setColor(Color.WHITE);

	g.fillOval(leftRearLegCenterX - (int)(clawSize * 11 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize); // left claws
	g.fillOval(leftRearLegX + rearLegWidth / 2 - clawSize / 2, frogBottom - clawSize, clawSize, clawSize);
	g.fillOval(leftRearLegCenterX + (int)(clawSize * 5 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize);

	g.fillOval(rightRearLegCenterX - (int)(clawSize * 11 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize); // right claws
	g.fillOval(rightRearLegX + rearLegWidth / 2 - clawSize / 2, frogBottom - clawSize, clawSize, clawSize);
	g.fillOval(rightRearLegCenterX + (int)(clawSize * 5 / 6.0), frogBottom - (int)(clawSize * 4 / 3.0), clawSize, clawSize);

	g.setColor(Color.GREEN);

	g.fillOval(leftRearLegX, rearLegY, rearLegWidth, rearLegHeight); // left rear leg
	g.fillOval(rightRearLegX, rearLegY, rearLegWidth, rearLegHeight); // right rear leg

	// draw the body

	g.setColor(Color.GREEN);

	g.fillOval(centerX - bodySize / 2, centerY - bodySize / 2, bodySize, bodySize);

	g.setColor(new Color(192, 255, 192));

	g.fillOval(centerX - bellySize / 2, centerY - bellySize / 2, bellySize, bellySize);

	// draw the eyes

	g.setColor(Color.WHITE);

	g.fillOval(leftEyeX, eyeY, eyeSize, eyeSize); // main part
	g.fillOval(rightEyeX, eyeY, eyeSize, eyeSize);

	g.setColor(Color.BLACK);
	g.drawOval(leftEyeX, eyeY, eyeSize, eyeSize); // outline
	g.drawOval(rightEyeX, eyeY, eyeSize, eyeSize);

	g.setColor(Color.BLACK);
	g.fillRect(leftEyeX + eyeSize / 2 - pupilSize / 2, eyeY + eyeSize / 2 - pupilSize / 2, pupilSize, pupilSize); // pupils
	g.fillRect(rightEyeX + eyeSize / 2 - pupilSize / 2, eyeY + eyeSize / 2 - pupilSize / 2, pupilSize, pupilSize);

	// draw the head

	g.setColor(Color.GREEN);
	g.fillOval(centerX - headWidth / 2, centerY - bodySize / 2 - headHeight / 2, headWidth, headHeight);

	// draw the mouth

	g.setColor(Color.BLACK);
	g.drawLine(centerX - (int)(headWidth * 0.4), centerY - (int)(bodySize * 0.45), centerX + (int)(headWidth * 0.4), centerY - (int)(bodySize * 0.45));
    }

}
This code can also be downloaded from the file
FrogApplet2.java.