/* * MWT - Micro Window Toolkit * Copyright (C) 2007 Lucas Domanico - lucazd@gmail.com * * Licensed under the terms of the GNU Lesser General Public License: * http://www.opensource.org/licenses/lgpl-license.php * * For further information visit: * http://j2me-mwt.sourceforge.net/ */ import java.util.Random; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Graphics; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import mwt.Button; import mwt.Component; import mwt.EventListener; import mwt.Label; import mwt.Window; // Dialogs // class Canvas7 extends Canvas implements Runnable, EventListener { Window win = new Window(20,20,100,100); // the main window -reference- boolean exit; // setted to true to finish the application static final int ACTION_EXIT = 1; // buttons' action ids static final int ACTION_CONFIRM = 2; static final int ACTION_PROGRESS = 3; static final int ACTION_CLOSEDIALOG = 4; // notify input protected void keyPressed(int keyCode) { win.setKeyState(keyCode,Window.KEYSTATE_PRESSED,true); } protected void keyReleased(int keyCode) { win.setKeyState(keyCode,Window.KEYSTATE_RELEASED,true); } // event listener implementation public void processEvent(int eventType, Component sender, Object[] args) { switch(eventType) { case EVENT_ACTION: // when a button is pressed an event action is triggered switch(((Button)sender).getActionType()) { case ACTION_CONFIRM: // display a confirm dialog final Window dialog = new Window(18,30,116,50); dialog.add(new Label(4,4,100,20,"Are you sure?")); dialog.add(new Button(40,20,30,20,"No",Canvas7.this,ACTION_CLOSEDIALOG)); dialog.add(new Button(4,20,30,20,"Yes",Canvas7.this,ACTION_PROGRESS)); dialog.setFocusFirst(); win.dialogOpen(dialog); break; case ACTION_PROGRESS: // display a dialog with a "progress bar" final Window progress = new Window(10,24,100,50); progress.add(new Button(4,20,40,20,"Cancel",Canvas7.this,ACTION_CLOSEDIALOG)); progress.add(new Label(4,4,100,20,"Progress:") { final long start = System.currentTimeMillis(); boolean finished; protected void paint(Graphics g, Window window) { int perc = (int) ((System.currentTimeMillis()-start) / 100); if(perc >= 100) { if(!finished) { finished = true; ((Button) progress.getChild(0)).setText("Ok"); } setText("Progress: finished!"); } else setText("Progress:" + ((System.currentTimeMillis()-start) / 100) + "%"); super.paint(g, window); } }); progress.setFocusFirst(); win.getDialog().dialogOpen(progress); break; case ACTION_CLOSEDIALOG: win.dialogClose(); break; case ACTION_EXIT: exit = true; return; } break; default: break; } } Canvas7() { win.add(new Label(4,4,80,20,"Options")); win.add(new Button(4,24,60,20,"Erase data",this,ACTION_CONFIRM)); win.add(new Button(4,54,60,20,"Exit",this,ACTION_EXIT)); win.setFocusFirst(); } public void run() { while(!exit) { // main loop win.repeatKeys(true); repaint(); serviceRepaints(); try { Thread.sleep(1); } catch(Exception e) { e.printStackTrace(); } } Example7.instance.notifyDestroyed(); } protected void paint(Graphics g) { g.setColor(0xFFFFFFFF); // clear graphics g.fillRect(0,0,getWidth(),getHeight()); Random r = new Random(); // paint the "background" for(int i=0; i<10 ;i++) { g.setColor(Math.abs(r.nextInt())); g.drawRect(Math.abs(r.nextInt())%128,Math.abs(r.nextInt())%128,Math.abs(r.nextInt())%128,Math.abs(r.nextInt())%128); } win.paint(g); // and paint the window... } } public class Example7 extends MIDlet { static MIDlet instance; protected void startApp() throws MIDletStateChangeException { instance = this; Canvas7 c = new Canvas7(); Display.getDisplay(this).setCurrent(c); new Thread(c).start(); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } }