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

📄 gamemidlet.java

📁 Programming java 2 micro edition on symbian os 一书中作者写的使用了MMAPI的小拼图游戏
💻 JAVA
字号:
/*
 * Copyright 2003, 2004 Symbian Ltd.
 * For License terms see http://www.symbian.com/developer/techlib/codelicense.html
 */

package picturepuzzle;
import javax.microedition.midlet.MIDlet ;
import javax.microedition.lcdui.* ;
import java.io.* ;

public class GameMIDlet extends MIDlet {
    
    private Display display;
    private ChoiceForm choiceForm;
    private CaptureCanvas captureCanvas;
    private PuzzleCanvas puzzleCanvas;
    private Capturer capturer;
    private RMSHandler rms;
    
    public GameMIDlet() {
        rms = new RMSHandler();
        display = Display.getDisplay(this);
        choiceForm = new ChoiceForm(this);
    }
    
    public void startApp() {
        Displayable current = display.getCurrent();
        try{
            rms.openRecordStore();
        }catch(ApplicationException ae){
            showAlert(ae);
        }
        
        if (current == null) {
            // first call
            displayChoiceForm();
        }else {
            //called after a pause
            display.setCurrent(current);
            //player will have been discarded so recreate.
            try{
                capturer.createPlayer();
            }catch(ApplicationException ae) {
                showAlert(ae);
            }
        }
    }
    
    public void pauseApp() {
        try{
            rms.closeRecordStore();
        }catch(ApplicationException ae){}
        if(capturer != null){
            capturer.discardPlayer();
        }
    }
    
    public void destroyApp(boolean unconditional) {
        try{
            rms.closeRecordStore();
        }catch(ApplicationException ae){}
        if(capturer != null){
            capturer.discardPlayer();
        }
    }
    
    public void displayChoiceForm() {
        try {
            String [] imageNames = loadImageNames();
            choiceForm.setImageNames(imageNames);
        }catch(ApplicationException ae) {
            showAlert(ae);
        }
        display.setCurrent(choiceForm);
    }
    
    public void displayHintCanvas(Image image){
        HintCanvas hintCanvas = new HintCanvas(image);
        display.setCurrent(hintCanvas);
        try {
            Thread.sleep(1500);
        }catch (InterruptedException ie) {}
        display.setCurrent(puzzleCanvas);
    }
    
    public void displayCaptureCanvas() {
        if (captureCanvas == null) {
            //create CaptureCanvas and associated player (Capturer)
            captureCanvas = new CaptureCanvas(this);
            try{
                capturer = new Capturer(this, captureCanvas);
                capturer.startPlayer();
                display.setCurrent(captureCanvas);
            }catch(final ApplicationException ae){
                captureCanvas = null;//set to null if unable to create associated player
                showAlert(ae);
            }
        } else {
            //CaptureCanvas and associated player (Capturer) already exist
            display.setCurrent(captureCanvas);
        }
    }
    
    public void displayPuzzleCanvas(byte[] imageData) {
        Image image = Image.createImage(imageData, 0, imageData.length);
        puzzleCanvas = new PuzzleCanvas(this, image);
        display.setCurrent(puzzleCanvas);
    }
    
    public void takePhoto(){
        try {
            byte[] data = capturer.takeSnapshot();
            ImageNameBox imageNameBox = new ImageNameBox(this, data);
            display.setCurrent(imageNameBox);
        }catch(final ApplicationException ae) {
            showAlert(ae);
        }
    }
    
    public void loadAndDisplayImage(String imageName){
        try{
            byte[] imageData = rms.retrieveImageFromStore(imageName);
            displayPuzzleCanvas(imageData);
        }catch(ApplicationException ae){
            showAlert(ae);
        }
    }
    
    public String[] loadImageNames() throws ApplicationException {
        String[] images = rms.retrieveStoredImageNames();
        return images;
    }
    
    public void saveImage(String imageName, byte[] data) {
        try {
            rms.addImageToStore(imageName, data);
        }catch(ApplicationException ae) {
            showAlert(ae);
        }
    }
    
    public void deleteImage(String imageName) {
        try{
            rms.deleteImageFromStore(imageName);
        }catch(ApplicationException ae){
            showAlert(ae);
        }
    }
    
    public void showAlert(final ApplicationException ae){
        new Thread() {
            public void run() {
                Alert alert = new Alert(ae.getExceptionType(), ae.getMessage(), null, AlertType.ERROR);
                alert.setTimeout(1500);
                display.setCurrent(alert);
            }
        }.start();
    }
    
    public void exit(){
        try{
            rms.closeRecordStore();
        }catch(ApplicationException ae){}
        if(capturer != null){
            capturer.discardPlayer();
        }
        notifyDestroyed();
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -