Professor Kit Romano's Web Site

 



Site Index

About Me

Welcome
Definition of Program
Variable Types
Understanding Memory
Declaring Variables
Simple Commands
Concatenation and Strings
Sleep-Threads-Try Catch
Campus Scenes
Pictures
Java simplified
Application Shell Styles
Reading Data From File
Overview of Applets
Excellent Graphics Applet
Online Reference
Nested if statements
Arrays
Sorting Arrays
Using Strings
Functions
Top 20 Replies 
First program 211
Important Class Example
JAVA in SDK Environment
Understanding OOP
OOP simple example
Recursion example
Example of Inheritance
     and use of JTextArea



*** Web Related **********
Build Student Web Page ?
What is FTP
Download WS ftp95
Download Java Plug In
Applets from jbuilder to sdk
Practice Problems

 

Programming Examples

 

 

                                                

 

Program 6-3

// A nice applet by Jeff Wratchford           RUN THIS APPLET
// This is a good example of animation and handling action events
// good example of arrays, polygons, buttons and many graphics
// commands. Also demonstrates functions within an Applet

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



public class Applet1 extends Applet implements ActionListener


{
//arrays for drawing and coloring the house
int x1points[] = {475,475,160,160,105,195,230,230,265,265,440,195,440,530,475,160};
int y1points[] = {215,415,415,215,215,150,150,110,110,150,150,150,150,215,215,215};
int x2points[] = {530,105,195,440};
int y2points[] = {215,215,150,150};
int x3points[] = {230,265,265,230};
int y3points[] = {110,110,150,150};
//polygon for trees
int x4points[] = {100,100,125,125,165,113,60};
int y4points[] = {310,350,350,310,310,170,310};
int x5points[] = {40,40,65,65,105,53,0};
int y5points[] = {310,350,350,310,310,170,310};
//polygon for big tree
int x6points[] = {660,755,594};
int y6points[] = {125,355,355};


//loop integers
int h=0;int u=0;int trespc = 110; int n1,n2;int wratch=0;int pr=0;
int ty=0;int wee=0;int tw=0;int it=0;int ee=0;
String dub = "Jeff Wratchford.";
String rip = "RIP";


int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;


Button door = new Button ("Very Afraid!");
Button witch = new Button ("Be Afraid!");
Button credit = new Button ("Credits");
Button eyes = new Button ("Oooooh!");


Thread th = new Thread();
Thread sh = new Thread();
Thread ch = new Thread();
Thread ph = new Thread();
Thread ho = new Thread();
Thread bo = new Thread();


Color lbrown = new Color(128,128,0); //creates new colors
Color dbrown = new Color(128,64,0);
Color chimney = new Color(180,12,12);
Color rdbrown = new Color(92,65,35);
Color gold = new Color(242,197,13);
Color dgreen = new Color(22,105,49);


public void init()
{ //sets up the buttons
setLayout(null);
door.addActionListener(this);
door.setBounds (380,455,75,40);
witch.addActionListener(this);
witch.setBounds (280,455,75,40);
credit.addActionListener(this);
credit.setBounds (700,490,55,20);
eyes.addActionListener(this);
eyes.setBounds(180,455,75,40);
add(door);
add(witch);
add(credit);
add(eyes);
}


public void start()
{
flag1 = 1;
flag2 = 1;
flag3 = 1;
}


public void paint (Graphics g)
{
g.setColor (lbrown); //colors background
g.fillRect(0,350,900,900);
g.setColor (Color.black);
g.fillRect(0,0,900,350);



g.setColor (Color.yellow);
for (int pd=1;pd<300;pd++)
{
n1=1+(int) (Math.random()*800); //creates stars
n2=1+(int) (Math.random()*300);
g.fillOval (n1,n2,2,2);
}
g.fillOval (570,30,135,135); //creates moon


g.setColor (Color.gray); //makes clouds
g.fillOval (621,85,140,20);
g.fillOval (497,115,140,20);
for (int cc=0;cc<=1;cc++)
{
for (int cl=0;cl<=1;cl++)
{
g.fillOval (516+cl*50,112+cc*13,50,12);
g.fillOval (642+cl*50,82+cc*13,50,12);
}
}


for (int tre=0;tre<=7;tre++) //creates trees
{
g.setColor(dgreen);
g.fillPolygon(x4points,y4points,7);
g.setColor(rdbrown);
g.fillRect(100+trespc*tre,310,25,40);
g.setColor(Color.black);
g.drawRect(100+trespc*tre,310,25,40);
for (int tr=0;tr<=6;tr++)
x4points[tr] = x4points[tr] + trespc;
}
for (int pie=0;pie<=6;pie++)
x4points[pie] = x4points[pie] - 880;


for (int trees=0;trees<=7;trees++) //creates trees
{
g.setColor(dgreen);
g.fillPolygon(x5points,y5points,7);
g.setColor(rdbrown);
g.fillRect(40+trespc*trees,310,25,40);
g.setColor(Color.black);
g.drawPolygon(x5points,y5points,7);
g.drawRect(40+trespc*trees,310,25,40);
for (int tree=0;tree<=6;tree++)
x5points[tree] = x5points[tree] + trespc;
}
for (int pi=0;pi<=6;pi++)
x5points[pi] = x5points[pi] - 880;


for (int l=0;l<=5;l++) //creates woodpile
{
int y=0;
for (int m=0;m<=5-l;m++)
{
if (y==0) u+=6;
g.setColor(rdbrown);
g.fillOval((30+u)+m*12,390-l*12,12,12);
g.setColor(Color.black);
g.drawOval((30+u)+m*12,390-l*12,12,12);
y++;
}
}
u=0;
for (wee=0;wee<=2;wee++) //creates graveyard
{
for (wratch=0;wratch<=5;wratch++)
{
g.setColor(Color.white);
g.fillRect(535+wratch*50,365+wee*40,23,27);
g.fillOval(535+wratch*50,360+wee*40,23,10);
//g.setColor(Color.black); names on tombstone
//g.drawString(rip,527+wratch*50,375+wee*40); takes too long
}
if (wee == 1) //creates big tree in graveyard
{
g.setColor(dgreen);
g.fillPolygon(x6points,y6points,3);
g.setColor(Color.black);
g.drawPolygon(x6points,y6points,3);
g.setColor(rdbrown);
g.fillRect(650,355,45,100);
g.setColor(Color.black);
g.drawRect(650,355,45,100);
g.fillOval(660,370,25,45);
}
}


g.setColor(Color.black);
g.drawPolygon(x1points,y1points,16); //creates house
g.setColor (Color.gray);
g.fillPolygon (x1points,y1points,16);
g.setColor (dbrown);
g.fillPolygon(x2points,y2points,4);
g.setColor (chimney); //colors chimney
g.fillPolygon (x3points,y3points,4);
g.setColor (Color.gray);
for (int d=0;d<=3;d++)
g.drawLine (230,119+d*9,264,119+d*9);
g.setColor (Color.black);
for (int a=0;a<=10;a++) //creates siding
g.drawLine (160,215+a*20,475,215+a*20);
for (int b=0;b<=1;b++) //creates windows
{
g.setColor (dbrown);
g.fillRect (210+b*160,227,60,64);
g.setColor (Color.black);
g.drawRect (210+b*160,227,60,64);
}
for (int c=0;c<=1;c++)
g.fillRect(215+c*160,232,50,54);


g.setColor(dbrown); //creates window lines
for (int e=0;e<=1;e++)
{
g.drawLine(215+e*160,259,265+e*160,259);
g.drawLine(240+e*160,232,240+e*160,286);
}
g.setColor(dbrown); //creates door
g.fillRect(277,310,85,105);
g.setColor(Color.black);
g.drawRect(277,310,85,105);
g.setColor(rdbrown);
g.fillRect(283,315,73,95);
g.setColor(Color.black);
g.drawRect(283,315,73,95);
g.setColor(gold);
g.fillOval(345,357,7,7);
g.setColor (Color.black);
g.drawOval(345,357,7,7);


for (it=0;it<=1;it++) //left graveyard
{
for (tw=0;tw<=2;tw++) 
{
g.setColor(Color.white);
g.fillRect(10+tw*50,405+it*40,23,27);
g.fillOval(10+tw*50,400+it*40,23,10);
}
}


if (flag1 == 2) house(g);
if (flag2 == 2) witch(g);
if (flag3 == 2) credits(g);
if (flag4 %2 == 0) eyes(g);
if (flag4 %2 == 1) noeyes(g);
}


public void house(Graphics gg)
{
gg.setColor(Color.black);
for (int co=0;co<=1;co++)
gg.fillRect(215+co*160,232,50,54);
gg.setColor (Color.white); //ghost in window
gg.fillOval(225,250,30,35);
gg.fillRect(225,273,30,12);
gg.setColor (Color.black);
gg.fillOval(233,261,5,5);
gg.fillOval(243,261,5,5);
gg.fillOval(237,272,7,7);
gg.setColor(dbrown);
for (int ei=0;ei<=1;ei++)
{
gg.drawLine(215+ei*160,259,265+ei*160,259);
gg.drawLine(240+ei*160,232,240+ei*160,286);
}
try //pause before monster
{
bo.sleep(1500);
}
catch (Exception e)
{
}
gg.setColor (dgreen); //creates monster
gg.fillRect (390,246,20,18);
gg.fillOval (390,261,20,8);
gg.fillRect (396,269,8,10);
gg.setColor (Color.black);
gg.fillOval (398,261,5,6);
gg.setColor (dbrown);
for (int dq=0;dq<=1;dq++)
gg.fillRect (393+dq*11,271,3,3);
gg.setColor (rdbrown);
gg.fillOval (385,276,30,9);
gg.fillRect (385,282,30,4);
gg.fillRect (390,246,20,3);


for (int dg=0;dg<=1;dg++)
{
gg.setColor (Color.black);
gg.fillOval (394+dg*9,252,4,4);
gg.setColor (Color.red);
gg.fillOval (396+dg*8,254,1,1);
}


gg.setColor(dbrown); //window lines after button
for (int ie=0;ie<=1;ie++)
{
gg.drawLine(215+ie*160,259,265+ie*160,259);
gg.drawLine(240+ie*160,232,240+ie*160,286);
}
try
{
ho.sleep(1000);
}
catch (Exception e)
{
}


int i=0;int q=0;int r=0; //loop integers for doorknob
//polygon for door move
int xdoor[] = {283,356,356,283};
int ydoor[] = {315,315,410,410};


for (int h=0;h<=58;h++) //starts door animation
{
gg.setColor(Color.black);
gg.fillRect(282,314,74,96);
gg.setColor(rdbrown);
gg.fillPolygon(xdoor,ydoor,4);
xdoor[1] = xdoor[1] - 1;
xdoor[2] = xdoor[2] - 1;
if (h%7 == 0)
{
ydoor[1] = ydoor[1] + 1;
ydoor[2] = ydoor[2] - 1;
}
gg.setColor(gold); //doorknob animation
gg.fillOval(345-i+r,357-r,7-q,7-q);
gg.setColor(Color.black);
gg.drawOval(345-i+r,357-r,7-q,7-q);
i++;
if (h==25)
{
q++;
r+=2;
}
if (h==50)
{
q++;
r+=2;
}


try
{
th.sleep(50);
}
catch (Exception e)
{
}
}


try
{
ph.sleep(1500);
}
catch (Exception e)
{
}
gg.setColor(Color.white); //creates door ghost
gg.fillOval(311,323,32,53);
gg.fillRect(311,343,32,54);
gg.setColor(Color.black);
gg.fillOval(329,334,5,5);
gg.fillOval(321,334,5,5);
gg.fillOval(323,347,8,8);
try //pause before witch
{
sh.sleep(1500);
}
catch (Exception e)
{
}
}


public void witch(Graphics yo) //starts witch animation
{
//polygon for witch hat
int xhat[] = {18,34,21};
int yhat[] = {44,37,27};
//polygon for broom bristles
int xbris[] = {6,10,14};
int ybris[] = {57,67,60};
//polygon for witch arm
int xarm[] = {30,30,36,36};
int yarm[] = {48,52,58,54};


yo.setXORMode(Color.black);
for (int tt=0;tt<=800;tt+=10)
{
for(int ss=0;ss<=1;ss++)
{
yo.setColor(dbrown);
yo.drawLine(14+tt,60,45+tt,54);
yo.setColor(Color.yellow);
yo.fillPolygon(xbris,ybris,3);
yo.setColor(rdbrown);
yo.fillRect(25+tt,45,5,12);
yo.fillRect(25+tt,57,10,3);
yo.fillRect(32+tt,60,3,4);
yo.fillRect(32+tt,64,5,2);
yo.fillPolygon(xarm,yarm,4);
yo.setColor(dgreen);
yo.fillOval(23+tt,37,8,8);
yo.setColor(rdbrown);
yo.fillPolygon(xhat,yhat,3);
try
{
ch.sleep(20);
}
catch (Exception e)
{
}
}
for (int aa=0;aa<=2;aa++)
{
xbris[aa] = xbris[aa] + 10;
xhat[aa] = xhat[aa] + 10;
}
for (int vv=0;vv<=3;vv++)
xarm[vv] = xarm[vv] + 10;
}

}


public void credits (Graphics ww) //Credits
{
JOptionPane.showMessageDialog(null,"This Applet was created by "+dub,
"CREDITS",JOptionPane.INFORMATION_MESSAGE);
}


public void eyes (Graphics ic)
{
ic.setColor(Color.white);
ic.fillOval(664,387,6,6);
ic.fillOval(674,387,6,6);
ic.setColor(Color.black);
ic.fillOval(665,389,3,3);
ic.fillOval(676,389,3,3);
}


public void noeyes (Graphics ip)
{
ip.setColor(Color.black);
ip.fillRect(664,387,18,6);
}


public void actionPerformed(ActionEvent e)
{
if (e.getSource()== door)
{
flag1 = 2;
flag2 = 2;
flag3 = 1;
update(getGraphics());
}
if (e.getSource()== witch)
{
flag1 = 1;
flag2 = 2;
flag3 = 1;
update(getGraphics());
}
if (e.getSource()==credit)
{
flag1 = 1;
flag2 = 1;
flag3 = 2;
update(getGraphics());
}
if (e.getSource()==eyes)
{
flag1 = 1;
flag2 = 1;
flag3 = 1;
flag4++;
update(getGraphics());
}
}


}