Write a small game, do not know why the second time running, but less time

2010-09-23  来源:本站原创  分类:Mobile  人气:138 

package CANVAS3;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Midlet2 extends MIDlet {
        class GuessGame extends Canvas implements Runnable, CommandListener {
                private String num = "-1";//  Define shipborne machine DVD
                String action = "";
                private int x = 0;
                private int y = 0;
                private int dots = 5;//  The initial minute DVD
                private Thread th;
                private boolean RUN = true;
                private Command cmdReStart = new Command(" Start again  ", Command.SCREEN, 1);

                public GuessGame() {
                        th = new Thread(this);

                public void commandAction(Command c, Displayable d) {
                        if (c == cmdReStart) {
                                RUN = true;
                                num = "-1";
                                dots = 5;
                                action = "";
                                th = new Thread(this);

                public void paint(Graphics g) {
                        g.setColor(255, 255, 255);
                        g.fillRect(0, 0, this.getWidth(), this.getHeight());
                        g.setColor(255, 0, 0);
                        g.drawString(num.equals("-1") ? "" : num, x, y, Graphics.TOP
                                        | Graphics.LEFT);
                        if (dots <= 0) {
                                g.setColor(255, 255, 255);
                                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                                g.setColor(255, 0, 0);
                                g.drawString(" You have the content of the document  ", this.getWidth() / 2, this.getHeight() / 2,
                                                Graphics.TOP | Graphics.HCENTER);
                                RUN = false;
                                th = null;

                public void keyPressed(int keyCode) {
                        if (dots > 0) {
                                action = this.getKeyName(keyCode);
                                if (num.equals(action)) {
                                        dots += 1;
                                } else {
                                        dots -= 2;


                public void run() {
                        while (RUN) {
                                Random r = new Random();
                                num = String.valueOf(r.nextInt(9));
                                x = r.nextInt(this.getWidth());
                                y = r.nextInt(this.getHeight());

                                try {
                                } catch (Exception e) {

                                System.out.println(" Submit machine DVD for  :" + num);
                                System.out.println(" Press the button for  :" + action);
                                System.out.println(" Current minute DVD  :" + dots);


                                if (action.equals("")) {//  Control if the hyper-time rules for a


        private Display dis;
        private GuessGame gg = new GuessGame();

        protected void startApp() throws MIDletStateChangeException {
                dis = Display.getDisplay(this);

        protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
                // TODO Auto-generated method stub


        protected void pauseApp() {
                // TODO Auto-generated method stub


Write the above code is a small game, do not know why, click start again, always less time (when not click on any key), where is not a mistake, and please judge by look!

  • J2EE questions Noodles 2009-02-27

    2. Abstract class and interface difference (1) interface can be multiple implements, can only be a single abstract class extends (2) only the definition of interfaces, abstract class can have the definition and implementation (3) the definition of th

  • Great collection of java interview topics 2009-02-27

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract d

  • Learn Java flow 2009-02-27

    Related Articles: J2EE without EJB Introducing to Spring Framework (English revised edition) J2EE without EJB caused consider Recommend circles: reading space More related recommend Java Learning Path (1), tools, articles First, JDK (Java Development

  • What is the appfuse 2009-02-27

    First, Appfuse brief introduction Matt Raible are Appfuse developed a guiding entry-level J2EE framework, how to integrate its popular Spring, Hibernate, ibatis, struts, Xdcolet, junit, etc. give the basic framework of the model, the latest version 1

  • JAVA interview questions 2009-02-27

    JAVA interview questions 1, object-oriented features of what has 1. Abstract 2. Inheritance 3. Packaging 4. Polymorphisms 2, String data types are the most basic right? Basic data types include byte, int, char, long, float, double, boolean and short.

  • Some interview questions java 2009-02-27

    The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, al

  • Openfire Plugin Developer's Guide 2009-02-28

    Introduction Openfire features plug-ins are enhanced. This document is a guide to developers to create plug-ins. The structure of a plug-in Plug-ins Plug-ins openfireHome stored in the directory. When deploying a plug-in jar or war file, it will auto

  • jBPM Development Getting Started Guide 2009-02-28

    Although the workflow is still immature stage of development, not even a recognized standard. But its application has already been launched in the Express, indicating the market's demand for job-flow framework are urgent and enormous. Backgrounds of

  • web test summary 2009-02-28

    In the Web engineering process, Web-based system testing, recognition and acceptance is an important and challenging job. Web-based system testing with the traditional software testing different, it will not only need to check and verify whether the

  • servlet specification 2.2 2009-02-28

    Introduction: Spring Security watch, when to see more use of the Servlet Listener, filter and so on. Filter done, listener never. Therefore want from the Internet to find a closer look at the servlet specification, go thorough investigation, there is

  • Building Scalable java EE application (2) 2009-02-28

    When the number of concurrent users marked the beginning of growth, you may be dissatisfied with a single machine can provide the performance, or because of a single JVM instance gc restrictions, you can not expand your java application, in such circ

  • hibernate load delay 2009-02-28

    Lazy loading: Lazy loading mechanism is in order to avoid unnecessary performance overhead and put forward the so-called lazy loading is required when the real data at a time when the real implementation of the data load operation. At Hibernate provi

  • Shoes - Construction Ruby client gadget 2009-02-28

    Shoes Ruby are cross-platform GUI applications, recently released a new version, it is a very interesting Ruby GUI client, the following are some characteristics of Introduction: 1. Cross-platform support for windows / linux / mac os 2. Very small, t

  • JUnit Unit Testing Summary 2009-03-01

    Read some of the unit test article, from the article in the interception of a number of information, which could be considered in the learning process, a summary of it! See future! Unit testing code is not used to prove you are right, but in order to

  • Software engineer must know the concept of the 10 2009-03-01

    35 kilometers wrote "excellent software engineers make the best use of design patterns and diligent code Reconstruction, the preparation of unit testing, and has easy religious pursuit. In addition to these, excellent software engineers but also prof

  • JS after ajax request return of the problem can not be implemented 2009-03-01

    1: Send ajax request, in the onComplete, if back when the html contains a javascript, then these javascrip and will not be realized, it does not mean not to implement. This problem has troubled me for a long time, behind the hair and then put this kn

  • In the Windows platform using Apache2.2 and Mongrel running Ruby on Rails 2009-03-02

    First, install Ruby, rails, mongrel and Apache2.2 Rubyforge download from the web site One-Click Ruby Install, run setup on installed ruby and rubygems. Run the command: gem install rails-y gem install mongrel-y gem install mongrel_service-y Installe

  • In the Linux platform to install and configure Ruby on Rails Detailed 2009-03-02

    ruby on rails recommend the production environment is running Linux / FreeBSD / Unix, or Unix family of operating systems, using lighttpd + FCGI solution. The following will be my Linux operating system, lighttpd + FCGI, MySQL database as an example,

  • Talk about the "design mode" 2009-03-02

    Now more and more frameworks; model appears to reduce the use of the opportunity, then mean that we do not have to grasp the mode of the case? In fact, in order to study the actual training model mode of thinking, modes of thinking help to understand

  • Wait for prompt js General category 2009-03-03

    This is a very easy-to-use widget, the parameters have default values, default values, see the Notes wait = new WaitingTip(); wait.show(document.getElementById('id')); //Parameter 1 is displayed, in reference to the object ... wait.hide(); //Hide The