home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / JumpingBox / JumpingBox.java < prev   
Encoding:
Java Source  |  2002-09-06  |  5.8 KB  |  177 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)JumpingBox.java    1.12 02/06/13
  38.  */
  39.  
  40. import java.awt.event.*;
  41. import java.awt.*;
  42.  
  43. public class JumpingBox extends java.applet.Applet 
  44.     implements MouseListener, MouseMotionListener, ComponentListener {
  45.  
  46.     private int mx, my;
  47.     private Dimension size;
  48.     private int onaroll;
  49.  
  50.     public void init() {
  51.         onaroll = 0;
  52.         setSize(500, 500);
  53.         size = getSize();
  54.         addMouseListener(this);
  55.         addMouseMotionListener(this);
  56.         addComponentListener(this);
  57.     }
  58.  
  59.     public void update(Graphics g) {
  60.         Dimension newSize = getSize();
  61.         if (size.equals(newSize)) {
  62.             // Erase old box
  63.             g.setColor(getBackground());
  64.             g.drawRect(mx, my, (size.width / 10) - 1, 
  65.                        (size.height / 10) - 1);
  66.         } else {
  67.             size = newSize;
  68.             g.clearRect(0, 0, size.width, size.height);
  69.         }
  70.         // Calculate new position
  71.         mx = (int) (Math.random() * 1000) % 
  72.             (size.width - (size.width / 10));
  73.         my = (int) (Math.random() * 1000) % 
  74.             (size.height - (size.height / 10));
  75.         paint(g);
  76.     }
  77.   
  78.     public void paint(Graphics g) {
  79.         g.setColor(Color.black);
  80.         g.drawRect(0, 0, size.width - 1, size.height - 1);
  81.         g.drawRect(mx, my, (size.width / 10) - 1, 
  82.                    (size.height / 10) - 1);
  83.     }
  84.  
  85.     /*
  86.      * Mouse methods
  87.      */
  88.     public void mouseDragged(MouseEvent e) {}
  89.  
  90.     public void mouseMoved(MouseEvent e) {
  91.         e.consume();
  92.         if ((e.getX() % 3 == 0) && (e.getY() % 3 == 0)) {
  93.             repaint();
  94.         }
  95.     }
  96.  
  97.     public void mousePressed(MouseEvent e) {
  98.         int x = e.getX();
  99.         int y = e.getY();
  100.         e.consume();
  101.         requestFocus();
  102.         if (mx < x && x < mx + getSize().width / 10 - 1 && 
  103.             my < y && y < my + getSize().height / 10 - 1) {  //determine if hit
  104.             if (onaroll > 0) {   //not first hit
  105.                 switch (onaroll%4) {   //play a sound
  106.                 case 0:
  107.                     play(getCodeBase(), 
  108.                          "sounds/tiptoe.thru.the.tulips.au");
  109.                     break;
  110.                 case 1:
  111.                     play(getCodeBase(), "sounds/danger,danger...!.au");
  112.                     break;
  113.                 case 2:
  114.                     play(getCodeBase(), "sounds/adapt-or-die.au");
  115.                     break;
  116.                 case 3:
  117.                     play(getCodeBase(), "sounds/cannot.be.completed.au");
  118.                     break;
  119.                 }
  120.                 onaroll++;
  121.                 if (onaroll > 5) {
  122.                     getAppletContext()
  123.                         .showStatus("You're on your way to THE HALL OF FAME:"
  124.                                     + onaroll + "Hits!");
  125.                 } else {
  126.                     getAppletContext().showStatus("YOU'RE ON A ROLL:" 
  127.                                                   + onaroll + "Hits!");
  128.                 }
  129.             } else {   //first hit
  130.                 getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  131.                 play(getCodeBase(), "sounds/that.hurts.au");
  132.                 onaroll = 1;
  133.             }
  134.         } else {   //miss
  135.             getAppletContext().showStatus("You hit nothing at (" + x + ", " 
  136.                                           + y + "), exactly");
  137.             play(getCodeBase(), "sounds/thin.bell.au");
  138.             onaroll = 0;
  139.         }
  140.         repaint();
  141.     }
  142.  
  143.     public void mouseReleased(MouseEvent e) {}
  144.  
  145.     public void mouseEntered(MouseEvent e) {
  146.         repaint();
  147.     }
  148.  
  149.     public void mouseExited(MouseEvent e) {
  150.         repaint();
  151.     }
  152.  
  153.     public void mouseClicked(MouseEvent e) {}
  154.  
  155.     public void componentHidden(ComponentEvent e) {}
  156.  
  157.     public void componentMoved(ComponentEvent e) {}
  158.  
  159.     public void componentResized(ComponentEvent e) {
  160.         repaint();
  161.     }
  162.  
  163.     public void componentShown(ComponentEvent e) {
  164.         repaint();
  165.     }
  166.   
  167.     public void destroy() {
  168.         removeMouseListener(this);
  169.         removeMouseMotionListener(this);
  170.     }
  171.  
  172.     public String getAppletInfo() {
  173.         return "Title: JumpingBox\n"
  174.             + "Author: Anonymous";
  175.     }
  176. }
  177.