/* * flashlight.java * * Created on January 27, 2008, 4:29 PM *Forrest Heller 2008 *Released under GPL */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*; /** * * @author Forrest Heller *This code is not commented because I don't care. */ public class flashlight extends MIDlet implements CommandListener { flashlight_canvas f; /** Creates a new instance of flashlight */ public flashlight() { initialize(); } private Form controls;//GEN-BEGIN:MVDFields private StringItem controls_box; private Command okCommand1; private Command back_command; private StringItem stringItem1;//GEN-END:MVDFields //GEN-LINE:MVDMethods /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin * @param command the Command that ws invoked * @param displayable the Displayable on which the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin // Insert global pre-action code here if (displayable == controls) {//GEN-BEGIN:MVDCABody if (command == back_command) {//GEN-END:MVDCABody // Do nothing//GEN-LINE:MVDCAAction13 this.getDisplay().setCurrent(f); }//GEN-BEGIN:MVDCACase13 }//GEN-END:MVDCACase13 }//GEN-LINE:MVDCAEnd /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin */ private void initialize() {//GEN-END:MVDInitBegin back_command = new Command("Back", Command.BACK, 1);//GEN-BEGIN:MVDInitInit controls_box = new StringItem("Controls", "0=quit\n1=on / flashing / off\n2=show this\nColors [Hold down keys]:\n4=turn up red\n5=turn up green\n6=turn up blue\n7=turn down red\n8=turn down green\n9=turn down blue\nMax red green and blue is white.\nMin red green and blue is black."); stringItem1 = new StringItem("About", "Flashlight by Forrest Heller (forrest@forrestheller.com)\nThis software is released under the GPL and contains no ads or whatever."); controls = new Form(null, new Item[] { controls_box, stringItem1 }); controls.addCommand(back_command); controls.setCommandListener(this); okCommand1 = new Command("Ok", Command.OK, 1);//GEN-END:MVDInitInit }//GEN-LINE:MVDInitEnd public Display getDisplay() {//GEN-FIRST:MVDGetDisplay return Display.getDisplay(this); }//GEN-LAST:MVDGetDisplay public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet getDisplay().setCurrent(null); destroyApp(true); notifyDestroyed(); }//GEN-LAST:MVDExitMidlet public void show_help() { this.getDisplay().setCurrent(controls); } public void startApp() { f = new flashlight_canvas(0xffffff,this); getDisplay().setCurrent(f); } public void pauseApp() { } public void destroyApp(boolean unconditional) { f.stop(); } } class flashlight_canvas extends Canvas implements Runnable { public int bgcolor=0xffffff; int state = 0; flashlight ref_f; java.util.Random r; Thread game_thread; int sleep_time = 1000; public boolean is_running; public flashlight_canvas(int bcolor, flashlight f) { bgcolor = bcolor; ref_f = f; is_running = true; game_thread = new java.lang.Thread(this); game_thread.start(); this.setFullScreenMode(true); r = new Random(); r.setSeed(5175921); } public void stop() { is_running = false; /*wait for thread to die*/ try { game_thread.join(); } catch (Exception ex) { } /*reset all weapons*/ } public void run() { while(is_running) { this.repaint(); Thread.yield(); } } public void keyPressed(int keycode) { switch(keycode) { case Canvas.KEY_NUM0: ref_f.exitMIDlet(); break; case Canvas.KEY_NUM2: this.ref_f.show_help(); break; case Canvas.KEY_NUM1: switch(state) { case 0: state = 1; break; case 1: state = 2; bgcolor = 0x0; break; case 2: state =0; Display.getDisplay(ref_f).flashBacklight(0); bgcolor =0x00ffffff; break; } this.repaint(); break; default: switch(this.getGameAction(keycode)) { /* case Canvas.UP: if (sleep_time > 200) sleep_time -= 200; break; case Canvas.DOWN: sleep_time += 200; break; */ } } } public void keyRepeated(int keycode) { int red,green,blue; red = (bgcolor & 0xff0000); green = bgcolor & 0x00ff00; blue = bgcolor & 0x0000ff; switch(keycode) { case Canvas.KEY_NUM4: if (red < 0xff0000) bgcolor = (bgcolor & 0x00ffff) | (red + 0x0f0000); this.repaint(); break; case Canvas.KEY_NUM5: if (green < 0xff00) bgcolor = (bgcolor & 0xff00ff) | (green + 0x000f00); this.repaint(); break; case Canvas.KEY_NUM6: if (blue < 0xff) bgcolor = (bgcolor & 0xffff00) | (blue + 0x00000f); this.repaint(); break; case Canvas.KEY_NUM7: if (red > 0x00ffff) bgcolor = (bgcolor & 0x00ffff) | (red - 0x0f0000); this.repaint(); break; case Canvas.KEY_NUM8: if (green > 0xff) bgcolor = (bgcolor & 0xff00ff) | (green - 0x000f00); this.repaint(); break; case Canvas.KEY_NUM9: if (blue > 0) bgcolor = (bgcolor & 0xffff00) | (blue - 0x00000f); this.repaint(); break; } } public void paint(Graphics g) { if (state == 1) { bgcolor= r.nextInt() & 0x00ffffff; } g.setColor(bgcolor); g.fillRect(0,0,g.getClipWidth(),g.getClipHeight()); g.fillRect(0,0,g.getClipWidth(),g.getClipHeight()); if (state != 2) Display.getDisplay(ref_f).flashBacklight(1000); } }