分享
 
 
 

时钟计时器

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

*--------------------------------------------------

* AnimatedTimer - Main midlet.

* Shows canvas with an animated timer. Includes

* configuration options to start/stop the timer

* and to adjust the sleep interval of the thread

*

* Example from the book: Core J2ME Technology

* Copyright John W. MUChow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

import java.util.Stack;

import javax.microedition.lcdui.Alert;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Display;

import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.Form;

import javax.microedition.lcdui.Gauge;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.List;

import javax.microedition.midlet.MIDlet;

public class AnimatedTimer extends MIDlet {

PRivate Display display; // The display

protected TimerCanvas cvTimer; // Canvas to display timer

protected OptionsList lsOptions; // List to change timer options

protected SleepForm fmSleep; // Form with gauge to set timer sleep

protected DisplayManager displayMgr; // Class to help manage screens

public AnimatedTimer() {

display = Display.getDisplay(this);

cvTimer = new TimerCanvas(this);

lsOptions = new OptionsList("Timer options", List.IMPLICIT, this);

fmSleep = new SleepForm("Adjust sleep", this);

// Create a display manager object

displayMgr = new DisplayManager(display, cvTimer);

}

protected void startApp() {

// Start with the canvas

display.setCurrent(cvTimer);

}

protected void pauseApp() {

}

protected void destroyApp(boolean unconditional) {

}

public void exitMIDlet() {

destroyApp(true);

notifyDestroyed();

}

}

/*--------------------------------------------------

* Use a stack to push and pop displayable objects

*

* public void pushDisplayable(Displayable)

* public void popDisplayable()

* public void home()

*

* Example from the book: Core J2ME Technology

* Copyright John W. Muchow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

class DisplayManager extends Stack {

private Display display; // Reference to Display object

private Displayable mainDisplayable; // Main displayable for MIDlet

private Alert alStackError; // Alert for error conditions

/*--------------------------------------------------

* Display manager constructor

*-------------------------------------------------*/

public DisplayManager(Display display, Displayable mainDisplayable) {

// Only one display object per midlet, this is it

this.display = display;

this.mainDisplayable = mainDisplayable;

// Create an alert displayed when an error occurs

alStackError = new Alert("Displayable Stack Error");

alStackError.setTimeout(Alert.FOREVER); // Modal

}

/*--------------------------------------------------

* Push the current displayable onto stack and set

* the passed in displayable as active

*-------------------------------------------------*/

public void pushDisplayable(Displayable newDisplayable) {

push(display.getCurrent());

display.setCurrent(newDisplayable);

}

/*--------------------------------------------------

* Return to the main displayable object of MIDlet

*-------------------------------------------------*/

public void home() {

while (elementCount > 1)

pop();

display.setCurrent(mainDisplayable);

}

/*--------------------------------------------------

* Pop displayable from stack and set as active

*-------------------------------------------------*/

public void popDisplayable() {

// If the stack is not empty, pop next displayable

if (empty() == false)

display.setCurrent((Displayable) pop());

else

// On error show an alert

// Once acknowledged, set 'mainDisplayable' as active

display.setCurrent(alStackError, mainDisplayable);

}

}

/*--------------------------------------------------

* Class TimerCanvas

*

* Animate a sequence of images to simulate

* a moving timer

*

* Example from the book: Core J2ME Technology

* Copyright John W. Muchow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

class TimerCanvas extends Canvas implements Runnable, CommandListener {

private AnimatedTimer midlet; // Main midlet

private Command cmExit; // Exit midlet

private Command cmOptions; // Display options list

private Image im = null; // Sequence of images

private int imageCount = 4; // Four images in the sequence

private int imageWidth; // Width of one image in the sequence

private int imageHeight; // Height of one image in the sequence

private int imageIndex; // Current image in the sequence

private int translate_x; // Translated x and y

private int translate_y;

private int viewport_x; // Location of the viewport

private int viewport_y;

private boolean active = false; // Timer active?

private boolean requestedToStop = false; // Did user request to stop timer

private int sleepTime = 400; // Current sleep time (milliseconds)

public TimerCanvas(AnimatedTimer midlet) {

// Call canvas constructor

super();

// Save reference to MIDlet so we can

// access the display manager class

this.midlet = midlet;

// Create commands & listen for events

cmExit = new Command("Exit", Command.EXIT, 1);

cmOptions = new Command("Config", Command.SCREEN, 2);

addCommand(cmExit);

addCommand(cmOptions);

setCommandListener(this);

}

/*--------------------------------------------------

* application manager is about to display canvas

*-------------------------------------------------*/

protected void showNotify() {

if (im == null) {

try {

// Read the png from a file and get width and

// height of one image in the sequence

im = Image.createImage("/timer.png");

imageHeight = im.getHeight();

imageWidth = im.getWidth() / imageCount;

// Get the coordinates for x/y of viewport

// Viewport is centered on the display

viewport_x = (getWidth() / 2) - (imageWidth / 2);

viewport_y = (getHeight() / 2) - (imageHeight / 2);

// Set first translated coordinate to match viewport

translate_x = viewport_x;

translate_y = viewport_y;

} catch (Exception e) {

System.err.println("Unable to read png file.");

}

// Begin with the first image in the sequence

imageIndex = 0;

}

// If the user has not requested to stop the timer...

if (!requestedToStop)

active = true;

new Thread(this).start();

}

/*--------------------------------------------------

* Application manager is no longer displaying canvas

*-------------------------------------------------*/

protected void hideNotify() {

active = false;

}

/*--------------------------------------------------

* Draw next timer in sequence

*-------------------------------------------------*/

protected void paint(Graphics g) {

if (im != null) {

// Due to a bug in MIDP 1.0.3 we need to

// force a clear of the display

g.setColor(255, 255, 255); // White pen

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(0, 0, 0); // Black pen

// Viewport at center of display

g.setClip(viewport_x, viewport_y, imageWidth, imageHeight);

// Draw image at translated coordinates

g.drawImage(im, translate_x, translate_y, Graphics.TOP

Graphics.LEFT);

}

}

/*--------------------------------------------------

* Loop forever, translating image coordinates

*-------------------------------------------------*/

public void run() {

try {

while (active) {

Thread.sleep(sleepTime);

repaint();

// Reached the last image in sequence

if (imageIndex == imageCount - 1) {

// Reset translated coordinates

translate_x = viewport_x;

translate_y = viewport_y;

} else {

// Translate coordinate system to the left

translate_x -= imageWidth;

}

// Which image in the sequence is next

imageIndex = (imageIndex + 1) % imageCount;

}

} catch (InterruptedException e) {

}

}

/*--------------------------------------------------

* Called from the "Config" options menu

*-------------------------------------------------*/

public void startTimer() {

requestedToStop = false;

active = true;

repaint();

}

/*--------------------------------------------------

* Called from the "Config" options menu

*-------------------------------------------------*/

public void stopTimer() {

requestedToStop = true;

active = false;

repaint();

}

/*--------------------------------------------------

* Called from form/gauge to adjust sleep

*-------------------------------------------------*/

public void setSleep(int sleepTime) {

this.sleepTime = sleepTime;

}

/*--------------------------------------------------

* Called from form/gauge to adjust sleep

*-------------------------------------------------*/

public int getSleep() {

return sleepTime;

}

/*--------------------------------------------------

* Command event handling

*-------------------------------------------------*/

public void commandAction(Command c, Displayable s) {

if (c == cmOptions) {

// Push current displayable and show the options list

midlet.displayMgr.pushDisplayable(midlet.lsOptions);

} else if (c == cmExit) {

midlet.exitMIDlet();

}

}

}

/*--------------------------------------------------

* Class SleepForm

*

* Form with gauge to adjust sleep interval of timer

*

* Example from the book: Core J2ME Technology

* Copyright John W. Muchow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

class SleepForm extends Form implements CommandListener {

private AnimatedTimer midlet; // Main midlet

private Command cmBack, // Back to options list

cmHome, // Go to main displayable (canvas)

cmSave; // Save new sleep time

private Gauge gaSleep; // Gauge to adjust sleep

public SleepForm(String title, AnimatedTimer midlet) {

// Call the form constructor

super(title);

// Save reference to MIDlet so we can

// access the display manager class

this.midlet = midlet;

// Commands

cmSave = new Command("Save", Command.SCREEN, 1);

cmBack = new Command("Back", Command.BACK, 2);

cmHome = new Command("Home", Command.SCREEN, 2);

// Gauge to adjust the length of timer sleep

gaSleep = new Gauge("Timer Sleep", true, 100, 1000);

// Set to current sleep. Gauge holds values 0 to 100,

// divide the current sleep (milliseconds) by 10

gaSleep.setValue(midlet.cvTimer.getSleep() / 10);

// Add to form and listen for events

append(gaSleep);

addCommand(cmSave);

addCommand(cmBack);

addCommand(cmHome);

setCommandListener(this);

}

/*--------------------------------------------------

* Command event handling

*-------------------------------------------------*/

public void commandAction(Command c, Displayable s) {

if (c == cmSave) {

// Gauge returns a value between 0 and 100

// We want milliseconds, so multiply by 10

midlet.cvTimer.setSleep(gaSleep.getValue() * 10);

// Return to main midlet

midlet.displayMgr.home();

} else if (c == cmBack) {

// Pop the last displayable off the stack

midlet.displayMgr.popDisplayable();

} else if (c == cmHome) {

// Return to main midlet

midlet.displayMgr.home();

}

}

}

/*--------------------------------------------------

* Class OptionsList

*

* List to provide options for configuring of timer

*

* Example from the book: Core J2ME Technology

* Copyright John W. Muchow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

class OptionsList extends List implements CommandListener {

private AnimatedTimer midlet; // Main midlet

private Command cmBack;

public OptionsList(String title, int listType, AnimatedTimer midlet) {

// Call list constructor

super(title, listType);

// Save reference to MIDlet so we can

// access the display manager class

this.midlet = midlet;

// Create the list entries

append("Sleep interval", null);

append("Start", null);

append("Stop", null);

// Create command and listen for events

cmBack = new Command("Back", Command.BACK, 1);

addCommand(cmBack);

setCommandListener(this);

}

/*--------------------------------------------------

* Command event handling

*-------------------------------------------------*/

public void commandAction(Command c, Displayable s) {

// Event generated by the implicit list

if (c == List.SELECT_COMMAND) {

switch (getSelectedIndex()) {

case 0:

// Push current displayable and show the form

// to adjust the timer sleep

midlet.displayMgr.pushDisplayable(midlet.fmSleep);

break;

case 1:

// Start timer and return to previous displayable

midlet.cvTimer.startTimer();

midlet.displayMgr.popDisplayable();

break;

case 2:

// Stop timer and return to previous displayable

midlet.cvTimer.stopTimer();

midlet.displayMgr.popDisplayable();

break;

}

} else if (c == cmBack) {

// Return to previous displayable

midlet.displayMgr.popDisplayable();

}

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有