package com.josesandoval.magic;
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MagicTrick extends Applet {
Image[] cardImages;
private Image deckImage;
private Panel ivjPanelCards = null;
private Panel ivjPanelMenu = null;
IvjEventHandler ivjEventHandler = new IvjEventHandler();
private Button ivjButtonPile1 = null;
private Button ivjButtonPile2 = null;
private Button ivjButtonPile3 = null;
private Panel ivjPanelBegin = null;
private FlowLayout ivjPanelBeginFlowLayout = null;
private Deck deck;
private com.josesandoval.magic.Card[] playCards;
private com.josesandoval.magic.Card[] pile1;
private com.josesandoval.magic.Card[] pile2;
private com.josesandoval.magic.Card[] pile3;
private java.util.Hashtable cardTable;
private int countShuffles;
private final static int xBegin = 10;
private final static int yBegin = 10;
private final static int cardWidth = 73;
private final static int cardHeight = 107;
private Button ivjButtonStart = null;
private java.awt.Image backCard;
class IvjEventHandler implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (e.getSource() == MagicTrick.this.getButtonPile1())
connEtoC2(e);
if (e.getSource() == MagicTrick.this.getButtonPile2())
connEtoC3(e);
if (e.getSource() == MagicTrick.this.getButtonPile3())
connEtoC4(e);
if (e.getSource() == MagicTrick.this.getButtonStart())
connEtoC5(e);
};
};
private java.awt.Image offScreenBuffer;
public void buttonPile1_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
doSelectPile1();
doDisplayPiles(getPanelCards().getGraphics());
doCheckCountShuffles();
}
public void buttonPile2_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
doSelectPile2();
doDisplayPiles(getPanelCards().getGraphics());
doCheckCountShuffles();
}
public void buttonPile3_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
doSelectPile3();
doDisplayPiles(getPanelCards().getGraphics());
doCheckCountShuffles();
}
public void buttonStart_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
doResetToDeal();
doDisplayPiles(getPanelCards().getGraphics());
getButtonStart().setEnabled(false);
getButtonPile1().setEnabled(true);
getButtonPile2().setEnabled(true);
getButtonPile3().setEnabled(true);
doDisplayStatus(getPanelCards().getGraphics(), "Pick a card and click the button beside the pile it is in.");
}
private void connEtoC2(java.awt.event.ActionEvent arg1) {
try {
countShuffles++;
this.buttonPile1_ActionPerformed(arg1);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoC3(java.awt.event.ActionEvent arg1) {
try {
countShuffles++;
this.buttonPile2_ActionPerformed(arg1);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoC4(java.awt.event.ActionEvent arg1) {
try {
countShuffles++;
this.buttonPile3_ActionPerformed(arg1);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoC5(java.awt.event.ActionEvent arg1) {
try {
this.buttonStart_ActionPerformed(arg1);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void doCheckCountShuffles() {
if (countShuffles == 3) {
countShuffles = 0;
doDisplayAllBack(getPanelCards().getGraphics());
doDisplayChoosenCard(getPanelCards().getGraphics());
doDisplayStatus(getPanelCards().getGraphics(), "This is the card you picked.");
getButtonStart().setEnabled(true);
getButtonPile1().setEnabled(false);
getButtonPile2().setEnabled(false);
getButtonPile3().setEnabled(false);
initTrick();
} else {
doDisplayStatus(getPanelCards().getGraphics(), "Which pile is your card in?");
}
}
private void doDisplayAllBack(Graphics g) {
g.clearRect(0, 0, getPanelCards().getSize().width, getPanelCards().getSize().height);
for (int i = 0; i < 7; i++) {
g.drawImage(backCard, xBegin + (cardWidth * i), yBegin + (cardHeight * 0), null);
g.drawImage(backCard, xBegin + (cardWidth * i), yBegin + (cardHeight * 1), null);
g.drawImage(backCard, xBegin + (cardWidth * i), yBegin + (cardHeight * 2), null);
}
}
private void doDisplayChoosenCard(Graphics g) {
g.drawImage((Image) cardTable.get("" + playCards[10].getSuit() + "_" + playCards[10].getValue()), xBegin, yBegin, null);
}
private void doDisplayIntro(Graphics g) {
}
private void doDisplayPiles(Graphics g) {
g.clearRect(0, 0, getPanelCards().getSize().width, getPanelCards().getSize().height);
for (int i = 0; i < 7; i++) {
g.drawImage((Image) cardTable.get("" + pile1[i].getSuit() + "_" + pile1[i].getValue()), xBegin + (cardWidth * i), yBegin + (cardHeight * 0), null);
g.drawImage((Image) cardTable.get("" + pile2[i].getSuit() + "_" + pile2[i].getValue()), xBegin + (cardWidth * i), yBegin + (cardHeight * 1), null);
g.drawImage((Image) cardTable.get("" + pile3[i].getSuit() + "_" + pile3[i].getValue()), xBegin + (cardWidth * i), yBegin + (cardHeight * 2), null);
}
}
private void doDisplayStatus(Graphics g, String status) {
g.setFont(new java.awt.Font("Helvetica", java.awt.Font.BOLD, 16));
g.setColor(java.awt.Color.orange);
g.drawString(status, 40, 360);
}
private void doResetToDeal() {
for (int i = 0; i < 7; i++) {
playCards[i] = pile1[i];
playCards[i + 7] = pile2[i];
playCards[i + 14] = pile3[i];
}
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int switchFlag = 0;
for (int i = playCards.length - 1; i >= 0; i--) {
switch ((i + 1) % 3) {
case 0 :
pile1[counter1] = playCards[i];
counter1++;
break;
case 1 :
pile3[counter2] = playCards[i];
counter2++;
break;
case 2 :
pile2[counter3] = playCards[i];
counter3++;
break;
}
}
}
private void doSelectPile1() {
Card tmpPile[] = new Card[7];
tmpPile = pile1;
pile1 = pile2;
pile2 = tmpPile;
doResetToDeal();
}
private void doSelectPile2() {
doResetToDeal();
}
private void doSelectPile3() {
Card tmpPile[] = new Card[7];
tmpPile = pile2;
pile2 = pile3;
pile3 = tmpPile;
doResetToDeal();
}
public String getAppletInfo() {
return "MagicTrick\n"
+ "\n"
+ "This trick was taught to me, when I was around 10 years old, by my father.\n"
+ "Creation date: (25/12/2002 3:47:30 PM)\n"
+ "@author: Jose Sandoval\n"
+ "";
}
private java.awt.Button getButtonPile1() {
if (ivjButtonPile1 == null) {
try {
ivjButtonPile1 = new java.awt.Button();
ivjButtonPile1.setName("ButtonPile1");
ivjButtonPile1.setBounds(540, 50, 45, 23);
ivjButtonPile1.setEnabled(false);
ivjButtonPile1.setLabel("Pile 1");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjButtonPile1;
}
private java.awt.Button getButtonPile2() {
if (ivjButtonPile2 == null) {
try {
ivjButtonPile2 = new java.awt.Button();
ivjButtonPile2.setName("ButtonPile2");
ivjButtonPile2.setBounds(540, 155, 45, 23);
ivjButtonPile2.setEnabled(false);
ivjButtonPile2.setLabel("Pile 2");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjButtonPile2;
}
private java.awt.Button getButtonPile3() {
if (ivjButtonPile3 == null) {
try {
ivjButtonPile3 = new java.awt.Button();
ivjButtonPile3.setName("ButtonPile3");
ivjButtonPile3.setBounds(540, 260, 45, 23);
ivjButtonPile3.setEnabled(false);
ivjButtonPile3.setLabel("Pile 3");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjButtonPile3;
}
private java.awt.Button getButtonStart() {
if (ivjButtonStart == null) {
try {
ivjButtonStart = new java.awt.Button();
ivjButtonStart.setName("ButtonStart");
ivjButtonStart.setLabel("Play Game");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjButtonStart;
}
private java.awt.Panel getPanelBegin() {
if (ivjPanelBegin == null) {
try {
ivjPanelBegin = new java.awt.Panel();
ivjPanelBegin.setName("PanelBegin");
ivjPanelBegin.setLayout(getPanelBeginFlowLayout());
ivjPanelBegin.setBackground(java.awt.Color.gray);
getPanelBegin().add(getButtonStart(), getButtonStart().getName());
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjPanelBegin;
}
private java.awt.FlowLayout getPanelBeginFlowLayout() {
java.awt.FlowLayout ivjPanelBeginFlowLayout = null;
try {
ivjPanelBeginFlowLayout = new java.awt.FlowLayout();
ivjPanelBeginFlowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
};
return ivjPanelBeginFlowLayout;
}
private java.awt.Panel getPanelCards() {
if (ivjPanelCards == null) {
try {
ivjPanelCards = new java.awt.Panel();
ivjPanelCards.setName("PanelCards");
ivjPanelCards.setLayout(null);
ivjPanelCards.setBackground(new java.awt.Color(25, 105, 18));
getPanelCards().add(getButtonPile1(), getButtonPile1().getName());
getPanelCards().add(getButtonPile2(), getButtonPile2().getName());
getPanelCards().add(getButtonPile3(), getButtonPile3().getName());
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjPanelCards;
}
private java.awt.Panel getPanelMenu() {
if (ivjPanelMenu == null) {
try {
ivjPanelMenu = new java.awt.Panel();
ivjPanelMenu.setName("PanelMenu");
ivjPanelMenu.setLayout(new java.awt.GridBagLayout());
java.awt.GridBagConstraints constraintsPanelBegin = new java.awt.GridBagConstraints();
constraintsPanelBegin.gridx = 0;
constraintsPanelBegin.gridy = 0;
constraintsPanelBegin.fill = java.awt.GridBagConstraints.HORIZONTAL;
constraintsPanelBegin.anchor = java.awt.GridBagConstraints.EAST;
constraintsPanelBegin.weightx = 1.0;
constraintsPanelBegin.weighty = 1.0;
getPanelMenu().add(getPanelBegin(), constraintsPanelBegin);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjPanelMenu;
}
private void handleException(java.lang.Throwable exception) {
}
public void init() {
try {
super.init();
setName("MagicTrick");
setLayout(new java.awt.BorderLayout());
setSize(600, 420);
add(getPanelCards(), "Center");
add(getPanelMenu(), "South");
initConnections();
MediaTracker tracker = new MediaTracker(this);
playCards = new Card[21];
pile1 = new Card[7];
pile2 = new Card[7];
pile3 = new Card[7];
deck = new Deck();
initTrick();
Image wholeDeck = getImage(getCodeBase(), "images/cards.gif");
tracker.addImage(wholeDeck, 0);
try {
tracker.waitForID(0);
} catch (Exception e) {
e.printStackTrace();
}
cardTable = new java.util.Hashtable();
Image tmpImage = null;
java.awt.image.CropImageFilter filter = null;
int counter = 0;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
filter = new java.awt.image.CropImageFilter(cardWidth * j, (cardHeight - 10) * i, cardWidth, cardHeight - 10);
tmpImage = createImage(new java.awt.image.FilteredImageSource(wholeDeck.getSource(), filter));
tracker.addImage(tmpImage, ++counter);
cardTable.put("" + i + "_" + j, tmpImage);
}
}
filter = new java.awt.image.CropImageFilter(0, cardHeight - 10, cardWidth, cardHeight - 10);
backCard = createImage(new java.awt.image.FilteredImageSource(wholeDeck.getSource(), filter));
tracker.addImage(backCard, ++counter);
try {
tracker.waitForAll();
} catch (Exception e) {
e.printStackTrace();
}
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void initConnections() throws java.lang.Exception {
getButtonPile1().addActionListener(ivjEventHandler);
getButtonPile2().addActionListener(ivjEventHandler);
getButtonPile3().addActionListener(ivjEventHandler);
getButtonStart().addActionListener(ivjEventHandler);
}
private void initTrick() {
countShuffles = 0;
deck.shuffle();
for (int i = 0; i < 7; i++) {
pile1[i] = deck.getDeck()[i];
pile2[i] = deck.getDeck()[i + 7];
pile3[i] = deck.getDeck()[i + 14];
}
}
public static void main(java.lang.String[] args) {
try {
Frame frame = new java.awt.Frame();
MagicTrick aMagicTrick;
Class iiCls = Class.forName("com.josesandoval.magic.MagicTrick");
ClassLoader iiClsLoader = iiCls.getClassLoader();
aMagicTrick = (MagicTrick) java.beans.Beans.instantiate(iiClsLoader, "com.josesandoval.magic.MagicTrick");
frame.add("Center", aMagicTrick);
frame.setSize(aMagicTrick.getSize());
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
};
});
frame.show();
java.awt.Insets insets = frame.getInsets();
frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
frame.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception occurred in main() of java.applet.Applet");
exception.printStackTrace(System.out);
}
}
public void paint(Graphics g) {
paint(g);
}
public void start() {
}
public void stop() {
}
public void update(Graphics g) {
paint(g);
}
}