//*******************************************************************
// Loads a cards deck imagefile and creates full card pictures
// Programmer : H K Gupta
// The best thing is that it will not understand that
//        java is "Write once and deploy everywhere"
//*******************************************************************

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

public  class HridayeshLoadCards {
        private int cardW = 73;
        private int cardH = 97;
        private int noOfCardImages = 53; // 52 + upturned Image index 52 holds upturned Image

        private Image[] cardImage;
        private MediaTracker    tracker;
        private Image cardImages;
        private int[] suitColor = {0, 1, 1, 0}; // 0 is black, 1 is red
        private Image[] partImage;
        private Image[] partImageReversed;
        private int nPartImages = 47;
        private Color cardBackGroundColor = Color.blue;
        //        7 * 3 grid       A2 3  4   5    6     7      8       9        10         ;
        private String suitXPos = "1111110022001220002220001222000112220000122220000112222";
        private String suitYPos = "2130240404042040240240241024024130240246302460246150246";
        private int[] suitStart= {0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 54};

        public  int     getCardW() {
                return  cardW;
        }

        public  int     getCardH() {
                return  cardH;
        }

        public  Image   getCardImage(int i) {
                if((i < 0) || (i > 51)) return cardImage[52];
                return cardImage[i];
        }

        private Applet  parent;

        public  HridayeshLoadCards(Applet parent) {
                this.parent = parent;
                cardImages = parent.getImage(parent.getCodeBase(), "HridayeshCardImages.gif");
                tracker = new MediaTracker(parent);
                tracker.addImage(cardImages, 0);
                try {
                        tracker.waitForAll();
                }
                catch (InterruptedException e) {cardBackGroundColor = Color.red;}

                cardImage = new Image[noOfCardImages];
                //copy the partial images in exact size buffers to maintain compatibility with jdk 1.0.2
                partImage = new Image[nPartImages];
                partImageReversed = new Image[nPartImages];
                Graphics g;
                for(int i = 0; i < 4; i++) // load J Q K part Images
                        for(int j = 0; j < 3; j++) {
                                loadPart(i * 3 + j, 47, 37, j * 49 + 1, i * 39 + 1);
                                loadPartReversed(i * 3 + j, 47, 37, 298 - (j * 49), 172 - (i * 39));
                        }
                for(int i = 0; i < 2; i++) // Load numerals
                        for(int j = 0; j < 13; j++) {
                                loadPart(12 + i * 13 + j, 11, 16, 147 + i * 12, j * 16);
                                loadPartReversed(12 + i * 13 + j, 11, 16, 187 - (i * 12), 193 - (j * 16));
                        }
                for(int i = 0; i < 4; i++) {
                        loadPart(38 + i, 15, 18, 45 + i * 15, 157);
                        loadPartReversed(38 + i, 15, 18, 286 - i * 15, 34);
                }
                for(int i = 0; i < 4; i++) {
                        loadPart(42 + i, 11, 12, 45 + i * 11, 177);
                        loadPartReversed(42 + i, 11, 12, 290 - i * 11, 19);
                }
                loadPart(46, 45, 53, 0, 156); // Ace of spade picture

                // create card images by using part images
                for(int i = 0; i < 4; i++) {
                        for(int j = 0; j < 13; j++) {
                                cardImage[i * 13 + j] = createBlankCard();
                                g = cardImage[i * 13 + j].getGraphics();
                                g.drawImage(partImage[12 + suitColor[i] * 13 + j], 2, 2, parent);
                                g.drawImage(partImage[42 + i], 2, 19, parent);
                                g.drawImage(partImageReversed[12 + suitColor[i] * 13 + j], 59, 78, parent);
                                g.drawImage(partImageReversed[42 + i], 60, 65, parent);
                                if((j > 8) && (j < 12)) { // draw picture
                                        g.drawImage(partImageReversed[i * 3 + j - 9], 13, 47, parent);
                                        g.drawImage(partImage[i * 3 + j - 9], 13, 11, parent);
                                }
                                else {
                                        int st = j + 1; if(j == 12) st = 0; // Ace is 12
                                        int iStart = suitStart[st];
                                        for(int k = 0; k <= st; k++) {
                                                int xPos = (int)suitXPos.charAt(k + iStart) - (int)'0';
                                                int yPos = (int)suitYPos.charAt(k + iStart) - (int)'0';
                                                xPos = 14 + xPos * 15;
                                                boolean invertedImage = false;
                                                if(st < 8) {
                                                        if(yPos > 2) invertedImage = true;
                                                        yPos = 9 + yPos * 15;
                                                }
                                                else {
                                                        if(yPos > 3) invertedImage = true;
                                                        yPos = 10 + yPos * 10;
                                                }
                                                if((i != 3) || (j != 12)) {
                                                        if(invertedImage) {
                                                                g.drawImage(partImageReversed[38 + i], xPos, yPos, parent);
                                                        }
                                                        else {
                                                                g.drawImage(partImage[38 + i], xPos, yPos, parent);
                                                        }
                                                }
                                                else {
                                                        g.drawImage(partImage[46], 14, 22, parent);
                                                }
                                        }
                                }
                        }
                }
                cardImage[52] = createBlankCard();
                g = cardImage[52].getGraphics();
                g.setColor(cardBackGroundColor);
                g.fillRect(10, 10, cardW - 20, cardH - 20);
        }

        private Image createBlankCard() {
                Image card;
                Graphics g;
                card = parent.createImage(cardW, cardH);
                g = card.getGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, cardW, cardH);
                g.setColor(Color.black);
                g.drawRect(0, 0, cardW - 1, cardH -1);
                return card;
        }

        private void loadPart(int i, int w, int h, int xOff, int yOff) {
                Graphics g;
                partImage[i] = parent.createImage(w, h);
                g = partImage[i].getGraphics();
                g.drawImage(cardImages, -xOff, -yOff, parent);
        }

        private void loadPartReversed(int i, int w, int h, int xOff, int yOff) {
                Graphics g;
                partImageReversed[i] = parent.createImage(w, h);
                g = partImageReversed[i].getGraphics();
                g.drawImage(cardImages, -xOff, -yOff, parent);
        }
}

