请看下面的代码
首先,请注意,我是Java Mobile的100%新手。
在这里,当用户单击按钮时,我会点亮并振动。但是,我真的很想创建一个SOS应用程序,它将整个屏幕变成白色,然后在线程中变成黑色。我想我没有通过该应用程序实现这一目标,因为即使灯亮着,按钮仍然在那里。我试图将“窗体”颜色更改为“白色”,但似乎JME没有“颜色”类。
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(); } } } } }
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(); }
现在创建一个画布并实现如下所示的paint方法:
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> } }