?? stopwatchcanvas.java
字號:
//
// Copyright ? 2002 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at any time,
// without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation. Java and all Java-based marks are trademarks or registered
// trademarks of Sun Microsystems, Inc. Other product and company names
// mentioned herein may be trademarks or trade names of their respective owners.
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
//
package example.stopwatch;
import javax.microedition.lcdui.*;
import java.util.Random;
class StopwatchCanvas
extends Canvas
implements CommandListener, Runnable
{
private static final int DIGIT_INTERVAL = 1;
private static final int DOT_SIZE = 3;
private static final int[] base = {10, 1000, 60000, 3600000};
private static final Image[][] digitImage;
private static final int digitWidth, digitHeight;
private static final Image labelImage;
private final Command startCommand;
private final Command continueCommand;
private final Command stopCommand;
private final Command resetCommand;
private final Command quitCommand;
private final int[] digit = new int[4];
private final StopwatchMIDlet parent;
private final int labelY, dotY;
private final int width, height;
private final int panelX, panelY;
private Thread thread = null;
private long startTime = 0;
private long remainingTime = 0;
private volatile boolean startPressed = false;
static
{
Image fontImage = loadImage("/font.png");
digitWidth = fontImage.getWidth() / 10;
digitHeight = fontImage.getHeight() / 2;
digitImage = new Image[10][2];
for (int i=0; i<10; i++)
{
for (int k=0; k<2; k++)
{
digitImage[i][k] = Image.createImage(digitWidth, digitHeight);
Graphics gDigit = digitImage[i][k].getGraphics();
gDigit.drawImage(fontImage,
-i * digitWidth,
-k * digitHeight,
Graphics.TOP|Graphics.LEFT);
}
}
labelImage = loadImage("/label.png");
}
StopwatchCanvas(StopwatchMIDlet parent)
{
this.parent = parent;
width = getWidth();
height = getHeight();
panelX = (width - (8 * digitWidth) - (4 * DIGIT_INTERVAL) -
(3 * DOT_SIZE)) / 2;
panelY = ((height - digitHeight) / 2) + 4;
dotY = panelY + digitHeight - DOT_SIZE;
labelY = panelY - labelImage.getHeight() - 8;
quitCommand = new Command("Quit", Command.EXIT, 2);
addCommand(quitCommand);
startCommand = new Command("Start", Command.SCREEN, 1);
addCommand(startCommand);
stopCommand = new Command("Stop", Command.SCREEN, 1);
resetCommand = new Command("Reset", Command.SCREEN, 1);
continueCommand = new Command("Continue", Command.SCREEN, 1);
setCommandListener(this);
}
void start()
{
thread = new Thread(this);
thread.start();
}
void stop()
{
thread = null;
}
public void run()
{
Thread currentThread = Thread.currentThread();
while (currentThread == thread)
{
if (startPressed)
{
updateTime();
// One could check isShown() here, to only repaint
// and service repaints when the canvas is shown,
// and not when it is hidden by some system screen.
repaint(panelX-2, labelY,
width - panelX - panelX + 5,
height - labelY - labelY + 4);
serviceRepaints();
}
try
{
synchronized(this)
{
// The split time is measured in 100ths of a second.
// 0.01 seconds is 10 milliseconds. The wait
// should therefore be at most a few milliseconds.
wait(1); // wait one millisecond
}
}
catch (InterruptedException e)
{
}
}
}
private void updateTime()
{
long elapsedTime = System.currentTimeMillis() - startTime +
remainingTime;
for (int i=3; i>=0; i--)
{
digit[i] = (int)(elapsedTime / base[i]);
elapsedTime %= base[i];
}
}
public void paint(Graphics g)
{
// Paint the entire canvas
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
drawFrame(g);
drawDigits(g);
}
private void drawDigits(Graphics g)
{
for (int i=0; i<4; i++)
{
// update both (2) digits for hh mm ss nn
for (int k=0; k < 2; k++)
{
int index = (k == 0) ? digit[i]%10 : digit[i]/10;
int fontType = i > 0 ? 0 : 1;
int digitX = width - panelX - (((i*2)+k+1)*digitWidth) -
((i*2)+k+1)/(2*DIGIT_INTERVAL) - (i*DOT_SIZE);
g.drawImage(digitImage[index][fontType], digitX, panelY,
Graphics.TOP|Graphics.LEFT);
}
// draw dots
if (i < 2)
{
int dotX = panelX + (i+1) * (2*digitWidth + DIGIT_INTERVAL) +
i*DOT_SIZE + 1;
g.fillRect(dotX, dotY, DOT_SIZE, DOT_SIZE);
}
}
}
private void drawFrame(Graphics g)
{
g.setColor(0, 0, 0);
g.drawImage(labelImage, panelX-2, labelY, Graphics.TOP|Graphics.LEFT);
g.drawRoundRect(panelX-2, panelY-4, width-panelX*2+4, digitHeight+8,
4, 4);
}
public void commandAction(Command c, Displayable d)
{
if ((c == startCommand) || (c == continueCommand))
{
startTime = System.currentTimeMillis();
removeCommand(startCommand);
removeCommand(continueCommand);
removeCommand(resetCommand);
addCommand(stopCommand);
remainingTime = 0;
for (int i=0; i<4; i++)
{
remainingTime += digit[i] * base[i];
}
setStartPressed(true);
}
else if (c == stopCommand)
{
// The accuracy of using stopCommand to time an event to
// 100ths of a second, might be affected by how commands are
// implemented in a MIDP device. For example, navigating a menu
// of commands to select the stop command takes time, which
// might affect the ability to time an event accurately. Therefore,
// a second stop mechanism is also provided, using the
// FIRE button (see method keyPressed).
handleStop();
}
else if (c == resetCommand)
{
remainingTime = 0;
removeCommand(resetCommand);
removeCommand(continueCommand);
addCommand(startCommand);
// update and repaint zeroed digits
for (int i=0; i<4; i++)
{
digit[i] = 0;
}
repaint(panelX-2, labelY,
width - panelX - panelX + 5,
height - labelY - labelY + 4);
serviceRepaints();
}
else
{
parent.quit();
}
}
private void handleStop()
{
setStartPressed(false);
removeCommand(stopCommand);
addCommand(continueCommand);
addCommand(resetCommand);
}
private synchronized void setStartPressed(boolean bool)
{
startPressed = bool;
}
static Image loadImage(String imageFile)
{
Image image = null;
try
{
image = Image.createImage(imageFile);
}
catch (Exception e)
{
// use null image
}
return image;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -