home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / BLOCKS / BLOCKS.EXE / Blocks.java < prev    next >
Encoding:
Java Source  |  1996-03-14  |  24.9 KB  |  731 lines

  1. //****************************************************************************
  2.  
  3. // ---- general information ----
  4.  
  5. //
  6.  
  7. // BLOCKS.JAVA           v 1.01 13 jan. '96
  8.  
  9. // Written by :          I. van Rienen / E-mail ivr@bart.nl
  10.  
  11. // URL:                  http://www/bart.nl/~ivr
  12.  
  13. // Initial release:      17 dec. '95
  14.  
  15. // Released as freeware: 05 jan. '96 v 1.00
  16.  
  17. //
  18.  
  19. // ----- version information
  20.  
  21. // v 1.00   17 dec. '95 Initial Release
  22.  
  23. // v 1.01   13 jan. '96 Arrow-keys changed to fix sun problem.
  24.  
  25. //
  26.  
  27. // ---- Description ----
  28.  
  29. // JAVA program for playing BLOCKS on Internet
  30.  
  31. //
  32.  
  33. // This program is postcard-ware.
  34.  
  35. // If you like this program, send a postcard to:
  36.  
  37. //
  38.  
  39. //    Iwan van Rienen
  40.  
  41. //    J. Maetsuyckerstr. 145
  42.  
  43. //    2593 ZG  The Hague
  44.  
  45. //    The Netherlands
  46.  
  47. //
  48.  
  49. // Thanks in advance!
  50.  
  51. // you are free to do anything you want with this program.
  52.  
  53. // However, I am not responsible for any bugs in this program and
  54.  
  55. // possible damage to hard- or software when using this program.
  56.  
  57. // Please refer to this program when you are using it in one of your programs
  58.  
  59. // Feel free to e-mail me at any time at ivr@bart.nl
  60.  
  61. // Visit my homepage at http://www.bart.nl/~ivr for more software
  62.  
  63. //****************************************************************************
  64.  
  65.  
  66.  
  67. import java.awt.*;
  68.  
  69. import java.awt.image.*;
  70.  
  71. import java.applet.Applet;
  72.  
  73. import java.applet.AudioClip;
  74.  
  75. import java.util.Vector;
  76.  
  77. import java.lang.Math;
  78.  
  79. import java.lang.Thread;
  80.  
  81. import java.lang.System;
  82.  
  83.  
  84.  
  85. public class Blocks extends java.applet.Applet implements Runnable {
  86.  
  87.     public static final int cols = 10;
  88.  
  89.     public static final int rows = 18;
  90.  
  91.     public static final int ElementSize = 15;
  92.  
  93.     public static final int MaxElement = 3;
  94.  
  95.     public static final Color BackGroundColor = Color.black;
  96.  
  97.     public static final int SOUND_DROP = 0;
  98.  
  99.     public static final int SOUND_GAMEOVER = 1;
  100.  
  101.     public static final int SOUND_NEXTLEVEL = 2;
  102.  
  103.     public static final int SOUND_LINE = 3;
  104.  
  105.  
  106.  
  107.     PlayFieldCanvas myPlayFieldCanvas;
  108.  
  109.     StatisticsCanvas Statistics;
  110.  
  111.     Thread killme = null;
  112.  
  113.     Vector ShapeSet;
  114.  
  115.     Shape FallingShape = null;
  116.  
  117.     Shape NextShape = null;
  118.  
  119.     Color PlayField[][];
  120.  
  121.     static AudioClip sounds[];
  122.  
  123.     public boolean GamePaused = false;
  124.  
  125.  
  126.  
  127.     public void init() {
  128.  
  129.         setLayout(new BorderLayout());
  130.  
  131.         Panel grid = new Panel();
  132.  
  133.         grid.setLayout(new GridLayout(0, 2));
  134.  
  135.  
  136.  
  137.         add("Center", grid);
  138.  
  139.         myPlayFieldCanvas = new PlayFieldCanvas();
  140.  
  141.         grid.add(myPlayFieldCanvas);
  142.  
  143.         Statistics = new StatisticsCanvas();
  144.  
  145.         grid.add(Statistics);
  146.  
  147.  
  148.  
  149.         Panel p = new Panel();
  150.  
  151.         p.setLayout(new FlowLayout());
  152.  
  153.         add("East", p);
  154.  
  155.         p.add(new Button("About"));
  156.  
  157.         p.add(new Button("Pause / Resume"));
  158.  
  159.  
  160.  
  161.         ShapeSet = new Vector();
  162.  
  163.         ShapeSet.addElement(new Shape(  0x0000 ,
  164.  
  165.                                         0x0FF0 ,
  166.  
  167.                                         0x0FF0 ,
  168.  
  169.                                         0x0000 , Color.blue, 3));
  170.  
  171.  
  172.  
  173.         ShapeSet.addElement(new Shape(  0x0F00 ,
  174.  
  175.                                         0x0F00 ,
  176.  
  177.                                         0x0FF0 ,
  178.  
  179.                                         0x0000 , Color.yellow, 5));
  180.  
  181.  
  182.  
  183.         ShapeSet.addElement(new Shape(  0x00F0 ,
  184.  
  185.                                         0x00F0 ,
  186.  
  187.                                         0x0FF0 ,
  188.  
  189.                                         0x0000 , Color.pink, 5));
  190.  
  191.  
  192.  
  193.         ShapeSet.addElement(new Shape(  0x0000 ,
  194.  
  195.                                         0x0F00 ,
  196.  
  197.                                         0xFFF0 ,
  198.  
  199.                                         0x0000 , Color.green, 4));
  200.  
  201.  
  202.  
  203.         ShapeSet.addElement(new Shape(  0x0F00 ,
  204.  
  205.                                         0x0F00 ,
  206.  
  207.                                         0x0F00 ,
  208.  
  209.                                         0x0F00 , Color.red, 4));
  210.  
  211.  
  212.  
  213.         ShapeSet.addElement(new Shape(  0x0F00 ,
  214.  
  215.                                         0x0FF0 ,
  216.  
  217.                                         0x00F0 ,
  218.  
  219.                                         0x0000 , Color.magenta, 4));
  220.  
  221.  
  222.  
  223.         ShapeSet.addElement(new Shape(  0x00F0 ,
  224.  
  225.                                         0x0FF0 ,
  226.  
  227.                                         0x0F00 ,
  228.  
  229.                                         0x0000 , Color.orange, 4));
  230.  
  231.  
  232.  
  233.         sounds = new AudioClip[4];
  234.  
  235.         sounds[0] = getAudioClip(getCodeBase(), "audio/drop.au");
  236.  
  237.         sounds[1] = getAudioClip(getCodeBase(), "audio/gameover.au");
  238.  
  239.         sounds[2] = getAudioClip(getCodeBase(), "audio/nextlevel.au");
  240.  
  241.         sounds[3] = getAudioClip(getCodeBase(), "audio/line.au");
  242.  
  243.  
  244.  
  245.         PlayField = new Color[cols][rows];
  246.  
  247.         myPlayFieldCanvas.SetPlayField(PlayField);
  248.  
  249.         InitNewGame();
  250.  
  251.         GetNextRandomShape();
  252.  
  253.     }
  254.  
  255.  
  256.  
  257.     public static void play(int n) {
  258.  
  259.         if (sounds[n] != null) {
  260.  
  261.             sounds[n].play();
  262.  
  263.         }
  264.  
  265.     }
  266.  
  267.  
  268.  
  269.     public void InitNewGame() {
  270.  
  271.         ClearPlayField();
  272.  
  273.         Statistics.InitNewGame();
  274.  
  275.     }
  276.  
  277.  
  278.  
  279.     public void ClearPlayField() {
  280.  
  281.         int x, y;
  282.  
  283.  
  284.  
  285.         for (x = 0; x < cols ; x++) {
  286.  
  287.             for (y = 0 ; y < rows ; y++) {
  288.  
  289.                 PlayField[x][y] = Color.black;
  290.  
  291.             }
  292.  
  293.         }
  294.  
  295.     }
  296.  
  297.  
  298.  
  299.     public int GetRandomShapeNr() {
  300.  
  301.         int ShapeNr;
  302.  
  303.         do {
  304.  
  305.             ShapeNr = (int) (Math.random() * ShapeSet.size());
  306.  
  307.         } while (ShapeNr >= ShapeSet.size());
  308.  
  309.         return ShapeNr;
  310.  
  311.     }
  312.  
  313.  
  314.  
  315.     public void GetNextRandomShape() {
  316.  
  317.         if (FallingShape == null) {
  318.  
  319.                 FallingShape = (Shape) ShapeSet.elementAt(GetRandomShapeNr());
  320.  
  321.         } else {
  322.  
  323.                 FallingShape = NextShape;
  324.  
  325.         }
  326.  
  327.         FallingShape.Init();
  328.  
  329.         if (!FallingShape.CheckIfShapeFits(PlayField, 0, 0, false)) {
  330.  
  331.             GameOver();
  332.  
  333.         }
  334.  
  335.         NextShape = (Shape) ShapeSet.elementAt(GetRandomShapeNr());
  336.  
  337.         Statistics.DisplayNextShape(NextShape);
  338.  
  339.     }
  340.  
  341.  
  342.  
  343.     public void GameOver() {
  344.  
  345.         play (SOUND_GAMEOVER);
  346.  
  347.         myPlayFieldCanvas.GameOver();
  348.  
  349.         Statistics.GameOver();
  350.  
  351.         InitNewGame();
  352.  
  353.     }
  354.  
  355.  
  356.  
  357.     public void start() {
  358.  
  359.         if(killme == null) {
  360.  
  361.             killme = new Thread(this);
  362.  
  363.             killme.start();
  364.  
  365.         }
  366.  
  367.     }
  368.  
  369.  
  370.  
  371.     public void stop() {
  372.  
  373.         killme = null;
  374.  
  375.     }
  376.  
  377.  
  378.  
  379.     public void run() {
  380.  
  381.         while (killme != null) {
  382.  
  383.             try {
  384.  
  385.                 Thread.sleep(Statistics.GetGameSpeed());
  386.  
  387.             } catch (InterruptedException e){}
  388.  
  389.             if (!GamePaused) {
  390.  
  391.                 if (FallingShape != null) {
  392.  
  393.                     if (FallingShape.CheckIfShapeFits(PlayField, 0, 1, false)) {
  394.  
  395.                         ChangeShapePosition(0, 1, false);
  396.  
  397.                     } else {
  398.  
  399.                         play (SOUND_DROP);
  400.  
  401.                         FallingShape.PlaceInPlayField(PlayField);
  402.  
  403.                         myPlayFieldCanvas.RepaintPlayField();
  404.  
  405.                         Statistics.AddScore(FallingShape.GetValue());
  406.  
  407.                         CheckForFullLines();
  408.  
  409.                         GetNextRandomShape();
  410.  
  411.                     }
  412.  
  413.                 }
  414.  
  415.                 myPlayFieldCanvas.repaint(FallingShape);
  416.  
  417.             }
  418.  
  419.         }
  420.  
  421.         killme = null;
  422.  
  423.     }
  424.  
  425.  
  426.  
  427.     public void CheckForFullLines() {
  428.  
  429.         int Lines = 0;
  430.  
  431.         int x, y, yc;
  432.  
  433.         boolean Gap;
  434.  
  435.         for (y = 0; y < rows; y++) {
  436.  
  437.             Gap = false;
  438.  
  439.             for (x = 0; x < cols; x++) {
  440.  
  441.                 if (PlayField[x][y] == Color.black) Gap = true;
  442.  
  443.             }
  444.  
  445.             if (!Gap) {
  446.  
  447.                 Lines++;
  448.  
  449.                 for (yc = y - 1; yc >= 0; yc--) {
  450.  
  451.                     for (x = 0; x < cols; x++) {
  452.  
  453.                         PlayField[x][yc + 1] = PlayField[x][yc];
  454.  
  455.                     }
  456.  
  457.                 }
  458.  
  459.                 for (x = 0; x < cols; x++) {    // Delete top row.
  460.  
  461.                     PlayField[x][0] = Color.black;
  462.  
  463.                 }
  464.  
  465.             }
  466.  
  467.         }
  468.  
  469.         if (Lines > 0) {
  470.  
  471.             play (SOUND_LINE);
  472.  
  473.             myPlayFieldCanvas.RepaintPlayField();
  474.  
  475.         }
  476.  
  477.         Statistics.AddLines(Lines);
  478.  
  479.     }
  480.  
  481.  
  482.  
  483.     public boolean action(Event evt, Object arg) {
  484.  
  485.         if ("About".equals(arg)) {
  486.  
  487.             GamePaused = true;
  488.  
  489.             myPlayFieldCanvas.About();
  490.  
  491.         }
  492.  
  493.         if ("Pause / Resume".equals(arg)) {
  494.  
  495.             if (GamePaused) {
  496.  
  497.                 myPlayFieldCanvas.ShowAboutBox = false;
  498.  
  499.                 myPlayFieldCanvas.RepaintPlayField();
  500.  
  501.             }
  502.  
  503.  
  504.  
  505.             GamePaused = !GamePaused;
  506.  
  507.         }
  508.  
  509.         return true;
  510.  
  511.     }
  512.  
  513.  
  514.  
  515.     public synchronized boolean handleEvent(Event e) {
  516.  
  517.         int xChange = 0;
  518.  
  519.         int yChange = 0;
  520.  
  521.         boolean Rotate = false;
  522.  
  523.  
  524.  
  525.         switch (e.id) {
  526.  
  527.         case Event.ACTION_EVENT:
  528.  
  529.             return action(e, e.arg);
  530.  
  531.         case Event.KEY_ACTION:
  532.  
  533.         case Event.KEY_PRESS:
  534.  
  535.             switch (e.key) {
  536.  
  537.             case 'b':
  538.  
  539.             case 'B':           // Move block to left
  540.  
  541.                 xChange--;
  542.  
  543.                 break;
  544.  
  545.             case 'm':
  546.  
  547.             case 'M':           // Move block to right
  548.  
  549.                 xChange++;
  550.  
  551.                 break;
  552.  
  553.             case ' ':           // Move block down
  554.  
  555.                 yChange++;
  556.  
  557.                 break;
  558.  
  559.             case 'n':
  560.  
  561.             case 'N':           // Rotate block
  562.  
  563.                 Rotate = true;
  564.  
  565.                 break;
  566.  
  567.             default:
  568.  
  569.                 return false;
  570.  
  571.             }
  572.  
  573.             break;
  574.  
  575.         default:
  576.  
  577.             return false;
  578.  
  579.         }
  580.  
  581.         ChangeShapePosition (xChange, yChange, Rotate);
  582.  
  583.         return true;
  584.  
  585.     }
  586.  
  587.  
  588.  
  589.     public void ChangeShapePosition(int xChange, int yChange, boolean Rotate) {
  590.  
  591.         while (!FallingShape.IsReady()) ;
  592.  
  593.         if (FallingShape.CheckIfShapeFits(PlayField, xChange, yChange, Rotate)) {
  594.  
  595.             FallingShape.ChangePosition(xChange, yChange, Rotate);
  596.  
  597.             myPlayFieldCanvas.repaint(FallingShape);
  598.  
  599.         }
  600.  
  601.     }
  602.  
  603. }
  604.  
  605.  
  606.  
  607. class StatisticsCanvas extends Canvas implements ImageObserver {
  608.  
  609.     public static final Color textColor = Color.black;
  610.  
  611.     public static final int MaxLevel = 9;
  612.  
  613.     public static final int myFontHeight = 16;
  614.  
  615.  
  616.  
  617.     protected Font BlocksFont;
  618.  
  619.     protected FontMetrics BlocksFontMetrics;
  620.  
  621.     protected int Level, Lines, Score;
  622.  
  623.     protected Shape NextShape = null;
  624.  
  625.  
  626.  
  627.     public void InitNewGame() {
  628.  
  629.         Level = Lines = Score = 0;
  630.  
  631.     }
  632.  
  633.  
  634.  
  635.     public void GameOver() {
  636.  
  637.  
  638.  
  639.     }
  640.  
  641.  
  642.  
  643.     public int GetGameSpeed() {
  644.  
  645.         switch (Level) {
  646.  
  647.             case 0:     return 700;
  648.  
  649.             case 1:     return 600;
  650.  
  651.             case 2:     return 500;
  652.  
  653.             case 3:     return 400;
  654.  
  655.             case 4:     return 350;
  656.  
  657.             case 5:     return 300;
  658.  
  659.             case 6:     return 250;
  660.  
  661.             case 7:     return 200;
  662.  
  663.             case 8:     return 150;
  664.  
  665.             case 9:     return 100;
  666.  
  667.             default:    return 100;
  668.  
  669.         }
  670.  
  671.     }
  672.  
  673.  
  674.  
  675.     public StatisticsCanvas() {
  676.  
  677.         reshape(0, 0, 100, 100);
  678.  
  679.         BlocksFont = new Font("TimesRoman",Font.PLAIN,20);
  680.  
  681.         setFont(BlocksFont);
  682.  
  683.         BlocksFontMetrics = getFontMetrics(BlocksFont);
  684.  
  685.         InitNewGame();
  686.  
  687.     }
  688.  
  689.  
  690.  
  691.     public void AddScore(int s) {
  692.  
  693.         Score += s;
  694.  
  695.         repaint();
  696.  
  697.     }
  698.  
  699.  
  700.  
  701.     public void AddLines(int ln) {
  702.  
  703.         switch (ln) {
  704.  
  705.             case 1:
  706.  
  707.                 AddScore (10);
  708.  
  709.                 break;
  710.  
  711.             case 2:
  712.  
  713.                 AddScore (20);
  714.  
  715.                 break;
  716.  
  717.             case 3:
  718.  
  719.                 AddScore (30);
  720.  
  721.                 break;
  722.  
  723.             case 4:
  724.  
  725.                 AddScore (40);
  726.  
  727.                 break;
  728.  
  729.         }
  730.  
  731.         Lines += ln;
  732.  
  733.         if (Lines > (10 * (Level + 1))) AddLevel();
  734.  
  735.         repaint();
  736.  
  737.     }
  738.  
  739.  
  740.  
  741.     public void AddLevel() {
  742.  
  743.         Blocks.play (Blocks.SOUND_NEXTLEVEL);
  744.  
  745.         if (Level < MaxLevel) Level++;
  746.  
  747.         repaint();
  748.  
  749.     }
  750.  
  751.  
  752.  
  753.     public void DisplayNextShape(Shape s) {
  754.  
  755.         NextShape =  s;
  756.  
  757.         repaint();
  758.  
  759.     }
  760.  
  761.  
  762.  
  763.     public void paint(Graphics g) {
  764.  
  765.         g.setColor(textColor);
  766.  
  767.         g.drawString("Level: " + Level, 0, myFontHeight);
  768.  
  769.         g.drawString("Lines: " + Lines, 0, myFontHeight * 3);
  770.  
  771.         g.drawString("Score: " + Score, 0, myFontHeight * 5);
  772.  
  773.         g.drawString("Next:", 0, myFontHeight * 7);
  774.  
  775.         if (NextShape != null) {
  776.  
  777.             NextShape.DisplayAbs(g, 10, myFontHeight * 7 + 10);
  778.  
  779.         }
  780.  
  781.     }
  782.  
  783. }
  784.  
  785.  
  786.  
  787. class PlayFieldCanvas extends Canvas implements ImageObserver {
  788.  
  789.     public static final int fh1 = 17, fh2 = 17;
  790.  
  791.     public static final int BorderWidth = 5;
  792.  
  793.     public static final Color BorderColor = Color.blue;
  794.  
  795.     protected Shape FallingShape = null;
  796.  
  797.     boolean FallingShapeNeedRepaint = false;
  798.  
  799.     boolean PlayFieldNeedRepaint = false;
  800.  
  801.     boolean DiscardGame = false;
  802.  
  803.  
  804.  
  805.     boolean ShowAboutBox = false;
  806.  
  807.     protected Font BlocksFont1, BlocksFont2;
  808.  
  809.  
  810.  
  811.  
  812.  
  813.     Color PlayField[][] = null;
  814.  
  815.  
  816.  
  817.     public void About() {
  818.  
  819.         ShowAboutBox = true;
  820.  
  821.         repaint();
  822.  
  823.     }
  824.  
  825.  
  826.  
  827.     public void GameOver() {
  828.  
  829.         DiscardGame = true;
  830.  
  831.         repaint();
  832.  
  833.     }
  834.  
  835.  
  836.  
  837.     public void RepaintPlayField() {
  838.  
  839.         PlayFieldNeedRepaint = true;
  840.  
  841.         repaint();
  842.  
  843.     }
  844.  
  845.  
  846.  
  847.     public void SetPlayField(Color pv[][]) {
  848.  
  849.             PlayField = pv;
  850.  
  851.     }
  852.  
  853.  
  854.  
  855.     public PlayFieldCanvas() {
  856.  
  857.         BlocksFont1 = new Font("TimesRoman",Font.BOLD,20);
  858.  
  859.         FontMetrics BlocksFontMetrics1 = getFontMetrics(BlocksFont1);
  860.  
  861.         BlocksFont2 = new Font("TimesRoman",Font.PLAIN,14);
  862.  
  863.         FontMetrics BlocksFontMetrics2 = getFontMetrics(BlocksFont2);
  864.  
  865.         reshape(0, 0, Blocks.ElementSize * Blocks.cols + BorderWidth * 2, Blocks.ElementSize * Blocks.rows + BorderWidth );
  866.  
  867.     }
  868.  
  869.  
  870.  
  871.     public void repaint(Shape Shp) {
  872.  
  873.        FallingShape = Shp;
  874.  
  875.         FallingShapeNeedRepaint = true;
  876.  
  877.         repaint();
  878.  
  879.     }
  880.  
  881.  
  882.  
  883.     public void DrawLines (Graphics g, int y1, int y2) {
  884.  
  885.         for (int y = y1 * Blocks.ElementSize; y < y2 * Blocks.ElementSize; y++) {
  886.  
  887.             g.drawLine (BorderWidth, y, BorderWidth + Blocks.cols * Blocks.ElementSize, y);
  888.  
  889.         }
  890.  
  891.     }
  892.  
  893.  
  894.  
  895.     public void GraphicsEffect(Graphics g, int y1, int y2) {
  896.  
  897.         for (int l = 0; l < 10; l++) {
  898.  
  899.             g.setColor(Color.red);
  900.  
  901.             DrawLines (g, y1, y2);
  902.  
  903.             g.setColor(Color.green);
  904.  
  905.             DrawLines (g, y1, y2);
  906.  
  907.             g.setColor(Color.blue);
  908.  
  909.             DrawLines (g, y1, y2);
  910.  
  911.             g.setColor(Color.black);
  912.  
  913.             DrawLines (g, y1, y2);
  914.  
  915.         }
  916.  
  917.     }
  918.  
  919.  
  920.  
  921.     public void DiscardIt(Graphics g) {
  922.  
  923.         DiscardGame = false;
  924.  
  925.         GraphicsEffect (g, 0, Blocks.rows);
  926.  
  927.     }
  928.  
  929.  
  930.  
  931.     public void DisplayAboutBox(Graphics g)
  932.  
  933.     {
  934.  
  935.         int y = 1;
  936.  
  937.         // Clear background
  938.  
  939.         g.setColor(Color.black);
  940.  
  941.         g.fillRect (BorderWidth, 0, Blocks.ElementSize * Blocks.cols, Blocks.ElementSize * Blocks.rows);
  942.  
  943.         g.setFont(BlocksFont1);
  944.  
  945.         g.setColor(Color.red);
  946.  
  947.         g.drawString("BLOCKS", 20, fh1);
  948.  
  949.         g.setFont(BlocksFont2);
  950.  
  951.         g.setColor(Color.cyan);
  952.  
  953.         g.drawString("v 1.01", BorderWidth + 110, fh1);
  954.  
  955.         g.drawString("Copyright (c) 1995, 1996", BorderWidth + 2, fh1 + fh2 * y++);
  956.  
  957.         g.drawString("Iwan van Rienen", BorderWidth + 2, fh1 + fh2 * y++);
  958.  
  959.         g.drawString("This program is postcard", BorderWidth + 2, fh1 + fh2 * y++);
  960.  
  961.         g.drawString("ware. If you like this", BorderWidth + 2, fh1 + fh2 * y++);
  962.  
  963.         g.drawString("program buy a stamp and", BorderWidth + 2, fh1 + fh2 * y++);
  964.  
  965.         g.drawString("a postcard and send it to: ", BorderWidth + 2, fh1 + fh2 * y++);        g.setColor(Color.green);
  966.  
  967.         g.setColor(Color.yellow);
  968.  
  969.         g.drawString("Iwan van Rienen", BorderWidth + 2, fh1 + fh2 * y++);
  970.  
  971.         g.drawString("J. Maetsuyckerstr. 145", BorderWidth + 2, fh1 + fh2 * y++);
  972.  
  973.         g.drawString("2593 ZG  The Hague", BorderWidth + 2, fh1 + fh2 * y++);
  974.  
  975.         g.drawString("The Netherlands", BorderWidth + 2, fh1 + fh2 * y++);
  976.  
  977.         g.setColor(Color.cyan);
  978.  
  979.         g.drawString("The JAVA source is free", BorderWidth + 2, fh1 + fh2 * y++);
  980.  
  981.         g.drawString("Visit my homepage at", BorderWidth + 2, fh1 + fh2 * y++);
  982.  
  983.         g.setColor(Color.green);
  984.  
  985.         g.drawString("http://www.bart.nl/~ivr", BorderWidth + 2, fh1 + fh2 * y++);
  986.  
  987.         g.drawString("E-mail to ivr@bart.nl", BorderWidth + 2, fh1 + fh2 * y++);
  988.  
  989.     }
  990.  
  991.  
  992.  
  993.     public void update(Graphics g) {
  994.  
  995.         if (DiscardGame) {
  996.  
  997.             DiscardIt(g);
  998.  
  999.         }
  1000.  
  1001.         if (PlayFieldNeedRepaint) {
  1002.  
  1003.             DrawPlayField(g);
  1004.  
  1005.         }
  1006.  
  1007.         DrawFallingShape(g);
  1008.  
  1009.         if (ShowAboutBox) {
  1010.  
  1011.             DisplayAboutBox(g);
  1012.  
  1013.         }
  1014.  
  1015.     }
  1016.  
  1017.  
  1018.  
  1019.     public void paint(Graphics g) {
  1020.  
  1021.         if (ShowAboutBox) {
  1022.  
  1023.             DisplayAboutBox(g);
  1024.  
  1025.         } else {
  1026.  
  1027.             DrawPlayField(g);
  1028.  
  1029.             FallingShapeNeedRepaint = true;
  1030.  
  1031.             DrawFallingShape(g);
  1032.  
  1033.         }
  1034.  
  1035.     }
  1036.  
  1037.  
  1038.  
  1039.     public void DrawFallingShape(Graphics g) {
  1040.  
  1041.         if (FallingShapeNeedRepaint && FallingShape != null) {
  1042.  
  1043.             FallingShape.hide(g, BorderWidth);
  1044.  
  1045.             while (!FallingShape.IsReady()) ;
  1046.  
  1047.             FallingShape.Display(g, BorderWidth);
  1048.  
  1049.             FallingShapeNeedRepaint = false;
  1050.  
  1051.         }
  1052.  
  1053.     }
  1054.  
  1055.  
  1056.  
  1057.     public void DrawPlayField(Graphics g) {
  1058.  
  1059.         int x, y;
  1060.  
  1061.         g.setColor(BorderColor);
  1062.  
  1063.         // Draw left border
  1064.  
  1065.         g.fillRect (0, 0, BorderWidth, Blocks.ElementSize * Blocks.rows);
  1066.  
  1067.         // Draw right border
  1068.  
  1069.         g.fillRect (Blocks.ElementSize * Blocks.cols + BorderWidth, 0, BorderWidth, Blocks.ElementSize * Blocks.rows);
  1070.  
  1071.         // Draw bottom border
  1072.  
  1073.         g.fillRect (0, Blocks.ElementSize * Blocks.rows, Blocks.ElementSize * Blocks.cols + BorderWidth * 2, BorderWidth);
  1074.  
  1075.         for (x = 0; x < Blocks.cols; x++) {
  1076.  
  1077.             for (y = 0; y < Blocks.rows; y++) {
  1078.  
  1079.                 if (PlayField[x][y] != Color.black) {
  1080.  
  1081.                     g.setColor(PlayField[x][y]);
  1082.  
  1083.                     g.fillRect(BorderWidth + x * Blocks.ElementSize + 1, y * Blocks.ElementSize + 1, Blocks.ElementSize - 2, Blocks.ElementSize - 2);
  1084.  
  1085.                     g.setColor(Color.white);
  1086.  
  1087.                     g.drawRect(BorderWidth + x * Blocks.ElementSize, y * Blocks.ElementSize, Blocks.ElementSize - 1, Blocks.ElementSize - 1);
  1088.  
  1089.                 } else {
  1090.  
  1091.                     g.setColor(Color.black);
  1092.  
  1093.                     g.fillRect(BorderWidth + x * Blocks.ElementSize, y * Blocks.ElementSize, Blocks.ElementSize, Blocks.ElementSize);
  1094.  
  1095.                 }
  1096.  
  1097.             }
  1098.  
  1099.         }
  1100.  
  1101.     PlayFieldNeedRepaint = false;
  1102.  
  1103.     }
  1104.  
  1105. }
  1106.  
  1107.  
  1108.  
  1109. class Element {
  1110.  
  1111.     protected int x, y;
  1112.  
  1113.     int oldX, oldY;
  1114.  
  1115.     protected int xInShape, yInShape;
  1116.  
  1117.     protected int OriginalX, OriginalY;
  1118.  
  1119.     protected int OriginalXInShape, OriginalYInShape;
  1120.  
  1121.     protected Color clr;
  1122.  
  1123.     protected boolean ErasePossible;
  1124.  
  1125.  
  1126.  
  1127.     public Element(int xPos, int yPos, Color c) {
  1128.  
  1129.         ErasePossible = false;
  1130.  
  1131.         xInShape = OriginalXInShape = xPos;
  1132.  
  1133.         yInShape = OriginalYInShape = yPos;
  1134.  
  1135.         x = OriginalX = xPos + Blocks.cols / 2 - (Blocks.MaxElement + 1)/ 2;
  1136.  
  1137.         y = OriginalY = yPos;
  1138.  
  1139.         clr = c;
  1140.  
  1141.     }
  1142.  
  1143.  
  1144.  
  1145.     public void Init() {
  1146.  
  1147.         ErasePossible = false;
  1148.  
  1149.         x = OriginalX; y = OriginalY;
  1150.  
  1151.         xInShape = OriginalXInShape;
  1152.  
  1153.         yInShape = OriginalYInShape;
  1154.  
  1155.     }
  1156.  
  1157.  
  1158.  
  1159.     public void hide (Graphics g, int xOffs, int yOffs) {
  1160.  
  1161.         if (ErasePossible) {
  1162.  
  1163.             int Size = Blocks.ElementSize;
  1164.  
  1165.             g.setColor(Color.black);
  1166.  
  1167.             g.fillRect(xOffs + oldX * Size, yOffs + oldY * Size, Size, Size);
  1168.  
  1169.             ErasePossible = false;
  1170.  
  1171.         }
  1172.  
  1173.     }
  1174.  
  1175.  
  1176.  
  1177.     public void Display (Graphics g, int xOffs, int yOffs) {
  1178.  
  1179.         int Size = Blocks.ElementSize;
  1180.  
  1181.         g.setColor(clr);
  1182.  
  1183.         g.fillRect(xOffs + x * Size + 1, yOffs + y * Size + 1, Size - 2, Size - 2);
  1184.  
  1185.         g.setColor(Color.white);
  1186.  
  1187.         g.drawRect(xOffs + x * Size, yOffs + y * Size, Size - 1, Size - 1);
  1188.  
  1189.         oldX = x;
  1190.  
  1191.         oldY = y;
  1192.  
  1193.         ErasePossible = true;
  1194.  
  1195.     }
  1196.  
  1197.  
  1198.  
  1199.      public void DisplayAbs (Graphics g, int xOffs, int yOffs) {
  1200.  
  1201.         int Size = Blocks.ElementSize;
  1202.  
  1203.         g.setColor(clr);
  1204.  
  1205.         g.fillRect(xOffs + OriginalXInShape * Size + 1, yOffs + OriginalYInShape * Size + 1, Size - 2, Size - 2);
  1206.  
  1207.         g.setColor(Color.white);
  1208.  
  1209.         g.drawRect(xOffs + OriginalXInShape * Size, yOffs + OriginalYInShape * Size, Size - 1, Size - 1);
  1210.  
  1211.     }
  1212.  
  1213.  
  1214.  
  1215.     public boolean CheckIfElementFits(Color PlayField[][],
  1216.  
  1217.                                       int xOffs, int yOffs, boolean Rotate) {
  1218.  
  1219.        if (Rotate) {
  1220.  
  1221.             xOffs += Blocks.MaxElement - yInShape - xInShape;
  1222.  
  1223.             yOffs += xInShape - yInShape;
  1224.  
  1225.         }
  1226.  
  1227.         if (x + xOffs < 0 || x + xOffs >= Blocks.cols) return false;
  1228.  
  1229.         if (y + yOffs >= Blocks.rows ) return false;
  1230.  
  1231.         if (PlayField[x + xOffs][y + yOffs] != Color.black) return false;
  1232.  
  1233.         return true;
  1234.  
  1235.     }
  1236.  
  1237.  
  1238.  
  1239.     public void ChangeElementPosition(int xOffs, int yOffs, boolean Rotate) {
  1240.  
  1241.         if (Rotate) {
  1242.  
  1243.             xOffs += Blocks.MaxElement - yInShape - xInShape;
  1244.  
  1245.             yOffs += xInShape - yInShape;
  1246.  
  1247.             int tempxInShape = xInShape;
  1248.  
  1249.             xInShape = Blocks.MaxElement - yInShape;
  1250.  
  1251.             yInShape = tempxInShape;
  1252.  
  1253.         }
  1254.  
  1255.         x += xOffs;
  1256.  
  1257.         y += yOffs;
  1258.  
  1259.     }
  1260.  
  1261.  
  1262.  
  1263.     public int GetXPos() { return x; }
  1264.  
  1265.     public int GetYPos() { return y; }
  1266.  
  1267.     public Color GetColor() { return clr; }
  1268.  
  1269.  
  1270.  
  1271. }
  1272.  
  1273.  
  1274.  
  1275. class Shape {
  1276.  
  1277.     protected Vector Elements;
  1278.  
  1279.     protected int Value;
  1280.  
  1281.     protected boolean DrawReady = true;
  1282.  
  1283.  
  1284.  
  1285.     public Shape() {
  1286.  
  1287.         DrawReady = true;
  1288.  
  1289.     }
  1290.  
  1291.  
  1292.  
  1293.     public void Init() {
  1294.  
  1295.         DrawReady = true;
  1296.  
  1297.         Element WalkElement;
  1298.  
  1299.         for (int ix = 0; ix < Elements.size(); ix++) {
  1300.  
  1301.             WalkElement = (Element) Elements.elementAt(ix);
  1302.  
  1303.             WalkElement.Init();
  1304.  
  1305.         }
  1306.  
  1307.    }
  1308.  
  1309.  
  1310.  
  1311.     public Shape(int a, int b, int c, int d, Color clr, int v) {
  1312.  
  1313.         Value = v;
  1314.  
  1315.         Elements = new Vector();
  1316.  
  1317.         AddElements(0, a, clr);
  1318.  
  1319.         AddElements(1, b, clr);
  1320.  
  1321.         AddElements(2, c, clr);
  1322.  
  1323.         AddElements(3, d, clr);
  1324.  
  1325.         Init();
  1326.  
  1327.     }
  1328.  
  1329.  
  1330.  
  1331.     protected void AddElements (int row, int a, Color clr) {
  1332.  
  1333.         if ((a & 0xf000) > 0) Elements.addElement (new Element(0, row, clr));
  1334.  
  1335.         if ((a & 0x0f00) > 0) Elements.addElement (new Element(1, row, clr));
  1336.  
  1337.         if ((a & 0x00f0) > 0) Elements.addElement (new Element(2, row, clr));
  1338.  
  1339.         if ((a & 0x000f) > 0) Elements.addElement (new Element(3, row, clr));
  1340.  
  1341.     }
  1342.  
  1343.  
  1344.  
  1345.     public void hide (Graphics g, int xOffs) {
  1346.  
  1347.         Element WalkElement;
  1348.  
  1349.         for (int ix = 0; ix < Elements.size(); ix++) {
  1350.  
  1351.             WalkElement = (Element) Elements.elementAt(ix);
  1352.  
  1353.             WalkElement.hide(g, xOffs, 0);
  1354.  
  1355.         }
  1356.  
  1357.     }
  1358.  
  1359.  
  1360.  
  1361.     public void Display (Graphics g, int xOffs) {
  1362.  
  1363.         Element WalkElement;
  1364.  
  1365.         DrawReady = false;
  1366.  
  1367.         for (int ix = 0; ix < Elements.size(); ix++) {
  1368.  
  1369.             WalkElement = (Element) Elements.elementAt(ix);
  1370.  
  1371.             WalkElement.Display(g, xOffs, 0);
  1372.  
  1373.         }
  1374.  
  1375.         DrawReady = true;
  1376.  
  1377.     }
  1378.  
  1379.  
  1380.  
  1381.    public void DisplayAbs (Graphics g, int xAbs, int yAbs) {
  1382.  
  1383.         Element WalkElement;
  1384.  
  1385.         for (int ix = 0; ix < Elements.size(); ix++) {
  1386.  
  1387.             WalkElement = (Element) Elements.elementAt(ix);
  1388.  
  1389.             WalkElement.DisplayAbs(g, xAbs, yAbs);
  1390.  
  1391.         }
  1392.  
  1393.     }
  1394.  
  1395.  
  1396.  
  1397.     public boolean CheckIfShapeFits(Color PlayField[][], int xOffs, int yOffs,
  1398.  
  1399.                                     boolean Rotate) {
  1400.  
  1401.         Element WalkElement;
  1402.  
  1403.         for (int ix = 0; ix < Elements.size(); ix++) {
  1404.  
  1405.             WalkElement= (Element) Elements.elementAt(ix);
  1406.  
  1407.             if (!WalkElement.CheckIfElementFits
  1408.  
  1409.                    (PlayField, xOffs, yOffs, Rotate)) return false;
  1410.  
  1411.         }
  1412.  
  1413.         return true;
  1414.  
  1415.     }
  1416.  
  1417.  
  1418.  
  1419.     public void ChangePosition(int xOffs, int yOffs, boolean Rotate) {
  1420.  
  1421.         Element WalkElement;
  1422.  
  1423.         for (int ix = 0; ix < Elements.size(); ix++) {
  1424.  
  1425.             WalkElement = (Element) Elements.elementAt(ix);
  1426.  
  1427.             WalkElement.ChangeElementPosition(xOffs, yOffs, Rotate);
  1428.  
  1429.         }
  1430.  
  1431.     }
  1432.  
  1433.  
  1434.  
  1435.     public void PlaceInPlayField(Color PlayField[][]) {
  1436.  
  1437.         Element WalkElement;
  1438.  
  1439.         for (int ix = 0; ix < Elements.size(); ix++) {
  1440.  
  1441.             WalkElement= (Element) Elements.elementAt(ix);
  1442.  
  1443.             PlayField[WalkElement.GetXPos()]
  1444.  
  1445.                      [WalkElement.GetYPos()] = WalkElement.GetColor();
  1446.  
  1447.         }
  1448.  
  1449.     }
  1450.  
  1451.  
  1452.  
  1453.     public int GetValue() { return Value; }
  1454.  
  1455.     public boolean IsReady() { return DrawReady; }
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461. }