home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / PICTURE.ZIP / PictureGame.java < prev    next >
Encoding:
Java Source  |  1997-09-19  |  8.5 KB  |  273 lines

  1. import java.applet.Applet;
  2.  
  3.  
  4. import java.awt.*;
  5.  
  6.  
  7.  
  8.  
  9.  
  10. public class PictureGame extends Applet 
  11.  
  12.  
  13. {
  14.  
  15.  
  16.     private boolean laidOut = false, WordUpdate = true;
  17.  
  18.  
  19.     private int TotalLetters, TotalPictures, CurrentPicture, PreviousPicture, counter;
  20.  
  21.  
  22.     private int offsettop, offsetleft;
  23.  
  24.  
  25.     private Button cmdDone, cmdReset, cmdHelp, cmdLetter[];
  26.  
  27.  
  28.     private Label lblWordTyped;
  29.  
  30.  
  31.     private String ThingsName[], msgWon = "You Won!", msgLost = "Try again.";
  32.  
  33.  
  34.     private Image Things[];
  35.  
  36.  
  37.  
  38.  
  39.  
  40.     public void init() 
  41.  
  42.  
  43.     {
  44.  
  45.  
  46.         PictureGame window = new PictureGame();
  47.  
  48.  
  49.         Insets insets = window.insets();
  50.  
  51.  
  52.         TotalPictures = 35;
  53.  
  54.  
  55.         CurrentPicture = Random();
  56.  
  57.  
  58.  
  59.  
  60.  
  61.         Things = new Image[TotalPictures];
  62.  
  63.  
  64.         for(counter = 1; counter <= TotalPictures; counter++)
  65.  
  66.  
  67.         {
  68.  
  69.  
  70.             showStatus("Loading images/T"+counter+".gif");
  71.  
  72.  
  73.             Things[counter - 1] = getImage(getCodeBase(), "images/T"+counter+".gif");
  74.  
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.         ThingsName = new String[TotalPictures];
  83.  
  84.  
  85.         ThingsName[0] = "PICTURE";
  86.  
  87.  
  88.         ThingsName[1] ="BOOK";
  89.  
  90.  
  91.         ThingsName[2] = "BOX";
  92.  
  93.  
  94.         ThingsName[3] = "LOCK";
  95.  
  96.  
  97.         ThingsName[4] = "SUN";
  98.  
  99.  
  100.         ThingsName[5] = "CAMERA";
  101.  
  102.  
  103.         ThingsName[6] = "DOG";
  104.  
  105.  
  106.         ThingsName[7] = "DOOR";
  107.  
  108.  
  109.         ThingsName[8] = "EARTH";
  110.  
  111.  
  112.         ThingsName[9] = "EIGHT";
  113.  
  114.  
  115.         ThingsName[10] = "EYE";
  116.  
  117.  
  118.         ThingsName[11] ="FIRE";
  119.  
  120.  
  121.         ThingsName[12] = "FIVE";
  122.  
  123.  
  124.         ThingsName[13] = "FOUR";
  125.  
  126.  
  127.         ThingsName[14] = "HAND";
  128.  
  129.  
  130.         ThingsName[15] = "HOUSE";
  131.  
  132.  
  133.         ThingsName[16] = "KEY";
  134.  
  135.  
  136.         ThingsName[17] = "TV";
  137.  
  138.  
  139.         ThingsName[18] = "TREE";
  140.  
  141.  
  142.         ThingsName[19] = "LIGHTBULB";
  143.  
  144.  
  145.         ThingsName[20] = "MAILBOX";
  146.  
  147.  
  148.         ThingsName[21] ="NINE";
  149.  
  150.  
  151.         ThingsName[22] = "ONE";
  152.  
  153.  
  154.         ThingsName[23] = "PAINT";
  155.  
  156.  
  157.         ThingsName[24] = "PHONE";
  158.  
  159.  
  160.         ThingsName[25] = "SEVEN";
  161.  
  162.  
  163.         ThingsName[26] = "SIX";
  164.  
  165.  
  166.         ThingsName[27] = "SPIDER";
  167.  
  168.  
  169.         ThingsName[28] = "STAR";
  170.  
  171.  
  172.         ThingsName[29] = "COMPUTER";
  173.  
  174.  
  175.         ThingsName[30] = "THREE";
  176.  
  177.  
  178.         ThingsName[31] ="CASTLE";
  179.  
  180.  
  181.         ThingsName[32] = "TWO";
  182.  
  183.  
  184.         ThingsName[33] = "MONEY";
  185.  
  186.  
  187.         ThingsName[34] = "NEWSPAPER";
  188.  
  189.  
  190.  
  191.  
  192.  
  193.         //How do we know insets is valid here?
  194.  
  195.  
  196.         window.resize(400 + insets.left + insets.right, 300 + insets.top + insets.bottom);
  197.  
  198.  
  199.         offsettop = insets.top;
  200.  
  201.  
  202.         offsetleft = insets.left;
  203.  
  204.  
  205.         window.show();
  206.  
  207.  
  208.  
  209.  
  210.  
  211.     }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.     public PictureGame() 
  218.  
  219.  
  220.     {
  221.  
  222.  
  223.         super();
  224.  
  225.  
  226.         setLayout(null);
  227.  
  228.  
  229.         TotalLetters = 26;
  230.  
  231.  
  232.         cmdLetter = new Button[TotalLetters];
  233.  
  234.  
  235.  
  236.  
  237.  
  238.         cmdDone = new Button("Done");
  239.  
  240.  
  241.         cmdDone.setFont(new Font("Helvetica", Font.BOLD, 30));
  242.  
  243.  
  244.         add(cmdDone);
  245.  
  246.  
  247.  
  248.  
  249.  
  250.         cmdReset = new Button("Reset");
  251.  
  252.  
  253.         cmdReset.setFont(new Font("Helvetica", Font.BOLD, 30));
  254.  
  255.  
  256.         add(cmdReset);
  257.  
  258.  
  259.  
  260.  
  261.  
  262.         cmdHelp = new Button("Help");
  263.  
  264.  
  265.         cmdHelp.setFont(new Font("Helvetica", Font.BOLD, 30));
  266.  
  267.  
  268.         cmdHelp.disable();
  269.  
  270.  
  271.         add(cmdHelp);
  272.  
  273.  
  274.     
  275.  
  276.  
  277.         lblWordTyped = new Label();
  278.  
  279.  
  280.         lblWordTyped.setFont(new Font("Helvetica", Font.PLAIN, 20));
  281.  
  282.  
  283.         add(lblWordTyped);
  284.  
  285.  
  286.  
  287.  
  288.  
  289.         setFont(new Font("Helvetica", Font.PLAIN, 20));
  290.  
  291.  
  292.         cmdLetter[0] = new Button("A");
  293.  
  294.  
  295.         cmdLetter[1] = new Button("B");
  296.  
  297.  
  298.         cmdLetter[2] = new Button("C");
  299.  
  300.  
  301.         cmdLetter[3] = new Button("D");
  302.  
  303.  
  304.         cmdLetter[4] = new Button("E");
  305.  
  306.  
  307.         cmdLetter[5] = new Button("F");
  308.  
  309.  
  310.         cmdLetter[6] = new Button("G");
  311.  
  312.  
  313.         cmdLetter[7] = new Button("H");
  314.  
  315.  
  316.         cmdLetter[8] = new Button("I");
  317.  
  318.  
  319.         cmdLetter[9] = new Button("J");
  320.  
  321.  
  322.         cmdLetter[10] = new Button("K");
  323.  
  324.  
  325.         cmdLetter[11] = new Button("L");
  326.  
  327.  
  328.         cmdLetter[12] = new Button("M");
  329.  
  330.  
  331.         cmdLetter[13] = new Button("N");
  332.  
  333.  
  334.         cmdLetter[14] = new Button("O");
  335.  
  336.  
  337.         cmdLetter[15] = new Button("P");
  338.  
  339.  
  340.         cmdLetter[16] = new Button("Q");
  341.  
  342.  
  343.         cmdLetter[17] = new Button("R");
  344.  
  345.  
  346.         cmdLetter[18] = new Button("S");
  347.  
  348.  
  349.         cmdLetter[19] = new Button("T");
  350.  
  351.  
  352.         cmdLetter[20] = new Button("U");
  353.  
  354.  
  355.         cmdLetter[21] = new Button("V");
  356.  
  357.  
  358.         cmdLetter[22] = new Button("W");
  359.  
  360.  
  361.         cmdLetter[23] = new Button("X");
  362.  
  363.  
  364.         cmdLetter[24] = new Button("Y");
  365.  
  366.  
  367.         cmdLetter[25] = new Button("Z");
  368.  
  369.  
  370.  
  371.  
  372.  
  373.         for(counter = 0; counter < TotalLetters; ++counter)
  374.  
  375.  
  376.             add(cmdLetter[counter]);
  377.  
  378.  
  379.     }
  380.  
  381.  
  382.  
  383.  
  384.  
  385.     public void paint(Graphics g) 
  386.  
  387.  
  388.     {
  389.  
  390.  
  391.         g.setColor(Color.magenta);
  392.  
  393.  
  394.         g.fill3DRect(offsetleft,offsettop, 600, 350, true);
  395.  
  396.  
  397.         g.drawImage(Things[CurrentPicture], 275 + offsetleft, 210 + offsettop, this);
  398.  
  399.  
  400.         if (!laidOut) 
  401.  
  402.  
  403.         {
  404.  
  405.  
  406.             Insets insets = insets();
  407.  
  408.  
  409.             insets = insets(); 
  410.  
  411.  
  412.             cmdReset.reshape(130 + offsetleft, 300 + offsettop, 100, 30);
  413.  
  414.  
  415.             cmdDone.reshape(375 + offsetleft, 300 + offsettop, 100, 30);
  416.  
  417.  
  418.             cmdHelp.reshape(250 + offsetleft, 300 + offsettop, 100, 30);
  419.  
  420.  
  421.             for(counter = 0; counter < 8; ++counter)
  422.  
  423.  
  424.                                //      Left, Top, Right, Bottom
  425.  
  426.  
  427.                 cmdLetter[counter].reshape(  160 + (counter * 35), 20, 35, 30);
  428.  
  429.  
  430.             for(;counter < 18; ++counter)
  431.  
  432.  
  433.                 cmdLetter[counter].reshape(  130 + ((counter - 8) * 35), 80, 35, 30);
  434.  
  435.  
  436.             for(;counter < TotalLetters; ++counter)
  437.  
  438.  
  439.                 cmdLetter[counter].reshape(  160 + ((counter - 18) * 35), 140, 35, 30);
  440.  
  441.  
  442.  
  443.  
  444.  
  445.             laidOut = true;
  446.  
  447.  
  448.         }
  449.  
  450.  
  451.  
  452.  
  453.  
  454.         if(!WordUpdate)
  455.  
  456.  
  457.         {
  458.  
  459.  
  460.             g.setColor(Color.black);
  461.  
  462.  
  463.             g.drawString(lblWordTyped.getText(), 250 + offsetleft, 200+ offsettop);
  464.  
  465.  
  466.             g.setColor(Color.magenta);
  467.  
  468.  
  469.         }
  470.  
  471.  
  472.     }
  473.  
  474.  
  475.  
  476.  
  477.  
  478.     public boolean action(Event evt, Object obj)
  479.  
  480.  
  481.     {
  482.  
  483.  
  484.         if(lblWordTyped.getText() == msgWon || lblWordTyped.getText() == msgLost)
  485.  
  486.  
  487.             Reset();
  488.  
  489.  
  490.         if("A".equals(obj))
  491.  
  492.  
  493.             lblWordTyped.setText(lblWordTyped.getText() + "A");
  494.  
  495.  
  496.         if("B".equals(obj))
  497.  
  498.  
  499.             lblWordTyped.setText(lblWordTyped.getText() + "B");
  500.  
  501.  
  502.         if("C".equals(obj))
  503.  
  504.  
  505.             lblWordTyped.setText(lblWordTyped.getText() + "C");
  506.  
  507.  
  508.         if("D".equals(obj))
  509.  
  510.  
  511.             lblWordTyped.setText(lblWordTyped.getText() + "D");
  512.  
  513.  
  514.         if("E".equals(obj))
  515.  
  516.  
  517.             lblWordTyped.setText(lblWordTyped.getText() + "E");
  518.  
  519.  
  520.         if("F".equals(obj))
  521.  
  522.  
  523.             lblWordTyped.setText(lblWordTyped.getText() + "F");
  524.  
  525.  
  526.         if("G".equals(obj))
  527.  
  528.  
  529.             lblWordTyped.setText(lblWordTyped.getText() + "G");
  530.  
  531.  
  532.         if("H".equals(obj))
  533.  
  534.  
  535.             lblWordTyped.setText(lblWordTyped.getText() + "H");
  536.  
  537.  
  538.         if("I".equals(obj))
  539.  
  540.  
  541.             lblWordTyped.setText(lblWordTyped.getText() + "I");
  542.  
  543.  
  544.         if("J".equals(obj))
  545.  
  546.  
  547.             lblWordTyped.setText(lblWordTyped.getText() + "J");
  548.  
  549.  
  550.         if("K".equals(obj))
  551.  
  552.  
  553.             lblWordTyped.setText(lblWordTyped.getText() + "K");
  554.  
  555.  
  556.         if("L".equals(obj))
  557.  
  558.  
  559.             lblWordTyped.setText(lblWordTyped.getText() + "L");
  560.  
  561.  
  562.         if("M".equals(obj))
  563.  
  564.  
  565.             lblWordTyped.setText(lblWordTyped.getText() + "M");
  566.  
  567.  
  568.         if("N".equals(obj))
  569.  
  570.  
  571.             lblWordTyped.setText(lblWordTyped.getText() + "N");
  572.  
  573.  
  574.         if("O".equals(obj))
  575.  
  576.  
  577.             lblWordTyped.setText(lblWordTyped.getText() + "O");
  578.  
  579.  
  580.         if("P".equals(obj))
  581.  
  582.  
  583.             lblWordTyped.setText(lblWordTyped.getText() + "P");
  584.  
  585.  
  586.         if("Q".equals(obj))
  587.  
  588.  
  589.             lblWordTyped.setText(lblWordTyped.getText() + "Q");
  590.  
  591.  
  592.         if("R".equals(obj))
  593.  
  594.  
  595.             lblWordTyped.setText(lblWordTyped.getText() + "R");
  596.  
  597.  
  598.         if("S".equals(obj))
  599.  
  600.  
  601.             lblWordTyped.setText(lblWordTyped.getText() + "S");
  602.  
  603.  
  604.         if("T".equals(obj))
  605.  
  606.  
  607.             lblWordTyped.setText(lblWordTyped.getText() + "T");
  608.  
  609.  
  610.         if("U".equals(obj))
  611.  
  612.  
  613.             lblWordTyped.setText(lblWordTyped.getText() + "U");
  614.  
  615.  
  616.         if("V".equals(obj))
  617.  
  618.  
  619.             lblWordTyped.setText(lblWordTyped.getText() + "V");
  620.  
  621.  
  622.         if("W".equals(obj))
  623.  
  624.  
  625.             lblWordTyped.setText(lblWordTyped.getText() + "W");
  626.  
  627.  
  628.         if("X".equals(obj))
  629.  
  630.  
  631.             lblWordTyped.setText(lblWordTyped.getText() + "X");
  632.  
  633.  
  634.         if("Y".equals(obj))
  635.  
  636.  
  637.             lblWordTyped.setText(lblWordTyped.getText() + "Y");
  638.  
  639.  
  640.         if("Z".equals(obj))
  641.  
  642.  
  643.             lblWordTyped.setText(lblWordTyped.getText() + "Z");
  644.  
  645.  
  646.         if("Reset".equals(obj))
  647.  
  648.  
  649.             Reset();
  650.  
  651.  
  652.         if("Help".equals(obj))
  653.  
  654.  
  655.             lblWordTyped.setText(ThingsName[CurrentPicture]);
  656.  
  657.  
  658.         if("Done".equals(obj))
  659.  
  660.  
  661.         {
  662.  
  663.  
  664.             if(CheckWord() == false)
  665.  
  666.  
  667.                 TooBad();
  668.  
  669.  
  670.             else
  671.  
  672.  
  673.             {
  674.  
  675.  
  676.                 Celebrate();
  677.  
  678.  
  679.                 showStatus("Finding Next Picture.");
  680.  
  681.  
  682.                 CurrentPicture = Random();
  683.  
  684.  
  685.                 showStatus("Picture Found, Good Luck!");
  686.  
  687.  
  688.             }
  689.  
  690.  
  691.         }
  692.  
  693.  
  694.         WordUpdate = false; repaint();return true;
  695.  
  696.  
  697.     }
  698.  
  699.  
  700.     
  701.  
  702.  
  703.     boolean CheckWord()
  704.  
  705.  
  706.     {
  707.  
  708.  
  709.          if((int)ThingsName[CurrentPicture].compareTo(lblWordTyped.getText()) != 0)
  710.  
  711.  
  712.             return false;
  713.  
  714.  
  715.         else
  716.  
  717.  
  718.             return true;
  719.  
  720.  
  721.     }
  722.  
  723.  
  724.  
  725.  
  726.  
  727.     void Celebrate()
  728.  
  729.  
  730.     {
  731.  
  732.  
  733.         lblWordTyped.setText(msgWon);
  734.  
  735.  
  736.     }
  737.  
  738.  
  739.  
  740.  
  741.  
  742.     void TooBad()
  743.  
  744.  
  745.     {
  746.  
  747.  
  748.         cmdHelp.enable();
  749.  
  750.  
  751.         lblWordTyped.setText(msgLost);
  752.  
  753.  
  754.     }    
  755.  
  756.  
  757.  
  758.  
  759.  
  760.     void Reset()
  761.  
  762.  
  763.     {
  764.  
  765.  
  766.         lblWordTyped.setText("");
  767.  
  768.  
  769.     }
  770.  
  771.  
  772.     
  773.  
  774.  
  775.     int Random()
  776.  
  777.  
  778.     {
  779.  
  780.  
  781.         int ranint = -1;
  782.  
  783.  
  784.  
  785.  
  786.  
  787.         while(ranint < 0 || ranint >= TotalPictures || ranint == PreviousPicture)
  788.  
  789.  
  790.         {
  791.  
  792.  
  793.             ranint = (int)(java.lang.Math.random()*TotalPictures);
  794.  
  795.  
  796.             showStatus("Looking for random number");
  797.  
  798.  
  799.         }
  800.  
  801.  
  802.         PreviousPicture = ranint;
  803.  
  804.  
  805.         cmdHelp.disable();
  806.  
  807.  
  808.         return ranint;
  809.  
  810.  
  811.     }            
  812.  
  813.  
  814. }
  815.