📄 spritetest.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
/**
* A example that demonstrates the use of the ImageSet class to create a sprite.
* Note that this is used as a precursor to creating a Sprite class, not as an
* example of how to use the Sprite class.
* @author Martin J. Wells
*/
public class SpriteTest extends MIDlet implements CommandListener, Runnable
{
private static int SHIP_FRAME_WIDTH = 16;
private static int SHIP_FRAME_HEIGHT = 16;
private MyCanvas myCanvas;
private Command quit;
private Image[] shipFrames;
private boolean running;
private int currentFrame;
/**
* A custom canvas that will draw the current frame in the animation.
*/
class MyCanvas extends Canvas
{
/**
* The overidden Canvas class paint method takes care of drawing the
* current frame.
* @param graphics The graphics context on which you draw.
*/
protected void paint(Graphics graphics)
{
graphics.setColor(0);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.drawImage(shipFrames[currentFrame], getWidth() / 2,
getHeight() / 2,
Graphics.HCENTER | Graphics.VCENTER);
}
}
/**
* Constructor loads the ship.png file (make sure this image file is in the
* JAR (and not in a subdirectory) for this code to work. After loading the
* image we extract the frames, create an image set then setup the canvas.
*/
public SpriteTest()
{
try
{
// Construct the image and extract the red ship frames
Image shipImage = Image.createImage("/ship.png");
shipFrames = ImageSet.extractFrames(shipImage, 4 * SHIP_FRAME_WIDTH,
0, 4, 4, SHIP_FRAME_WIDTH,
SHIP_FRAME_HEIGHT);
}
catch (IOException ioe)
{
System.out.println("unable to load image");
}
// Construct a the canvas
myCanvas = new MyCanvas();
// And a way to quit
quit = new Command("Quit", Command.EXIT, 2);
myCanvas.addCommand(quit);
myCanvas.setCommandListener(this);
// create the thread that will carry out the animation.
running = true;
Thread t = new Thread(this);
t.start();
}
public void run()
{
while (running)
{
// Increment the current frame. Note that this is not a particular
// good way to do animation. See the Sprite class for how to handle it
// properly (using timing).
currentFrame++;
if (currentFrame > 15)
currentFrame = 0;
// Request a canvas repaint.
myCanvas.repaint();
try
{
// Hang around a bit.
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
}
}
/**
* Handles Application Manager notification the MIDlet is starting (or
* resuning from a pause). In this case we set the canvas as the current
* display screen.
* @throws MIDletStateChangeException
*/
protected void startApp() throws MIDletStateChangeException
{
Display.getDisplay(this).setCurrent(myCanvas);
}
/**
* Handles Application Manager notification the MIDlet is about to be paused.
* We don't bother doing anything for this case.
*/
protected void pauseApp()
{
}
/**
* Handles Application Manager notification the MIDlet is about to be
* destroyed. We don't bother doing anything for this case.
*/
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
}
/**
* The CommandListener interface method called when the user executes
* a Command, in this case it can only be the quit command we created in the
* consutructor and added to the Canvas.
* @param command The command that was executed.
* @param displayable The displayable that command was embedded within.
*/
public void commandAction(Command command, Displayable displayable)
{
try
{
if (command == quit)
{
running = false;
destroyApp(true);
notifyDestroyed();
}
}
catch (MIDletStateChangeException me)
{
System.out.println(me + " caught.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -