?? demointro.java
字號:
/*==============================================================================
FILE: DemoIntro.java
GENERAL DESCRIPTION:
MIDlet to display introduction for demonstration menu MIDlet.
Copyright (c) 2002 Qualcomm Inc. All rights reserved.
==============================================================================*/
package com.qualcomm.demo.menu;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.util.*;
import com.sun.midp.midlet.Scheduler;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import com.qualcomm.midp.Main;
/**
* DemoIntro MIDlet.
*/
public class DemoIntro extends MIDlet implements CommandListener
{
// MIDlet to instantiate when this MIDlet is done
private final String MENU_MIDLET = "com.qualcomm.demo.menu.DemoMenu";
// Number of milliseconds to animate and to wait before going to next MIDlet
private final int ANIMATION_TIME = 5000;
private final int TOTAL_TIME = 8000;
// Number of milliseconds for each animation step
private final int ANIMATION_STEP_TIME = 30;
// Horizontal offset of final location of first and second images
// ("Qualcomm" and "Presents")
private final int IMAGE_OFFSET_X1 = -20;
private final int IMAGE_OFFSET_X2 = 0;
// Vertical space between final location of first two and second two images
// (after "Qualcomm" and "Presents")
private final int IMAGE_SPACING_Y1 = 5;
private final int IMAGE_SPACING_Y2 = 15;
// Maximum value for animation stepping
private final int MAX_ANIMATION_STEP = 10;
// Random number generator
private Random random = new Random();
// Animation step number (counts down to 0)
private int animationStep;
// Timer tasks to animate screen and wait for demo to finish
TimerTask animatorTask;
TimerTask waiterTask;
// Display for this MIDlet
private Display display;
private Player introMusic = null;
private Control volControl = null;
// Softkeys for main screen
private final Command skipCommand =
new Command("Skip", Command.SCREEN, 1);
private final Command exitCommand =
new Command("Exit", Command.EXIT, 1);
// Main screen and initialization code
private final Canvas screen = new Canvas()
{
// Height of all 3 images plus space in between
// (assume screen is big enough for this)
private final int totalImageHeight =
qualcommImg.getHeight() + IMAGE_SPACING_Y1 +
presentsImg.getHeight() + IMAGE_SPACING_Y1 +
javaImg.getHeight();
// Image display final X-positions
private final int qualcommX =
((getWidth() - qualcommImg.getWidth()) / 2) + IMAGE_OFFSET_X1;
private final int presentsX =
((getWidth() - presentsImg.getWidth()) / 2) + IMAGE_OFFSET_X2;
private final int javaX =
(getWidth() - javaImg.getWidth()) / 2;
// Image display final Y-positions
private final int qualcommY =
(getHeight() - totalImageHeight) / 2;
private final int presentsY =
qualcommY + qualcommImg.getHeight() + IMAGE_SPACING_Y1;
private final int javaY =
presentsY + presentsImg.getHeight() + IMAGE_SPACING_Y2;
// Images with final positions
private final MovingImage qualcomm =
new MovingImage(qualcommImg, qualcommX, qualcommY);
private final MovingImage presents =
new MovingImage(presentsImg, presentsX, presentsY);
private final MovingImage java =
new MovingImage(javaImg, javaX, javaY);
// Class to represent moving image
class MovingImage
{
// Image and final position
private Image image;
private int finalX;
private int finalY;
// Animation step values
private int stepX = randomAnimationStep();
private int stepY = randomAnimationStep();
// Constructor
MovingImage(Image image, int finalX, int finalY)
{
this.image = image;
this.finalX = finalX;
this.finalY = finalY;
}
// Returns random animation step value
private int randomAnimationStep()
{
// Get random number between 1 and MAX_ANIMATION_STEP (inclusive)
int i = ((random.nextInt() & 0x7fffffff) % MAX_ANIMATION_STEP) + 1;
// Return positive or negative number
return ((random.nextInt() >= 0) ? i : -i);
}
// Converts a virtual X or Y position (which is where the image would
// be if the movement wasn't confined to the screen) to an actual X or
// Y position, given the effective X or Y screen dimension (which is
// the actual X or Y screen dimension minus the X or Y image dimension)
private int convertPos(int pos, int dim)
{
// Convert position to equivalent number between 0 and (dim * 2)
if (pos < 0)
{
pos = -pos;
}
pos %= (dim * 2);
// If position between dim and (dim * 2), convert to go from other
// direction
if (pos >= dim)
{
pos = (dim * 2) - 1 - pos;
}
return pos;
}
// Draws the image in the position specified by the given step
public void draw(Graphics g, int step)
{
int x = convertPos(finalX - (step * stepX),
(getWidth() - image.getWidth()));
int y = convertPos(finalY - (step * stepY),
(getHeight() - image.getHeight()));
g.drawImage(image, x, y, Graphics.TOP | Graphics.LEFT);
}
}
// Paints the screen
protected void paint(Graphics g)
{
// Clear screen to white
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
// Draw first two images ("qualcomm" will go on top if overlapping)
presents.draw(g, animationStep);
qualcomm.draw(g, animationStep);
// Draw "java" image only when animation complete
if (animationStep == 0)
{
java.draw(g, 0);
}
}
};
{
// Configure softkeys
screen.addCommand(skipCommand);
screen.addCommand(exitCommand);
screen.setCommandListener(this);
}
// Schedules menu MIDlet in place of this MIDlet
private void scheduleMenuMidlet()
{
// Instead of instantiating the menu MIDlet while demoIntro is
// still active, we will restart KVM to run menu MIDlet. This
// can prevent the case that when garbage collection needs to be
// kicked in to reclaim free memory for the newly-instantiated
// MIDlet and at the same time an asynchronous funciton is active.
// This would cause the applicaiton to hang.
Main.registerStartApp(MENU_MIDLET, null);
/*
// Exit this app
// Try to instantiate new midlet
try
{
Scheduler.getScheduler().scheduleMIDlet(
(MIDlet)(Class.forName(MENU_MIDLET).newInstance()));
}
catch (Exception e)
{
// Create an error message
String errorString =
"Could not instantiate " + MENU_MIDLET + "\n" + e.toString();
// Create an "error" screen containing error message
Form errorScreen = new Form("Fatal Error");
errorScreen.append(errorString);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -