KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JumpingBox


1 /*
2  * @(#)JumpingBox.java 1.18 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)JumpingBox.java 1.18 06/02/22
39  */

40
41 import java.awt.event.*;
42 import java.awt.*;
43
44 public class JumpingBox extends java.applet.Applet JavaDoc
45     implements MouseListener, MouseMotionListener, ComponentListener {
46
47     private int mx, my;
48     private Dimension size;
49     private int onaroll;
50
51     public void init() {
52         onaroll = 0;
53         setSize(500, 500);
54         size = getSize();
55         addMouseListener(this);
56         addMouseMotionListener(this);
57         addComponentListener(this);
58     }
59
60     public void update(Graphics g) {
61         Dimension newSize = getSize();
62         if (size.equals(newSize)) {
63             // Erase old box
64
g.setColor(getBackground());
65             g.drawRect(mx, my, (size.width / 10) - 1,
66                        (size.height / 10) - 1);
67         } else {
68             size = newSize;
69             g.clearRect(0, 0, size.width, size.height);
70         }
71         // Calculate new position
72
mx = (int) (Math.random() * 1000) %
73             (size.width - (size.width / 10));
74         my = (int) (Math.random() * 1000) %
75             (size.height - (size.height / 10));
76         paint(g);
77     }
78   
79     public void paint(Graphics g) {
80         g.setColor(Color.black);
81         g.drawRect(0, 0, size.width - 1, size.height - 1);
82         g.drawRect(mx, my, (size.width / 10) - 1,
83                    (size.height / 10) - 1);
84     }
85
86     /*
87      * Mouse methods
88      */

89     public void mouseDragged(MouseEvent e) {}
90
91     public void mouseMoved(MouseEvent e) {
92         e.consume();
93         if ((e.getX() % 3 == 0) && (e.getY() % 3 == 0)) {
94             repaint();
95         }
96     }
97
98     public void mousePressed(MouseEvent e) {
99         int x = e.getX();
100         int y = e.getY();
101         e.consume();
102         requestFocus();
103         if (mx < x && x < mx + getSize().width / 10 - 1 &&
104             my < y && y < my + getSize().height / 10 - 1) { //determine if hit
105
if (onaroll > 0) { //not first hit
106
switch (onaroll%4) { //play a sound
107
case 0:
108                     play(getCodeBase(),
109                          "sounds/tiptoe.thru.the.tulips.au");
110                     break;
111                 case 1:
112                     play(getCodeBase(), "sounds/danger.au");
113                     break;
114                 case 2:
115                     play(getCodeBase(), "sounds/adapt-or-die.au");
116                     break;
117                 case 3:
118                     play(getCodeBase(), "sounds/cannot.be.completed.au");
119                     break;
120                 }
121                 onaroll++;
122                 if (onaroll > 5) {
123                     getAppletContext()
124                         .showStatus("You're on your way to THE HALL OF FAME:"
125                                     + onaroll + "Hits!");
126                 } else {
127                     getAppletContext().showStatus("YOU'RE ON A ROLL:"
128                                                   + onaroll + "Hits!");
129                 }
130             } else { //first hit
131
getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
132                 play(getCodeBase(), "sounds/that.hurts.au");
133                 onaroll = 1;
134             }
135         } else { //miss
136
getAppletContext().showStatus("You hit nothing at (" + x + ", "
137                                           + y + "), exactly");
138             play(getCodeBase(), "sounds/thin.bell.au");
139             onaroll = 0;
140         }
141         repaint();
142     }
143
144     public void mouseReleased(MouseEvent e) {}
145
146     public void mouseEntered(MouseEvent e) {
147         repaint();
148     }
149
150     public void mouseExited(MouseEvent e) {
151         repaint();
152     }
153
154     public void mouseClicked(MouseEvent e) {}
155
156     public void componentHidden(ComponentEvent e) {}
157  
158     public void componentMoved(ComponentEvent e) {}
159  
160     public void componentResized(ComponentEvent e) {
161         repaint();
162     }
163  
164     public void componentShown(ComponentEvent e) {
165         repaint();
166     }
167   
168     public void destroy() {
169         removeMouseListener(this);
170         removeMouseMotionListener(this);
171     }
172
173     public String JavaDoc getAppletInfo() {
174         return "Title: JumpingBox\n"
175             + "Author: Anonymous";
176     }
177 }
178
Popular Tags