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 / BRICK / BRICK.EXE / BricksApplet.java < prev    next >
Encoding:
Java Source  |  1996-04-23  |  4.3 KB  |  196 lines

  1. import java.awt.*;
  2. import java.util.Vector;
  3. import java.util.Date;
  4. import java.util.Random;
  5. import java.lang.Math;
  6. import java.applet.AudioClip;
  7.  
  8.  
  9. public class BricksApplet extends java.applet.Applet {
  10.   BricksGame game;
  11.   CommandPanel cp;
  12.   Panel p1;
  13.   private AudioClip efx[];
  14.  
  15.   public void init () {
  16.     System.out.println("Initializing.");
  17.  
  18.     GridBagLayout gb = new GridBagLayout();
  19.     GridBagConstraints gbc = new GridBagConstraints();
  20.  
  21.     this.setLayout(gb);
  22.  
  23.     game = new BricksGame(this);
  24.     gbc.fill = GridBagConstraints.BOTH;
  25.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  26.     gbc.weightx = gbc.weighty = 1.0;
  27.     gb.setConstraints(game, gbc);
  28.     this.add(game);
  29.  
  30.     cp = new CommandPanel(this);
  31.     gbc.fill = GridBagConstraints.HORIZONTAL;
  32.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  33.     gbc.weightx = gbc.weighty = 0.0;
  34.     gb.setConstraints(cp, gbc);
  35.     this.add(cp);
  36.  
  37.     efx = new AudioClip[BricksConstants.EFX_LENGTH];
  38.     efx[BricksConstants.EFX_IMPACT_WALL] = 
  39.       getAudioClip(getCodeBase(), "audio/impact-wall.au");
  40.     efx[BricksConstants.EFX_IMPACT_PADDLE] = 
  41.       getAudioClip(getCodeBase(), "audio/impact-paddle.au");
  42.     efx[BricksConstants.EFX_IMPACT_BRICK] = 
  43.       getAudioClip(getCodeBase(), "audio/impact-brick.au");
  44.     efx[BricksConstants.EFX_IMPACT_BRICK_SPECIAL] = 
  45.       getAudioClip(getCodeBase(), "audio/impact-brick-special.au");
  46.     efx[BricksConstants.EFX_LOST_BALL] = 
  47.       getAudioClip(getCodeBase(), "audio/lost-ball.au");
  48.     efx[BricksConstants.EFX_NEXT_LEVEL] = 
  49.       getAudioClip(getCodeBase(), "audio/next-level.au");
  50.  
  51.   }
  52.  
  53.   public final AudioClip[] getEfx() {
  54.     return efx;
  55.   }
  56.  
  57.   public void start () {
  58.     System.out.println(this + "Start.");
  59.  
  60.     game.start();
  61.     repaint();
  62.   }
  63.  
  64.   public void destroy () {
  65.     System.out.println(this + "Destroy.");
  66.     game.destroy();
  67.   }
  68.  
  69.   public void stop () {
  70.     System.out.println(this + "Stop.");
  71.     game.stop();
  72.   }
  73.  
  74.   public void pause () {
  75.     System.out.println(this + "Pause.");
  76.     game.pause();
  77.   }
  78.  
  79.   public void resume () {
  80.     System.out.println(this + "Resume.");
  81.     game.resume();
  82.   }
  83.  
  84.   public void reset () {
  85.     System.out.println(this + "Reset.");
  86.     game.reset();
  87.   }
  88.  
  89.   public Insets insets () {
  90.     return new Insets(10,10,10,10);
  91.   }
  92.  
  93.  
  94. }
  95.  
  96. class CommandPanel extends Panel {
  97.   private BricksApplet parent;
  98.   private Button reset, pause;
  99.   
  100.   public CommandPanel(BricksApplet parent) {
  101.  
  102.     System.out.println("CommandPanel:Create.");
  103.  
  104.     this.parent = parent;
  105.  
  106.     this.add(new Label("Score: "));
  107.     Label score = new Label("0000");
  108.  
  109.     this.add(score);
  110.  
  111.     reset = new Button("Reset");
  112.     pause = new Button("Pause");
  113.  
  114.     this.add(reset);
  115.     this.add(pause);
  116.     this.layout();
  117.  
  118.     ScoreKeeper s = new ScoreKeeper(parent.game, (Panel) this, score);
  119.     s.start();
  120.  
  121.   }
  122.  
  123.   public boolean action (Event evt, Object obj) {
  124.     System.out.println("CommandPanel: got event.");
  125.  
  126.     if (evt.target instanceof Button) {
  127.       return buttonAction(evt, obj);
  128.     } else {
  129.       return false;
  130.     }
  131.  
  132.   }
  133.  
  134.   public boolean buttonAction (Event evt, Object obj) {
  135.     System.out.println("CommandPanel: Handling button event.");
  136.  
  137.     String which;
  138.  
  139.     which = ((Button) evt.target).getLabel();
  140.     System.out.println("CommandPanel: Handling '" + which + "'.");
  141.  
  142.     if (which.equals("Reset")) {
  143.       pause.setLabel("Pause");
  144.       parent.reset();
  145.       return true;
  146.     } else if (which.equals("Pause")) {
  147.       pause.setLabel("Resume"); this.layout();
  148.       parent.pause();
  149.       return true;
  150.     } else if (which.equals("Resume")) {
  151.       pause.setLabel("Pause"); this.layout();
  152.       parent.resume();
  153.       return true;
  154.     } else
  155.       return false;
  156.  
  157.   }
  158.  
  159. }
  160.  
  161.  
  162. class ScoreKeeper extends Thread {
  163.  
  164.   BricksGame game;
  165.   Panel p;
  166.   Label score;
  167.  
  168.   ScoreKeeper (BricksGame game, Panel p, Label l) {
  169.     this.game = game;
  170.     this.p = p;
  171.     this.score = l;
  172.   }
  173.  
  174.   public void run () {
  175.     setPriority(2);
  176.  
  177.     while (true) {
  178.       if (game != null && game.ball != null) {
  179.         score.setText(Integer.toString(game.getBall().getScore()));
  180.       }
  181.  
  182.       try {
  183.         sleep(3600);
  184.       } catch (InterruptedException e) {
  185.         //
  186.       }
  187.  
  188.     }
  189.  
  190.   }
  191.  
  192. }
  193.  
  194.   
  195.  
  196.