/* * 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 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.Window; // Dynamic Properties // class Canvas6 extends Canvas implements Runnable, EventListener { Window win = new Window(8,8,100,100); // the main window -reference- Window sub = new Window(4,24,60,60); // a sub window Component panel = new Component(44,4,80,80,true); // component used as a panel boolean exit; // setted to true to finish the application static final int ACTION_MOVEUP = 0; // button's action ids static final int ACTION_MOVEDOWN = 1; static final int ACTION_MOVERIGHT = 2; static final int ACTION_MOVELEFT = 3; static final int ACTION_SHRINK = 4; static final int ACTION_STRETCH = 5; static final int ACTION_ENABLE = 6; static final int ACTION_EXIT1 = 7; static final int ACTION_EXIT2 = 8; // 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_MOVEUP: sub.setY(sub.getY()-4); break; case ACTION_MOVEDOWN: sub.setY(sub.getY()+4); break; case ACTION_MOVERIGHT: sub.setX(sub.getX()+4); break; case ACTION_MOVELEFT: sub.setX(sub.getX()-4); break; case ACTION_SHRINK: win.setWidth(win.getWidth()-2); win.setHeight(win.getHeight()-2); break; case ACTION_STRETCH: win.setWidth(win.getWidth()+2); win.setHeight(win.getHeight()+2); break; case ACTION_ENABLE: sub.setEnabled(!sub.isEnabled()); panel.setVisible(!panel.isVisible()); break; case ACTION_EXIT1: exit = true; break; case ACTION_EXIT2: exit = true; return; } break; default: break; } } Canvas6() { // initialize the window win.add(new Button(24,4,16,16,"?",this,ACTION_ENABLE)); win.add(new Button(4,4,16,16,"X",this,ACTION_EXIT1),0); panel.add(new Button( 0,0,16,16,"-",this,ACTION_SHRINK)); panel.add(new Button(20,0,16,16,"+",this,ACTION_STRETCH)); win.add(panel); sub.add(new Button(22, 4,16,16,"^",this,ACTION_MOVEUP)); sub.add(new Button( 4,22,16,16,"<",this,ACTION_MOVELEFT)); sub.add(new Button(40,22,16,16,">",this,ACTION_MOVERIGHT)); sub.add(new Button(22,40,16,16,"¬",this,ACTION_MOVEDOWN)); win.add(sub); win.setFocusFirst(); } public void run() { while(!exit) { // main loop win.repeatKeys(true); repaint(); serviceRepaints(); try { Thread.sleep(1); } catch(Exception e) { e.printStackTrace(); } } Example6.instance.notifyDestroyed(); } protected void paint(Graphics g) { g.setColor(0xFFFFFFFF); // clear graphics g.fillRect(0,0,getWidth(),getHeight()); win.paint(g); // and paint the window... } } public class Example6 extends MIDlet { static MIDlet instance; protected void startApp() throws MIDletStateChangeException { instance = this; Canvas6 c = new Canvas6(); Display.getDisplay(this).setCurrent(c); new Thread(c).start(); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } }