libro
www.tuyano.com
iアプリ・プログラミング入門

スクラッチパッドを利用しよう (3/6)

作成:2009-12-30 10:14
更新:2009-12-30 10:14

■ソースコードの作成

では、ソースコードを作成しましょう。SampleIapp.javaを開き、MainCanvasクラスを以下のように修正してください。

iアプリを実行したら、上下左右のキーで赤い枠を移動し、選択キーで表示を設定していきます。そして右ソフトキーを押してiアプリを終了させてください。終了時にデータをスクラッチパッドに保存します。もう一度iアプリを起動してみると、保存されたスクラッチパッドからデータを読み込み、最後に保存した状態に復帰して表示されます。

※プログラムリストが表示されない場合

AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。

●プログラム・リスト●

※MainCanvasの修正
// import java.io.*;
// import javax.microedition.io.Connector;
// import com.nttdocomo.io.ConnectionException;

class MainCanvas extends Canvas {
    private Image[] images;
    private static final int MAX_X = 5, MAX_Y = 5;
    private static final int IMG_W = 32, IMG_H = 32;
    private int x, y;
    private int[] data;
    private ImageMap map;

    MainCanvas() {
        this.setSoftLabel(Frame.SOFT_KEY_2, "EXIT");
        data = new int[MAX_X * MAX_Y];
        images = new Image[3];
        try {
            MediaImage mi = MediaManager.getImage("resource:///image.gif");
            mi.use();
            images[0] = mi.getImage();
            MediaImage mi1 = MediaManager.getImage("resource:///image2.gif");
            mi1.use();
            images[1] = mi1.getImage();
            MediaImage mi2 = MediaManager.getImage("resource:///image3.gif");
            mi2.use();
            images[2] = mi2.getImage();
            map = new ImageMap(IMG_W, IMG_H, MAX_X, MAX_Y, data, images);
            load();
        } catch (ConnectionException e) {
        }
    }

    public void paint(Graphics g) {
        g.lock();
        g.clearRect(0, 0, this.getWidth(), this.getHeight());
        g.drawImageMap(map, 0, 0);
        g.setColor(Graphics.getColorOfName(Graphics.RED));
        g.drawRect(IMG_W * x, IMG_H * y, IMG_W, IMG_H);
        g.unlock(true);
    }

    public void processEvent(int type, int param) {
        super.processEvent(type, param);
        switch (type) {
        case Display.KEY_PRESSED_EVENT:
            switch (param) {
            case Display.KEY_UP:
                y -= y == 0 ? 0 : 1;
                break;
            case Display.KEY_DOWN:
                y += y == MAX_Y - 1 ? 0 : 1;
                break;
            case Display.KEY_LEFT:
                x -= x == 0 ? 0 : 1;
                break;
            case Display.KEY_RIGHT:
                x += x == MAX_X - 1 ? 0 : 1;
                break;
            case Display.KEY_SELECT:
                data[x + y * MAX_X] = (data[x + y * MAX_X] + 1) % images.length;
                break;
            case Display.KEY_SOFT2:
                save();
                IApplication.getCurrentApp().terminate();
                break;
            }
            repaint();
        }
    }

    // データの書き出し
    public void save() {
        OutputStream output = null;
        try {
            output = Connector.openOutputStream("scratchpad:///0;pos=0");
            for (int i = 0; i < data.length; i++)
                output.write(data[i]);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // データの読み込み
    public void load() {
        InputStream input = null;
        try {
            input = Connector.openInputStream("scratchpad:///0;pos=0");
            for (int i = 0; i < data.length; i++)
                data[i] = input.read();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

※関連コンテンツ

「iアプリ・プログラミング入門」に戻る