import java.awt.*; import java.applet.*; import java.net.*; public class CardShow extends Applet { // Suites Choice suites = new Choice (); // Values Choice values = new Choice (); // Current card selection Label cardSelection = new Label (); // Picture Frame PictureFrame pictureFrame = new PictureFrame (this, 100, 100); // Initialize my applet public void init () { // Initialize the suites initSuites (); // Initialize the values initValues (); // set the selection string setCardSelection (); // // Add all the GUI items // // Add the choice objects add (suites); add (values); // Add the label add (cardSelection); // Add picture frame add ("Center", pictureFrame); } public boolean action (Event event, Object what) { // If the action was on the suites or values choice objects if (event.target == suites || event.target == values) { // reset the card selection setCardSelection (); } return true; } private void setCardSelection () { // // Get the strings from the choices // // Get the suit String suit = suites.getSelectedItem (); // Get the value String value = values.getSelectedItem (); // Reset the label cardSelection.setText (suit + " " + value); // Redraw the picture pictureFrame.repaint (); } private void initSuites () { // Add the suites suites.addItem ("Spades"); suites.addItem ("Clubs"); suites.addItem ("Diamonds"); suites.addItem ("Hearts"); } private void initValues () { // Add the values values.addItem ("2"); values.addItem ("3"); values.addItem ("4"); values.addItem ("5"); values.addItem ("6"); values.addItem ("7"); values.addItem ("8"); values.addItem ("9"); values.addItem ("10"); values.addItem ("Jack"); values.addItem ("Queen"); values.addItem ("King"); values.addItem ("Ace"); } private char suitToFilename (String suit) { // // Conversion from suit string to suit part of filename // // Grab the first character and lower case it return Character.toLowerCase (suit.charAt (0)); } private char valueToFilename (String value) { // // Conversion from value string to value part of filename // // Grab the first character char firstChar = value.charAt (0); // Lowercase the colored cards switch (firstChar) { case 'J': case 'Q': case 'K': case 'A': firstChar = Character.toLowerCase (firstChar); } // Special case for value 10 if (firstChar == '1') { firstChar = 't'; } // // All others values (2 through 9) do not need any conversions // return firstChar; } public String filename () { // Get the suit String suit = suites.getSelectedItem (); // Get the value String value = values.getSelectedItem (); // Get the character for the suit char s = suitToFilename (suit); // Get the character for the value char v = valueToFilename (value); // Make the name of the image String imageName = "images/" + String.valueOf (v) + String.valueOf (s) + ".gif"; // Return the image name return imageName; } } class PictureFrame extends Canvas { // Reference to the CardShow object CardShow cardShow; PictureFrame (CardShow c, int width, int height) { // Remember the reference cardShow = c; // Resize self resize (width, height); // Repaint repaint (); } public void update (Graphics graphics) { displayImage (graphics); } public void paint (Graphics graphics) { displayImage (graphics); } private void displayImage (Graphics graphics) { // Get the selected card's filename String filename = cardShow.filename (); // Try to get the image from the server where the file came from try { // Create a URL object for the image file URL url = new URL (cardShow.getCodeBase () + filename); // Get the image Image image = Toolkit.getDefaultToolkit ().getImage (url); // Get the width of the image int imageWidth = image.getWidth (this); // Get the height of the image int imageHeight = image.getHeight (this); // Get the width of the canvas int myWidth = bounds ().width; // Get the height of the canvas int myHeight = bounds ().height; // Starting width of the image int startingWidthOfImage = (myWidth - imageWidth) / 2; // Starting height of the image int startingHeightOfImage = (myHeight - imageHeight) / 2; // Draw the image graphics.drawImage (image, startingWidthOfImage, startingHeightOfImage, this); } // Catch any exceptions raised during downloading the images catch (Exception exce) { // nothing } } }