📄 example4.java
字号:
package example;
/*
* 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;
// Key States
//
class Canvas4 extends Canvas implements Runnable, EventListener {
Window win = new Window(8,8,100,100); // the main window -reference-
boolean exit; // setted to true to finish the application
int x; // the sprite's x and y
int y;
static final int ACTION_RESUME = 0; // button's action ids
static final int ACTION_EXIT = 1;
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_RESUME: win.setVisible(false); break;
case ACTION_EXIT: exit = true; break;
}
break;
default: break;
}
}
Canvas4() {
win.setVisible(false); // the window's initially hidden
win.add(new Label(4,4,30,20,"Menu"));
win.add(new Button(4,20,50,20,"Resume",this,ACTION_RESUME));
win.add(new Button(4,50,30,20,"Exit",this,ACTION_EXIT));
}
public void run() {
while(!exit) { // main loop
win.repeatKeys(true);
if(!win.isVisible()) {
// move the sprite
if(win.getKeyState(KEY_NUM4) != 0) x--;
if(win.getKeyState(KEY_NUM6) != 0) x++;
if(win.getKeyState(KEY_NUM2) != 0) y--;
if(win.getKeyState(KEY_NUM8) != 0) y++;
if(win.getKeyState(KEY_NUM0) != 0) { // set the window
win.setVisible(true);
win.setFocusFirst();
}
}
repaint();
serviceRepaints();
try { Thread.sleep(1); }
catch(Exception e) { e.printStackTrace(); }
}
Example4.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);
}
g.setColor(0,0,255); // paint the sprite
g.fillRect(x,y,20,20);
win.paint(g); // and paint the window...
}
}
public class Example4 extends MIDlet {
static MIDlet instance;
protected void startApp() throws MIDletStateChangeException {
instance = this;
Canvas4 c = new Canvas4();
Display.getDisplay(this).setCurrent(c);
new Thread(c).start();
}
protected void pauseApp() { }
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -