Hello all...first-time poster here, and I was wondering if any of you fine folk can help me out with what is surely a simple task, but something very confusing for someone just starting off with Java.

In a nutshell, I am creating a simple mobile game where a user is directed to tap on randomly flashing squares (through their touch screen), thus earning a score with each successful tap of a generated square. The GUI of the game itself is very barebones, with 2 options on the bottom of the screen, which are "Exit" and "Stop". What I am stuck on is the actual syntax needed to have it so that when a user taps on the "Stop" button, the game itself ends, and the "Stop" button then switches to a "Start" option where by tapping that, the game begins anew. As it stands, I'm a little perplexed here as to how this can be achieved...I have tried a few things here and there, but nothing seems to actually stop the game when hitting the "Stop" button. I have attached a screenshot of the game in action so it can give a better visual cue as to what this is all about.

Here is the code I have jumbled up thus far...I am sure it's as sloppy as can be, so excuse the messiness:

package dotsmasher;

import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;



public class DotSmasher extends MIDlet implements
CommandListener{

    private Timer timer;
    private DotSmasherCanvas canvas;
    private DotSmasherTimerTask task;
    private Display display;
    private Command exit;
    private Command stop;
    private Command start;
    public DotSmasher(){
        canvas = new DotSmasherCanvas();
        timer = new Timer();
        task = new DotSmasherTimerTask(canvas);
        exit = new Command("Exit", Command.EXIT, 1);
        stop = new Command("Stop", Command.STOP, 2);
        start=new Command("Start", Command.ITEM, 1);
    }

    public void startApp() {
        display = Display.getDisplay(this);
        canvas.addCommand(exit);
        canvas.addCommand(stop);
        canvas.setCommandListener(this);
        display.setCurrent(canvas);
        timer.schedule(task, 0, 1500);
    }

    public void pauseApp() {
    }

    public void stop() {

    }

    private void addCommand(Command start) {
    }

    private void removeCommand(Command stop) {
    }

    private void pauseApp(boolean b) {
        
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        String label = c.getLabel();
        if (label.equals("Exit")) {
            destroyApp(true);
        }
        else if(c==stop) {
            canvas.removeCommand(stop);
            canvas.addCommand(start);
    }
        else if(c==start) {
            canvas.removeCommand(start);
            canvas.addCommand(stop);
}

}

}
package dotsmasher;
import java.util.*;


public class DotSmasherTimerTask extends TimerTask{

    private DotSmasherCanvas canvas;
    DotSmasherTimerTask(DotSmasherCanvas canvas){
        this.canvas = canvas;
    }

    public void run() {
        canvas.moveDot();
        canvas.repaint();
    }

}
package dotsmasher;
import javax.microedition.lcdui.*;
import java.util.Random;


class DotSmasherCanvas extends Canvas {

private int dotX,dotY;// the coordinates of the dot
private int score;

    public int getDotX() {
        return dotX;
    }

    public void setDotX(int dotX) {
        this.dotX = dotX;
    }

    public int getDotY() {
        return dotY;
    }

    public void setDotY(int dotY) {
        this.dotY = dotY;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

public DotSmasherCanvas(){
moveDot();
}

    protected void paint(Graphics g) {
        g.fillRect(0, 0, getWidth(), getHeight());//clear the screen
        g.setColor(0xff0000); // choose your color! Red
        g.fillRect(getDotX(),getDotY(),20,20);
        g.drawString("Score: " + score, 10, g.getClipHeight()-20, 0);

    }

    protected void pointerReleased(int x, int y) {
        if(detectHit(x,y)){
            score += 1;
            repaint();
        }
    }

    protected boolean detectHit(int x, int y){
        if((x>=dotX&&x<=dotX+20)&&(y>=dotY&&y<=dotY+20)){
            //we have a hit
            return true;
        }
        return false;
    }

    protected void moveDot(){
    //create 2 random numbers
    //assign the new numbers to dotX, dotY
    Random generator = new Random();
    generator.setSeed(System.currentTimeMillis());
    int w = getWidth()-20;
    int h = getHeight()-40;//to avoid covering score
    float f = generator.nextFloat();
    dotX = (int)(f*w)%w;
    f = generator.nextFloat();
    dotY = (int)(f*h)%h;
}

    public void stop() {

    }

}

I very much appreciate any insight you may be able to give on this!

Recommended Answers

All 2 Replies

This should do it:

public void commandAction(Command c, Displayable d) {
        String label = c.getLabel();
        if (label.equals("Exit")) {
            destroyApp(true);
        }
        else if(c==stop) {
            timer.cancel();
            canvas.removeCommand(stop);
            canvas.addCommand(start);
    }
        else if(c==start) {
            timer.start(task, 0, 1500);
            canvas.removeCommand(start);
            canvas.addCommand(stop);
}

Many thanks for your help, burgercho! Much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.