⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example9.java

📁 Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面(UI)的工具包。它具有友好
💻 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;


// Scroller
//
class Scroll extends Component {
	public int vertical;	// x axis offset
	public int horizontal;	// y axis offset
	private Component button; // this button enables and disables the scroller
	
	public Scroll(int x, int y, int width, int height) {
		super(x,y,width,height,true);
		
		// create the button behavior
		button = new Component(0,0,8,8,false) {
			boolean on; // when is true "arrow" keys will scroll
			protected boolean keyEvent(long key, Window window) {
				if((key >> 32) == 0 && window.getFocusAction(key) == Window.FOCUSACTION_FIRE) {
					on = !on;
				}
				else if(on) {
//					switch(Example9.canvas.getGameAction((int)key)){
//						case Canvas.DOWN:	vertical -= 1;return true;
//						case Canvas.UP:		vertical += 1;return true;
//					}
					switch((int)key) {
						case Canvas.KEY_NUM2:	vertical -= 1; break;
						case Canvas.KEY_NUM8:	vertical += 1; break;
						case Canvas.KEY_NUM4:	horizontal -= 1; break;
						case Canvas.KEY_NUM6:	horizontal += 1; break;
					}
				}
				else return false;
				return true;
			}
			protected void paint(Graphics g, Window window) {
				if(on) g.setColor(0xCFFF40);
				else g.setColor(window.getFocus() == this? 0xA1C632 : 0xC6C6C6);
				g.fillRect(0,0,getWidth(),getHeight());
				g.setColor(0);
				g.drawRect(0,0,getWidth()-1,getHeight()-1);
			}
		};
		add(button);
	}
	// The paint method moves all child components generating a scrolling effect
	protected void paint(Graphics g, Window window) {
		for(int i=0; i<getChildCount() ;i++) {
			final Component c = getChild(i);
			if(c != button) {
				c.setX(c.getX() + horizontal);
				c.setY(c.getY() + vertical);
			}
		}
		paintChilds(g,window);
		for(int i=0; i<getChildCount() ;i++) {
			final Component c = getChild(i);
			if(c != button) {
				c.setX(c.getX() - horizontal);
				c.setY(c.getY() - vertical);
			}
		}
		g.setColor(0);
		g.drawRect(0,0,getWidth()-1,getHeight()-1);
	}
	// prevent removing the button
	public void add(Component c, int index) {
		if(c != button && index == getChildCount()) index--; 
		super.add(c, index);
	}
	public void remove(int index) {
		if(getChild(index) != button) super.removeChild(index);
	}
}


class Canvas9 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 = 0; // button's action ids
	
	// 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_EXIT: exit = true; return;
				}
				break;
			default: break;
		}
	}
	
	Canvas9() {
		win.add(new Label(4,4,100,20,"Sample Scroller"));
		Scroll scroll = new Scroll(20,24,60,60);
		scroll.add(new Label(4, 4,400,20,"Use the scroll to read the entire text"));
		scroll.add(new Label(4,20,400,20,"See below this text and find the exit button!"));
		scroll.add(new Button(4,80,60,20,"Exit",this,ACTION_EXIT));
		win.add(scroll);
		win.setFocusFirst();
	}
	
	public void run() {
		while(!exit) { // main loop
			win.repeatKeys(true);
			repaint();
			serviceRepaints();
			try { Thread.sleep(1); }
			catch(Exception e) { e.printStackTrace(); }
		}
		Example9.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 Example9 extends MIDlet {
	static MIDlet instance;
	static Canvas canvas;
	protected void startApp() throws MIDletStateChangeException {
		instance = this;
		Canvas9 c = new Canvas9();
		canvas = c;
		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 + -