// A Generic Card dealing class // Unit 11-F: Illustrates use of JAR files import java.awt.*; import java.applet.*; public class Cards extends Applet { static final int NONE = 0; static final int SHOW = 1; static final int DEAL = 2; int mode = NONE; Image[] deck = new Image[52]; Image[] hand = new Image[5]; AudioClip click = null; AudioClip bell = null; // Buttons Button deal = new Button("Deal"); Button show = new Button("Show Deck"); Button shuffle = new Button("Shuffle"); int[] curDeck; int curCard = 52; Panel table = new Panel(); public void init() { setLayout(new BorderLayout(5,5)); Panel p = new Panel(new FlowLayout(FlowLayout.RIGHT)); // 1. The Button Panel p.setBackground(new Color(128,0,128)); p.add(shuffle); p.add(show); p.add(deal); add("South", p); // 2. The Table setBackground(new Color(0, 196, 0)); table.setBackground(new Color(0, 196, 0)); table.setLayout(new BorderLayout()); table.add("Center", new Label("Images Loading...", Label.CENTER)); add("Center", table); // 3. Load the images MediaTracker mt = new MediaTracker(this); for (int i = 0; i < 13; i++) { deck[i] = getImage(getCodeBase(), "cards/c" + (i + 1) +".gif"); deck[13 + i] = getImage(getCodeBase(), "cards/d" + (i + 1) +".gif"); deck[26 + i] = getImage(getCodeBase(), "cards/h" + (i + 1) +".gif"); deck[39 + i] = getImage(getCodeBase(), "cards/s" + (i + 1) +".gif"); if (deck[i] == null) System.out.println("Couldn't load"); mt.addImage(deck[i], 0); mt.addImage(deck[13 + i], 0); mt.addImage(deck[26 + i], 0); mt.addImage(deck[39 + i], 0); } try { mt.waitForAll(); remove(table); } catch (InterruptedException ie) { table.add("Center", new Label("AAAARGGGGGGG......", Label.CENTER)); } click = getAudioClip(getCodeBase(), "audio/click.au"); bell = getAudioClip(getCodeBase(), "audio/cowbell.au"); } // Handle the Button Presses public boolean action(Event e, Object o) { mode = NONE; if (e.target == show) { out("Show pressed"); mode = SHOW; repaint(); bell.play(); } else if (e.target == deal) { out("Deal pressed"); dealCards(); mode = DEAL; repaint(); } else if (e.target == shuffle) { out("Shuffle pressed"); curDeck = shuffleCards(); curCard = 0; } return true; } private void dealCards() { // 1. Check to make sure we can deal if (curCard >= 52 - 5) // Not enough cards { curDeck = shuffleCards(); curCard = 0; } // 2. Deal the cards int i; for (i = 0; i < 5 && curCard+i < deck.length; i++) { hand[i] = deck[curDeck[curCard+i]]; } curCard += i; } int[] shuffleCards() { int[] a = new int[52]; for (int i = 0; i < 52; i++) a[i] = i; for (int i = 0; i < 52; i++) { int temp = a[i]; int idx2 = (int) (Math.random() * 52); a[i] = a[idx2]; a[idx2] = temp; } return a; } public void paint(Graphics g) { int imw = deck[0].getWidth(this) + 4; int imh = deck[0].getHeight(this)+ 4; g.setColor(getBackground()); g.fillRect(0, 0, size().width, size().height); out("In paint() # 1"); switch (mode) { case DEAL: out("In paint # 2"); g.drawString("#2", 100, 100); for (int i = 0; i < 5; i++) { g.drawImage(hand[i], i * imw, 0, this); click.play(); } break; case SHOW: out("In paint # 3"); g.drawString("#3", 100, 100); for (int i = 0; i < 13; i++) { g.drawImage(deck[i], i * imw, 0, this); g.drawImage(deck[13 + i], i * imw, imh, this); g.drawImage(deck[26 + i], i * imw, imh * 2, this); g.drawImage(deck[39 + i], i * imw, imh * 3, this); click.play(); } break; } g.dispose(); } // Debuffing methods static final boolean DEBUG = false; void out(String s) { if (DEBUG) System.out.println(s); } }