Eric Boyd - Assignment 5

Deck of Cards Applet
Assignment 4.3, p218:


// Deck of Cards applet
// Eric Boyd
//
// July 24, 2000
//
// Creates and displays a deck of cards [objects]. The deck is then randomized and displayed again.
//
// Assignment 4.3, p218

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Deal extends Applet {
	
		// deck is for our vanilla, organized deck. randomDeck is for the shuffled one.
	private Card[] deck = new Card[52];
	private Card[] randomDeck = new Card[52];
	
		// To load the images properly
	private MediaTracker tracker  = new MediaTracker(this);
	
	public void init() {
	
		int currentFaceNum = 1; // Ace (1), 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack (11), Queen (12), King (13)
		int currentSuitNum = 0; // 0 = clubs, 1 = diamonds, 2 = hearts, 3 = spades
				
			// Space for our images
		Image[] suits = new Image[4];		
				
			// Load the images off the drive (or web .. still off the remote drive)
		suits[0] = getImage(getDocumentBase(), "clubs.gif");
		suits[1] = getImage(getDocumentBase(), "diamonds.gif");
		suits[2] = getImage(getDocumentBase(), "hearts.gif");
		suits[3] = getImage(getDocumentBase(), "spades.gif");
 
 			// Load the images into the media tracker.
 		for (int i=0; i < 4; i++) tracker.addImage(suits[i], 0);
 
		// This for loop initializes the cards in order, ace 1-13, clubs 1-13, etc.
					
		for (int i=0; i < 52; i++) {

			deck[i] = new Card(currentSuitNum, currentFaceNum, suits);
			
			if (currentFaceNum == 13) {
				currentFaceNum = 1;		// Start at 1 again
				currentSuitNum++;		// Move to the next suite	
			} else { 			
				currentFaceNum++;
			}			
		}
		
		// Randomization code

			// Set up a number array that will hold cards available to be placed in the shuffled deck
		int[] cardStack = new int[52];		
		
			// Set values to those numbers.
		for (int i=0; i < 52; i++)
			cardStack[i] = i;
		
		int getCard = 0;		// Which card to put into the shuffled deck
		int cardsLeft = 52;		// How many cards left to put into the shuffled deck
		int cardSlot = 0;		// Which place to put the shuffled card into
		
			// While our randomDeck isn't filled, find a new card to stick in it
		while (cardSlot < 52) {

				// Get a number coinciding with the availability array (cardStack)
			getCard = (int)(Math.random() * cardsLeft);
			
				// Place that card into the finished deck
			randomDeck[cardSlot] = deck[cardStack[getCard]];
			
				// Next slot
			cardSlot++;
			
				// We need to shrink the availibility array now, so set up a temporary one to store the data
			int[] tempStack = new int[cardsLeft];
			
				// Make the temporary equal to the old availibility stack
			tempStack = cardStack;
				// One less card to place
			cardsLeft--;
			
				// Make the new availibility array (one smaller)
			cardStack = new int[cardsLeft];
			
				// Read the temporary array into the new one
				// It skips over the card just placed into the deck (getCard) by the if - else in the middle
				// 
				// The temporary array is 1 larger than the new availibility array, so i+1 element is safe to reference
			for (int i=0; i < cardsLeft; i++) {
			
				if (i < getCard) {
					cardStack[i] = tempStack[i];
				} else {
					cardStack[i] = tempStack[i+1];
				}
			}
		} 
		
				
			// This try / catch is to make sure that the images are loaded properly before paint is called.
		try { tracker.waitForID(0);	} 
		catch (InterruptedException e) { return; }		
	}

	public void paint(Graphics g) {

			// k keeps track of the current card
		int k=0;
		
			// Whiteout the background
		g.setColor(Color.white);
		g.fillRect(0, 0, 550, 550);
		
			// Get rid of the standard font.
		Font stdFont = new Font("Verdana", Font.BOLD, 9); 
		g.setFont(stdFont);

		g.setColor(Color.black);
		g.drawString("Sorted", 5, 15);
		g.drawString("Random", 5, 215);
		
			// Display the cards
		for (int i=0; i < 4; i++) {
			for (int j=0; j < 13; j++) {
				deck[k].display(g, j*35 + (i*10) + 10, (i*20) + 20);
				randomDeck[k].display(g, j*35 + (i*10) + 10, (i*50) + 220);
				k++;
			}
		}
	}
}

class Card {

		// The images of the suits, along with the names and sizes of the cards, don't change. 
	private static Image[] suits;
	private static int width=75, height=110;
	private static String[] cardNames = new String[4];

		// The values for individual cards
	private int suit;
	private int face;
	
	public Card(int suit, int face, Image[] suits) {
	
				// Set the individual values
		this.suit = suit;		
		this.face = face;	
		
			// Image array: 0 = clubs, 1 = diamonds, 2 = hearts, 3 = spades
		this.suits = suits;
		
			// Fill the cardName array
		cardNames[0] = " Ace";
		cardNames[1] = "Jack"; 
		cardNames[2] = "Queen"; 
		cardNames[3] = "King";
	}

	public void display(Graphics g, int x, int y) {
		
			// Card backgrounds
		g.setColor(Color.white);
		g.fillRect(x, y, width, height);
			
			// Card borders
		g.setColor(Color.black);
		g.drawRect(x, y, width, height);
		
			// Draw the suit image
		g.drawImage(suits[suit], x+3, y+4, null);
		
			// Space for the text on the card
		String cardText;
		
			// Set the text on the card 
			// If it's a word value, use the cardName array
		if ((face == 1) || (face > 10)) {
		
			if (face == 1) {cardText = cardNames[0];}		// An ace
			else {cardText = cardNames[face-10];}		// Another named card
			
		} else {
			cardText = "   " + face;
		}
		
			// Draw the text on the card
		g.setColor(Color.black);
		g.drawString(cardText, x+5, y+42);
	}
}

Click here for the Deal Class File
Click here for the Card Class File



Dinner Party Applet
Assignment 4.6, p218:



// Dinner Party applet
// Eric Boyd
//
// July 24, 2000
//
// Initilizes and draws a diagram of a seating placement for a dinner party.
// The table is, for this applet, oval and the guests are seated counter-clockwise.
// The places are color-coded and labelled.
//
// The names are alphabetic and fictional (except Clarice Dozier, my high school math 
// teacher - she happened to fit)
//
// Assignment 4.6, p218

import java.applet.Applet;
import java.awt.*;

public class Dinner extends Applet {

		// Declare the number of dinner guests and create an array for them
	private static int numGuests = 10;
	private Diner[] guests = new Diner[numGuests];
	
		// Declare some drawing values
	private static int tableWidth = 300, tableHeight = 150;
	private static int tableX = 100, tableY = 75;
	
	public void init() {

			// Initialize our guests
			// First, Last, Gender, Seat
		guests[0] = new Diner("Albert", "Bumpkin", 'm', 1);
		guests[1] = new Diner("Clarice", "Dozier", 'f', 2);
		guests[2] = new Diner("Eldrich", "Fong", 'm', 3);
		guests[3] = new Diner("Gertrude", "Humphry", 'f', 4);
		guests[4] = new Diner("Israel", "Jones", 'm', 5);
		guests[5] = new Diner("Kevin", "Larue", 'm', 6);
		guests[6] = new Diner("Mona", "Newhart", 'f', 7);
		guests[7] = new Diner("Oprah", "Pinky", 'f', 8);
		guests[8] = new Diner("Quenton", "Royce", 'm', 9);
		guests[9] = new Diner("Silvia", "Tymes", 'f', 10);
	}

	public void paint(Graphics g) {

			// Get rid of the standard font.
		Font stdFont = new Font("Verdana", Font.BOLD, 9); 
		g.setFont(stdFont);
		
			// Draw a nice background
		g.setColor(Color.white);
		g.fillRect(0, 0, 500, 300);
	
			// Draw the table
		g.setColor(Color.black);
		g.fillOval(tableX, tableY, tableWidth, tableHeight);
		
			// Placement algorithm! 
			// Based on the number of guests, determine the radians seperating them
					
		double guestArc = ((2*Math.PI) / numGuests);
		double currentArc = 0;		
		
			// Loop through the guests
		for (int i=0; i < numGuests; i++) {
		
				// Calculate x and y values for the guest based on current angle
			double guestX = (tableWidth*.65) * Math.cos(currentArc) + (0) * Math.sin(currentArc);
			double guestY = (0) * Math.cos(currentArc) - (tableHeight*.75) * Math.sin(currentArc);
			
				// Display the guest, centered at middlepoint of table. Casting to convert coords.
			guests[i].display(g, (int)(guestX) + (tableX + (int)(tableWidth/2)), (int)(guestY)+(tableY + (int)(tableHeight/2)));
			
				// Move to next guest angle
			currentArc = currentArc + guestArc;
		}
	}
}

class Diner {

		// Declare the necessary vars
	private String firstName, lastName;
	private char gender;
	private int location;
	
		// Declare some drawing properties
	private int height = 60, width = 60;
	private Color mainColor;

	public Diner(String firstName, String lastName, char gender, int location) {
	
		this.firstName = firstName;
		this.lastName = lastName;
		this.gender = gender;
		this.location = location;
		
			// Set the colors for male and female. male = blue, female = red
		if (gender == 'm') mainColor = new Color(0, 0, 220);
		if (gender == 'f') mainColor = new Color(220, 0, 0);
		
	}
		
	public void display(Graphics g, int x, int y) {
		
			// Draw the diner's circle in the specified color
		g.setColor(mainColor);
		g.drawOval(x - (int)(width/2), y - (int)(height/2), width, height);

			// Print the diner's first and last names centered in the circle
		g.setColor(Color.black);
		g.drawString(firstName, x-((int)(firstName.length()*5.5)/2), y-5);
		g.drawString(lastName, x-((int)(lastName.length()*5.5)/2), y+5);
		
			// Print the location number
		g.drawString(""+location, x-2, y+20);
	}
}
Click here for the Dinner Class File
Click here for the Diner Class File