Source Code Cross Referenced for SmallBall.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » demos » manyballs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » com.sun.midp.demos.manyballs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * 
003:         *
004:         * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation.
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt).
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions.
025:         */
026:
027:        package com.sun.midp.demos.manyballs;
028:
029:        import javax.microedition.lcdui.*;
030:
031:        /**
032:         * A SmallBall is a lightweight animated ball that runs in it's own thread.
033:         * It moves within a rectangular region, bouncing off the walls.
034:         */
035:        class SmallBall implements  Runnable {
036:
037:            // random number generator
038:            static java.util.Random random = new java.util.Random();
039:
040:            // controls the speed of all balls; delay in centiseconds
041:            static int delay = 20;
042:
043:            static void slower() {
044:                delay += 10;
045:                if (delay > 100)
046:                    delay = 100;
047:            }
048:
049:            static void faster() {
050:                delay -= 10;
051:                if (delay < 0)
052:                    delay = 0;
053:            }
054:
055:            // the matrix to transform the direction based on the
056:            // current direction and which wall was hit
057:            static int[][] matrix = { { 1, -1, -1, 1, 1, 1 },
058:                    { -1, -1, 1, 1, -1, 1 }, null, { 1, 1, -1, -1, 1, -1 },
059:                    { -1, 1, 1, -1, -1, -1 } };
060:
061:            // the region in which the ball moves
062:            int top, left, width, height;
063:
064:            // the position and radius of the ball
065:            int posX, posY;
066:            int radius = 5, ballSize = radius * 2;
067:
068:            // the direction of the ball is controlled by these two variables
069:            int deltaX;
070:            int deltaY;
071:
072:            // a handle onto the singleton Graphics object
073:            Graphics g;
074:            Canvas canvas;
075:
076:            // public variables to control the behaviour of the thread
077:            public boolean stop;
078:
079:            /**
080:             * Constructor defines the region in which the ball moves as well
081:             * as its starting position.
082:             */
083:            SmallBall(Canvas c, int left, int top, int width, int height) {
084:                super ();
085:                canvas = c;
086:
087:                this .left = left + 1;
088:                this .top = top + 1;
089:                this .width = width - (2 * radius + 2);
090:                this .height = height - (2 * radius + 2);
091:
092:                // use positive random #s
093:                this .posX = (random.nextInt() >>> 1) % (this .width - 20) + 10;
094:                this .posY = (random.nextInt() >>> 1) % (this .height - 20) + 10;
095:
096:                deltaX = random.nextInt() & 1;
097:                deltaY = random.nextInt() & 1;
098:
099:                if (deltaX == 0)
100:                    deltaX = -1;
101:                if (deltaY == 0)
102:                    deltaY = -1;
103:            }
104:
105:            /**
106:             * Starts the ball running.
107:             */
108:            public void run() {
109:                //	    System.out.println("starting... " + this);
110:                int right = left + width;
111:                int bottom = top + height;
112:
113:                stop = false;
114:                while (!stop) {
115:
116:                    ballSize = radius * 2;
117:
118:                    // calculate a direction of the ball 
119:                    // as an integer in the range
120:                    // -2 .. 2 (excluding 0)
121:                    int direction = deltaX + deltaY;
122:                    if (direction == 0)
123:                        direction = deltaX + 2 * deltaY;
124:
125:                    // is the current position colliding with any wall
126:                    int collision = 0;
127:                    if (posX <= left || posX >= right)
128:                        collision++;
129:                    if (posY <= top || posY >= bottom)
130:                        collision += 2;
131:
132:                    // change the direction appropriately 
133:                    // if there was a collision
134:                    if (collision != 0) {
135:                        collision = (collision - 1) * 2;
136:
137:                        deltaX = matrix[direction + 2][collision];
138:                        deltaY = matrix[direction + 2][collision + 1];
139:                    }
140:
141:                    // calculate the new position and queue a repaint
142:                    posX = posX + deltaX;
143:                    posY = posY + deltaY;
144:                    canvas.repaint(posX - 1, posY - 1, ballSize + 2,
145:                            ballSize + 2);
146:
147:                    // use the delay to control the speed of the ball
148:                    try {
149:                        Thread.sleep(delay);
150:                    } catch (InterruptedException e) {
151:                    }
152:                }
153:            }
154:
155:            /**
156:             * Paint the ball.
157:             */
158:            void paint(Graphics g) {
159:                g.setColor(0);
160:                g.fillArc(posX, posY, ballSize, ballSize, 0, 360);
161:            }
162:
163:            boolean inside(int x1, int y1, int x2, int y2) {
164:                return (posX <= x2) && (posY <= y2)
165:                        && ((posX + ballSize) >= x1)
166:                        && ((posY + ballSize) >= y1);
167:            }
168:
169:            public String toString() {
170:                return super .toString() + " x = " + posX + ", y = " + posY;
171:            }
172:
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.