Java Applet Source Code
Back
Quote.java
/** This program writes a little epigram attributed to one Eric Meltzer,
*   on the usenet board `rec.humor.funny'.
*
*   @author Phil Crissman
*   written in Notepad
*/
import java.awt.*;
//;import javax.swing.*;
import java.applet.Applet;
public class Quote extends Applet
{
	
	public void paint( Graphics g)
	{
		setBackground( Color.black);   //sets the background color, black
		
		/** the next block of code specifies a series of rectangles, filled
		*   with different colors, to make a border
		*/
		g.setColor(Color.red);
		g.fillRect(5, 5, 500, 200);
		g.setColor(Color.blue);
		g.fillRect(7, 7, 495, 195);
		g.setColor(Color.yellow);
		g.fillRect(10, 10, 490, 190);
		g.setColor(Color.green);
		g.fillRect(13, 13, 484, 184);		
		g.setColor(Color.magenta);
		g.fillRect(15, 15, 480, 180);
		g.setColor(Color.black);
		g.fillRect(18, 18, 475, 175);
		g.setColor(Color.orange);
		g.fillRect(22, 22, 467, 167);
		
		// sets color back to black for the text
		g.setColor(Color.black);
		
		// draws the body of the quote...
		g.drawString("Richard M. Stallman, Linus Torvalds, and Donald Knuth engage in a 
discussion", 30, 50);
		g.drawString("on whose impact on the computerized world was greatest...", 40, 62);
		
		g.drawString("Stallman: 'God told me I have programmed the best editor in the 
world!'", 50, 100);
		g.drawString("Torvalds: 'Well, God told *me* that I have programmed the best", 50, 
112);
		g.drawString("operating system in the world!'", 50, 124);
		g.drawString("Knuth: 'Wait, wait -- I never said that.'", 50, 136);
		g.drawString("- Erik Meltzer, rec.humor.funny", 100, 160); 
	}
}
Back
House.java
/*
 * House.java
 *
 * Created on February 2, 2003, 2:23 PM
 * Created with netBeans IDE
 * www.netBeans.org
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.awt.*;
import java.applet.Applet;
public class House extends Applet {
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    public void paint(java.awt.Graphics foo) {
        int roofx[]={50, 100, 150};   //declare x coords for polygon
        int roofy[]={50, 25, 50};     // " y coords for polygon
        Polygon roofP = new Polygon(roofx, roofy, 3);   // instance polygon
        
        setBackground(Color.cyan);   // sets background
        
        //makes the green foreground
        foo.setColor(Color.green); 
        foo.fillRect(1, 130, 400, 200);
        
        // creates the polygon for the roof
        foo.setColor(Color.red);
        foo.fillPolygon(roofP);
        
        // makes the house
        foo.setColor(Color.white);
        foo.fillRect(55, 50, 90, 90);
        
        // draws red rects for the door, and window shutters
        foo.setColor(Color.red);
        foo.fillRect(60, 100, 25, 40);
        foo.fillRect(90, 100, 50, 20);
        foo.fillRect(58, 55, 40, 30);
        foo.fillRect(102, 55, 40, 30);
        
        // draws the blue rects for windows
        foo.setColor(Color.blue);
        foo.fillRect(63, 56, 30, 28);
        foo.fillRect(107, 56, 30, 28);
        foo.fillRect(95, 101, 40, 18);
        
        // make some friendly sunshine
        foo.setColor(Color.yellow);
        foo.fillOval(200, 20, 40, 40);
        
    }
    
}
Back
Bargraph.java
/*
 * Bargraph.java
 *
 * Created on February 2, 2003, 3:15 PM
 * Created using netBeans IDE, www.netbeans.org
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.awt.*;
import java.applet.*;
public class Bargraph extends Applet {
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    /** Paints this component.
     * 

* This method is called when the contents of the component should * be painted; such as when the component is first being shown or * is damaged and in need of repair. The clip rectangle in the * Graphics parameter is set to the area * which needs to be painted. * Subclasses of Component that override this * method need not call super.paint(g). *

* For performance reasons, Components with zero width * or height aren't considered to need painting when they are first shown, * and also aren't considered to need repair. *

* Note: For more information on the paint mechanisms utilitized * by AWT and Swing, including information on how to write the most * efficient painting code, see * Painting in AWT and Swing. * * @param g the graphics context to use for painting * @see #update * @since JDK1.0 * */ public void paint(Graphics foo) { setBackground(Color.lightGray); foo.setColor(Color.green); foo.fill3DRect(100, 30, 150, 20, true); foo.setColor(Color.cyan); foo.fill3DRect(100, 60, 175, 20, true); foo.setColor(Color.yellow); foo.fill3DRect(100, 90, 120, 20, true); foo.setColor(Color.black); foo.drawString("1994:", 50, 43); foo.drawString("1995:", 50, 73); foo.drawString("1996:", 50, 103); foo.drawString("Annual Rainfall in Xanadu", 75, 150); foo.setColor(Color.black); foo.drawString("150 cm", 120, 43); foo.drawString("175 cm", 120, 73); foo.drawString("120 cm", 120, 103); } }

Back
Target.java
/*
 * Target.java
 *
 * Created on February 2, 2003, 9:10 PM with netBeans IDE, www.netbeans.org
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.awt.*;
import java.applet.*;
public class Target extends Applet {
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    /** Paints this component.
     * 

* This method is called when the contents of the component should * be painted; such as when the component is first being shown or * is damaged and in need of repair. The clip rectangle in the * Graphics parameter is set to the area * which needs to be painted. * Subclasses of Component that override this * method need not call super.paint(g). *

* For performance reasons, Components with zero width * or height aren't considered to need painting when they are first shown, * and also aren't considered to need repair. *

* Note: For more information on the paint mechanisms utilitized * by AWT and Swing, including information on how to write the most * efficient painting code, see * Painting in AWT and Swing. * * @param g the graphics context to use for painting * @see #update * @since JDK1.0 * */ public void paint(Graphics foo) { setBackground(Color.black); foo.setColor(Color.yellow); foo.fillOval(10, 10, 200, 200); foo.setColor(Color.red); foo.fillOval(30, 30, 160, 160); foo.setColor(Color.green); foo.fillOval(50, 50, 120, 120); foo.setColor(Color.orange); foo.fillOval(70, 70, 80, 80); foo.setColor(Color.blue); foo.fillOval(90, 90, 40, 40); foo.setColor(Color.black); foo.drawString("10", 12, 115); foo.setColor(Color.white); foo.drawString("20", 32, 115); foo.setColor(Color.black); foo.drawString("30", 52, 115); foo.setColor(Color.black); foo.drawString("40", 72, 115); foo.setColor(Color.white); foo.drawString("50", 92, 115); } }

Back
Project4_2.java
/*
 * Project4_2.java
 *
 * Created on February 2, 2003, 10:54 PM with netBeans IDE, www.netbeans.org
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.awt.*;
import java.applet.*;
public class Project4_2 extends Applet {
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    /** Paints this component.
     * 

* This method is called when the contents of the component should * be painted; such as when the component is first being shown or * is damaged and in need of repair. The clip rectangle in the * Graphics parameter is set to the area * which needs to be painted. * Subclasses of Component that override this * method need not call super.paint(g). *

* For performance reasons, Components with zero width * or height aren't considered to need painting when they are first shown, * and also aren't considered to need repair. *

* Note: For more information on the paint mechanisms utilitized * by AWT and Swing, including information on how to write the most * efficient painting code, see * Painting in AWT and Swing. * * @param g the graphics context to use for painting * @see #update * @since JDK1.0 * */ public void paint(Graphics g) { float radius = 7.5f; //declare the radius to be used. used as a constant. float circumference, area, volume; setBackground(Color.white); //perform the calculations circumference = 2.0f*(float)Math.PI*radius; area = (float)Math.PI*radius*radius; volume = (4.0f*(float)Math.PI*radius*radius*radius)/3.0f; // write the calculations g.drawString("Given that the radius is 7.5...", 50, 20); g.drawString("The circumference will be " + circumference, 50, 40); g.drawString("The area will be " + area, 50, 60); g.drawString("The volume will be " + volume, 50, 80); g.drawOval(60, 100, 45, 45); g.fillOval(110, 100, 45, 45); g.drawOval(160, 100, 46, 46); g.setColor(Color.gray); g.drawOval(175, 100, 16, 46); } }

Back
Flags.java
/*
 * Flags.java
 * A program which draws two flags, that of Canada and that of Finland.
 * Canada is my birthplace, and Finland, of course, is the origin of Linux.
 * Created on February 6, 2003, 11:11 PM
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.awt.*;
import java.applet.*;
public class Flags extends Applet {
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    /** Paints this component.
     * 

* This method is called when the contents of the component should * be painted; such as when the component is first being shown or * is damaged and in need of repair. The clip rectangle in the * Graphics parameter is set to the area * which needs to be painted. * Subclasses of Component that override this * method need not call super.paint(g). *

* For performance reasons, Components with zero width * or height aren't considered to need painting when they are first shown, * and also aren't considered to need repair. *

* Note: For more information on the paint mechanisms utilitized * by AWT and Swing, including information on how to write the most * efficient painting code, see * Painting in AWT and Swing. * * @param g the graphics context to use for painting * @see #update * @since JDK1.0 * */ public void paint(Graphics foo) { /** this block sets two arrays; they represent all the (x,y) points of the leaf on the * Canadian flag. So, if we made a set of all the (x,y) of the leaf, the array xval would * represent the domain(all the x values) and the array yval represents the range(all the * y values. They correspond on a one-to-one basis, and are used in the declaration of the * Polygon object which is used to draw the leaf. */ int xVal[]={65,75,70,86,89,101,98,105,115,125,132,129,141,144,160,155,165,140,143,117,118,112,113,87,90}; int yVal[]={60,58,40,43,38,50,25,30,10,30,25,50,38,43,40,58,60,80,90,85,110,110,85,90,80}; /** * This is the declaration and instancing of a Polygon object for our leaf. It uses xval and yval * as mentioned previously. The last argument, 25, tells the Polygon how many points our leaf has. */ Polygon leaf=new Polygon(xVal, yVal, 25); // sets the background to orange setBackground(Color.orange); // writes the names of the countries, so we can see them while we wait for the applet // to draw the flags. foo.setColor(Color.black); foo.drawString("Canada", 20, 130); foo.drawString("Republic of Finland", 235, 310); // draws a white rectangle, which we will draw our Canadian flag in. foo.setColor(Color.white); foo.fillRect(5, 5, 220, 110); // draws two red rectangles, and our leaf polygon, to make a Canadian flag. foo.setColor(Color.red); foo.fillRect(5, 5, 55, 110); foo.fillRect(170, 5, 55, 110); foo.fillPolygon(leaf); // draws a new white rectangle for the Finnish flag foo.setColor(Color.white); foo.fillRect(225, 185, 220, 110); // draws the blue cross, which is the figure of the Finnish flag foo.setColor(Color.blue); foo.fillRect(225, 223, 220, 34); foo.fillRect(290, 185, 34, 110); } }

Back
Thermometer.java
/*
 * Thermometer.java
 *
 * Created on February 9, 2003, 1:28 PM
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.awt.*;
import java.applet.Applet;
import java.text.*;
import javax.swing.*;
public class Thermometer extends Applet {
    
    float fval=75.0f, cval, cval2=0.0f, fval2;
    
    
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    /** Paints this component.
     *
     */
    public void paint(Graphics g) {
        // this creates a formating object, to truncate any extra decimal digits
        DecimalFormat oneDigit = new DecimalFormat("0.0");
        
        // gives cval the value of fval, run thru the tempConvert method
        cval=tempConvert(fval);
        
        // tells us the substance of our calculation
        g.drawString(oneDigit.format(fval) + " degrees Fahrenheit is equivalent to " + oneDigit.format(cval)
            + " degrees Celsius", 5, 10);
        
        // this ought to be self-explanatory
        setBackground(Color.gray);
        
        
        // now we'll draw graphics to display the Celsius - Fahrenheit relations
        
        // draws the white portions of the thermometer
        g.setColor(Color.white);
        g.fillOval(165, 20, 20, 20);
        g.fillRect(165, 30, 20, 180);
        
        // draws the red portions of the thermometer
        g.setColor(Color.red);
        g.fillRect(165, 70, 20, 140);
        g.fillOval(155, 200, 40, 40);
        
        // draws a line at `room temperature'
        g.setColor(Color.black);
        g.drawLine(163, 70, 187, 70);
        g.drawLine(163, 71, 187, 71);
        g.drawString(oneDigit.format(cval)+"C", 127, 75);
        g.drawString(oneDigit.format(fval)+"F", 188, 75);
        
        // draws a line at freezing point (of water), to add scale
        g.drawLine(163, 150, 187, 150);
        g.drawLine(163, 151, 187, 151);
        g.drawString(oneDigit.format(cval2)+"C", 127, 155);
        // this line makes use of the celConvert method, found below;
        g.drawString(oneDigit.format(celConvert(fval2))+"F", 188, 155);
    }
    
    // this method, per instruction, converts a Fahrenheit temp to its Celsius equivalent
    public float tempConvert(float val) {
        return (val - 32.0f) * 5.0f / 9.0f;
    }
    
    // for good measure; used to convert 0C to it's fahrenheit equivalent
    public float celConvert(float val) {
        return (val * 9.0f) / 5.0f + 32.0f;
    }
}
Back
Famile.java
/*
 * Famile.java
 *
 * Created on February 23, 2003, 3:36 PM
 */
/**
 *
 * @author  Philip_Crissman
 */
import java.applet.Applet;
import java.awt.*;
public class Famile extends Applet {
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
    }
    
    public void paint(Graphics foo) {
        //make some variables
        int adult, child;   //these will hold the sizes of the adult and child people
        adult = 200;        // adult size
        child = (int)(adult/2.0f);  //child size; half the size of adults
        
        setBackground(Color.lightGray);  //makes a nice light gray background
        drawFamily(foo, adult, child);   // calls the drawFamily method; all done!
    }
    
    public void drawFamily(Graphics foo, int adult, int child) {
        // declare some x and y variables for the people; we only use 2 y values,
        // because we presume the adults and children are the same height.
        int x1=5, x2=175, x3=360, x4=440, y1=25, y2=75;
        
        // draw the people
        drawPerson(foo, x1, y1, adult, Color.black);    // draws first person, x1, y1
        drawPerson(foo, x2, y1, adult, Color.blue);     // draws second person, x2, y1
        drawPerson(foo, x3, y2, child, Color.red);      // draws third person, x3, y2
        drawPerson(foo, x4, y2, child, Color.darkGray);  // draws fourth person, x4, y2
        
        // add a caption, in black; `Family'
        foo.setColor(Color.black);
        foo.drawString("Family", 350, 200);
        
    }
    
    public void drawPerson(Graphics foo, int x, int y, int size, Color bColor) {
        /**
         * To avoid continually making confusing calculations, we set a few variables; presuming our
         * size to imply a size*size grid, we make values which stand for 5%, 10%, 20%, ..., 90% of
         * size.  These are used to place various points on our polygon, and to place the head of our 
         * person.
        */
        int five,ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety;
        five = (int)(size*.05f);
        ten = (int)(size*.1f);
        twenty = (int)(size*.2f);
        thirty = (int)(size*.3f);
        forty = (int)(size*.4f);
        fifty = (int)(size*.5f);
        sixty = (int)(size*.6f);
        seventy = (int)(size*.7f);
        eighty = (int)(size*.8f);
        ninety = (int)(size*.9f);
        
        /** these long integer arrays specify the x and y values of 26 points on the body Polygon
        * each one is place relative to x or y, the passed parameter that tells us where to draw our
        * person.  
        */
        int[] xval={x+thirty+five, x+seventy+five, x+ninety, x+ninety+five, x+seventy+five, x+sixty, x+fifty, x+fifty+five, x+sixty+five-1, x+eighty-1, x+eighty+five-1, x+ninety-1, x+eighty-1, x+sixty+five-1, x+thirty+five, x+twenty, x+ten+five, x+ten, x+twenty, x+thirty+five, x+forty, x+twenty, x+forty+five, x+thirty+five, x+twenty, x+ten+five};
        int[] yval={y+ten, y+fifty, y+thirty+five, y+forty, y+sixty, y+forty+five, y+fifty+five, y+sixty, y+fifty+1, y+sixty+five+1, y+sixty+1, y+sixty+five+1, y+seventy+five+1, y+sixty, y+ninety, y+seventy+five, y+eighty, y+seventy+five, y+sixty+five, y+eighty, y+seventy+five, y+fifty+five, y+thirty, y+twenty, y+thirty+five, y+thirty};
        Polygon body= new Polygon(xval, yval, 26);
        
        // sets the color to which ever color was passed as a parameter
        foo.setColor(bColor);
        
        // draws the oval(circle) for the head
        foo.fillOval(x+sixty, y+twenty, twenty, twenty);
        foo.fillPolygon(body);      // draws the body polygon, painstakingly defined in the arrays above.
    }
}
Back
Faces.java
/*
 * @(#)Faces.java 1.0 03/03/03
 *
 * This program draws a dozen faces in random spots in a 350*350
 * applet space.  The faces are drawn with either the happyFace or
 * sadFace method; the corresponding faces are either happy, or sad.
 * It would maybe have been better to make a face class and call
 * Face.happy() or Face.sad(), but I have enough to do without doing
 * extra work that isn't part of the assignment.
 *
 */
//obligatory import statements.  no export statements.
import java.awt.*;
import java.applet.*;
// head of the class
public class Faces extends Applet {
	//init method
	public void init() {
	}
// here's the paint method, by default the `main' method of the applet
	public void paint(Graphics g) {
		
		//sets the background to black; thank God for comments to clarify
		// this obfusticated mess!
		setBackground(Color.black);
		
		// this daunting block of code calls various `methods'; happyFace, 
		// and sadFace.
		happyFace(g);  	//this is the happyFace
		sadFace(g);		//this is the sadFace
		happyFace(g);
		sadFace(g);
		happyFace(g);
		sadFace(g);
		happyFace(g);
		sadFace(g);
		happyFace(g);
		sadFace(g);
		happyFace(g);
		sadFace(g);
	}
	
	
	// this method finds a number between 1 and 300 inclusive using Math.random
	public int anyInt()
	{
		return (int)(Math.random() * 300 + 1);
	}
	
	// this method draws the happyFace; it needs to get g from paint
	public void happyFace(Graphics g)
	{
		int x=anyInt(), y=anyInt(); //initializes two variables, using anyInt method
		
		g.setColor(Color.red); 		//happyFaces are red!
		g.fillOval(x, y, 50, 50);	// draws the face circle
		
		g.setColor(Color.yellow);	//eyes and mouth will be yellow
		g.fillOval(x+5, y+15, 15, 15);	// left eye
		g.fillOval(x+26, y+2, 15, 15);  //right eye
		g.fillArc(x+3, y+3, 44, 44, 214, 180);	//mouth
	}
	
	// sadFace method
	public void sadFace(Graphics g)
	{
		// this code is identical to happyFace except for as commented
		int x=anyInt(), y=anyInt(); 
		
		g.setColor(Color.cyan); 	//well, the colors are different
		g.fillOval(x, y, 50, 50);
		
		g.setColor(Color.lightGray);
		g.fillOval(x+5, y+15, 15, 15);
		g.fillOval(x+26, y+2, 15, 15);
		g.fillArc(x+17, y+19, 44, 44, 34, 180); // upside down mouth, different x,y
	}
}
Back
Loopy.java(chess board applet)
Back
/*
 * @(#)Loopy.java 1.0 03/03/24
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 ***************************************************************
 *  
 *	This program draws a chessboard of a specified size, 
 *	defaulting to a standard 8*8 board.  It adds random 
 *	images of chess pieces.  Each square has a 1/2 chance 
 * 	having a chess piece on it. 
 *	!!**** This means that very small boards (eg, the 		****
 * 	!!**** 2*2 board) may very well have no pieces on them. 	****	
 *      !!****								****
 *	!!****  This is not a bug, it is expected behavior.  		****
 *	Also, the gifs only contain black color information and
 * 	so blend in with the black squares.  Therefore, no
 * 	black squares appear to have pieces on them.  This is also 
 *	not a bug, this is expected behavior.  I am not 
 * 	taking this class to mess around with gifs in ms paint
 * 	or any other such Wastes Of Time.
 *
 */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Loopy extends Applet implements ActionListener{
	
	/* this will be used for our chess pieces */
	public Image image;			
	
	/* The next few variables are our GUI objects, a label,
	*  a textbox and a button, and a couple strings and 
	*  ints.  The ints are declared at the top for portability 
	*  and consistency.
	 */
	Label lbl=new Label("Enter a size: ");
	TextField size=new TextField("8");
	String strSize, message;
	Button n;
	public int i, j, intSize=8;
	
	/* This method starts our applet out by putting objects on the screen.  */
	public void init() {
		n = new Button("Go");
		add(lbl);
		add(size);
		add(n);
		n.addActionListener(this);  /* makes the button 'listenable' */
		
	}
	/* This is our event method.  Every time the button is pressed, 
	 * it is called.  If the numeric value has changed, then a new
	 * board is drawn.  Otherwise, the only change will be new random
	 * chess pieces.
	 */
	public void actionPerformed(ActionEvent e)
	{
		strSize=size.getText();
		intSize=Integer.parseInt(strSize);
		/* This tests to make sure the integer is in {2,3,...,15}*/
		if (!(intSize>=2) || !(intSize<=15)) 
			{ 	message="Size must be between 2 and 15 (inclusive)!";
				intSize=0;
				size.setText(""); /* box is made 'empty' again */
			}
		else
			{i=intSize; j=intSize;}  
		
		repaint();
	}
	public void paint(Graphics g) {
		
		
		int x=0, y=0, numChess; /* These are left over variables from another
					 * loop which is no more, since the chess pieces
					 * are now placed within this loop.  The variables
					 * are left here, declared but unused, for a memorial. */
		g.drawRect(39,39, intSize*40+1, intSize*40+1);
		for(j=1; j<=intSize; j++)
		{
			for(i=1; i<=intSize; i++)
			{
				/* this probably needs a little explanatory note.  What the
				 * loops condition does is to see if either index i or index
				 * j is divisible by two (even), but not both.  I didn't see
				 * the XOR operator listed in the book, so rather than waste
				 * time looking elsewhere, I used the logical equivalent to 
				 * p XOR q, that is, (p OR q) AND NOT(p AND q).  Thus, if 
				 * either i or j is even, but not if both are, the color
				 * will be set to black.  This creates the checkerboard
				 */
				if 	(((i%2==0) || (j%2==0)) && !((i%2==0) && (j%2==0)))
					g.setColor(Color.black);
				else
					g.setColor(Color.white);
				g.fillRect(j*40, i*40, 40, 40);
				if (getNum(2)==2)
					{
						// draws (P(1/2)) a chess piece on the square.
						image=whichOne();
						g.drawImage(image, j*40, i*40, 39, 39, this);
					}
			}
			
		}
		
		// if intSize is 0, then the condition in actionPerformed must have been 
		// unacceptable (under 2, over 15).  Display the error message.
		if (intSize==0)
			g.drawString(message, 20, 100);	
		
		
	}
	// this method returns a random number between 1 and n, its argument.
	public int getNum(int n)
	{
		int x;
		x=(int)(Math.random() * (double)n + 1);
		return x;
	}
	// this method gets a random # and uses it to pick a chess piece.  It uses
	// 32 to simulate the approximate probability of coming up with any one 
	// piece; there is only one way to get a black king, but eight ways to 
	// get a white pawn, and so on.
	public Image whichOne()
	{
		int r=getNum(32);
		Image q;
		switch (r)
		{
			
			case 1:
				q=getImage(getCodeBase(), "pics/kingw.gif");
				break;
			case 2:
				q=getImage(getCodeBase(), "pics/kingb.gif");
				break;
			case 3:
				q=getImage(getCodeBase(), "pics/queenw.gif");
				break;
			case 4:
				q=getImage(getCodeBase(), "pics/queenb.gif");
				break;
			case 5: case 6:
				q=getImage(getCodeBase(), "pics/knightw.gif");
				break;
			case 7: case 8:
				q=getImage(getCodeBase(), "pics/knightb.gif");
				break;
			case 9: case 10:
				q=getImage(getCodeBase(), "pics/bishopw.gif");
				break;
			case 11: case 12:
				q=getImage(getCodeBase(), "pics/bishopb.gif");
				break;
			case 13: case 14: 
				q=getImage(getCodeBase(), "pics/rookw.gif");
				break;
			case 15: case 16:
				q=getImage(getCodeBase(), "pics/rookb.gif");   
				break;
			case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: 
				q=getImage(getCodeBase(), "pics/pawnw.gif");
				break;
			case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32:
				q=getImage(getCodeBase(), "pics/pawnb.gif");
				break;
			default:
				q=getImage(getCodeBase(), "pics/pawnw.gif");
				break;
		}
		return q;	
	}
}
Back
StopLight.java(a stoplight)
Back
/**
 * @author pcrissman
 * 
 * project StopLight  -- this makes a stoplight.  It uses a couple java2D api methods
 * just to make it look nicer.
 *
**/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class StopLight extends Applet implements ActionListener
{
	Button stop, go, wait;  // these are our buttons
	int nColor;		// this will carry the color info for the lights
	public void init()	// this method adds our Button objects
	{
		nColor=0;
		stop= new Button("Stop!");
		go= new Button("Go!");
		wait= new Button("Wait!");
		add(stop);
		add(go);
		add(wait);
		stop.addActionListener(this);
		go.addActionListener(this);
		wait.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)	// changes nColor based on 
							//which button was clicked
	{
		Button clicked = (Button)e.getSource();
		// see the switch in the paint method
		if (clicked==stop)
			nColor=1;
		if (clicked==go)
			nColor=2;
		if (clicked==wait)
			nColor=3;
		
	
		repaint();
	}
	public void paint(Graphics g)	// this does (almost) all our drawing
	{
		Graphics2D g2 = (Graphics2D)g;		//Graphics2D object
		// these next four blocks declare some arrays for the polygons
		int[] xval1={110, 130, 130, 110};
		int[] yval1={110, 95, 270, 255};
		int[] xval2={130, 170, 170, 130};
		int[] yval2={95, 104, 250, 270};
		int[] xval3={0, 20, 215, 220, 0};
		int[] yval3={0, 0, 105, 110, 20};
		int[] xval4={0, 220, 215, 0};
		int[] yval4={20, 110, 115, 50};
		// here the polygons are defined.  A careful reader will note
		// that I added the `arm' after the fact.
		Polygon top=new Polygon(xval3, yval3, 5);
		Polygon bottom=new Polygon(xval4, yval4, 4);
		Polygon leftSide=new Polygon(xval1, yval1, 4);
		Polygon rightSide=new Polygon(xval2, yval2, 4);
	
		// sets antialiasing on to make it all look smoother
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        			RenderingHints.VALUE_ANTIALIAS_ON);
		
		// we won't see this much, but at least the color will be close
		// when the applet flickers		
		setBackground(Color.cyan);
		// makes the gradient for the background rectangle, and makes the rect.
		g2.setPaint(new GradientPaint(1, 1, Color.cyan, 150, 150, Color.blue, true)); 
		g2.fillRect(1,1,298,298);
		//next, draw the forms
		
		g.setColor(Color.black);
		g.fillPolygon(top);
		g.setColor(Color.darkGray);
		g.fillPolygon(bottom);
		g.fillRect(142, 90, 6, 12);
		
		g.setColor(Color.black);
		g.fillPolygon(leftSide);
		g.setColor(Color.darkGray);
		g.fillPolygon(rightSide);
		// finally, check what color the light should be, and call the
		// lights method.
		switch (nColor)
		{
			case 1:
				lights(g, Color.red, Color.gray, Color.gray);
				break;
			case 2:
				lights(g, Color.gray, Color.gray, Color.green);
				break;
			case 3:
				lights(g, Color.gray, Color.yellow, Color.gray);
				break;
			default:
				lights(g, Color.gray, Color.gray, Color.gray);
				break;
		}		
		
		
	}
	//this is the lights method.  it draws our circles and colors them.
	public void lights(Graphics g, Color one, Color two, Color three)
	{
		g.setColor(one);		//one, two and three are colors
		g.fillOval(140, 110, 20, 40);
		g.setColor(two);
		g.fillOval(140, 160, 20, 40);
		g.setColor(three);
		g.fillOval(140, 210, 20, 40);
	}
	
}
Back