Question

Please have a look at the following code

First, Please note I am a 100% newbie to Java Mobile.

In here, I am making the light on and vibrate on when user click the button. However, I really wanted to create a SOS application which turn the whole screen into white, and go to black, like that, in the thread. I guess I didn't achieve that by this app because even the lights are on, the buttons are still there. I tried to turn the "Form" color to "white" but it seems like JME has no "Color" class.

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

public class Midlet extends MIDlet{

    private Form f;
    private Display d;
    private Command start,stop;
    private Thread t;

    public Midlet()
    {
        t = new Thread(new TurnLightOn());

    }

    public void startApp() 
    {
        f = new Form("Back Light On");


       d = Display.getDisplay(this);
       d.setCurrent(f);        

       start = new Command("Turn On",Command.OK,0);
       stop = new Command("Turn Off",Command.OK,1);

       f.addCommand(start);
       f.setCommandListener(new Action());



    }

    public void pauseApp() {
    }

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

    private class Action implements CommandListener
    {

        public void commandAction(Command c, Displayable dis) 
        {
            f.append("Light is Turnning On");

            t.start();

        }

    }

     private class ActionOff implements CommandListener
    {

        public void commandAction(Command c, Displayable dis) 
        {


        }

    }

    private class TurnLightOn implements Runnable
    {

        public void run() 
        {
            f.append("Working");
            for(int i=0;i<100;i++)
            {

                try 
                {

                    d.flashBacklight(200);
                    d.vibrate(200);

                    Thread.sleep(1000);

                } 
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
            }
        }

    }
}
Was it helpful?

Solution

    public void startApp() 
        {
            f = new Form("Back Light On");


           d = Display.getDisplay(this);


           start = new Command("Turn On",Command.OK,0);
           stop = new Command("Turn Off",Command.OK,1);

           f.addCommand(start);
           f.setCommandListener(new Action());

    myCanvas = new MyCanvas();
     d.setCurrent(myCanvas);   
            myCanvas.repaint();

}

Now create a canvas and implement paint method like this:

  class MyCanvas extends Canvas {
            public void paint(Graphics g) {
                // create a 20x20 black square in the center

                // clear the screen first
                g.setColor(0xffffff);
                g.fillRect(0, 0, getWidth(), getHeight());

                g.setColor(0xffffff); // make sure it is white color

                // draw the square, <b>changed to rely on instance variables</b>
                <b>g.fillRect(x, y, getWidth(), getHeight());</b>
            }
        }

OTHER TIPS

Use the javax.microedition.lcdui.Canvas instead of Form. This example can get you started

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top