);
addCommand(myExitCommand);
setCommandListener(this);
myCanvas = canvas;
setItemStateListener(this);
myWidthGauge = new Gauge("每一列的宽度", true,
myCanvas.getMaxColWidth(),
myCanvas.getColWidth());
myColumnsGauge = new Gauge("迷宫的列数", false,
myCanvas.getMaxNumCols(),
myCanvas.getNumCols());
// Warning: the setLayout method does not exist in
// MIDP 1.4. If there is any chance that a target
// device will be using MIDP 1.4, comment out the
// following two lines:
//myWidthGauge.setLayout(Item.LAYOUT_CENTER);
//myColumnsGauge.setLayout(Item.LAYOUT_CENTER);
append(myWidthGauge);
append(myColumnsGauge);
}
//----------------------------------------------------------------
// implementation of ItemStateListener
/**
* Respond to the user changing the width.
*/
public void itemStateChanged(Item item) {
if(item == myWidthGauge) {
int val = myWidthGauge.getValue();
if(val < myCanvas.getMinColWidth()) {
myWidthGauge.setValue(myCanvas.getMinColWidth());
} else {
int numCols = myCanvas.setColWidth(val);
myColumnsGauge.setValue(numCols);
}
}
}
//----------------------------------------------------------------
// implementation of CommandListener
/*
* Respond to a command issued on this screen.
* (either reset or exit).
*/
public void commandAction(Command c, Displayable s) {
if(c == myExitCommand) {
myCanvas.newMaze();
}
}
}
/*
* MazeCanvas.java
*
* Created on 2005年12月2日, 下午1:05
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package com.j2medev.maze;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
/**
* This class is the display of the game.
*
* @author Carol Hamer
*/
class MazeCanvas extends javax.microedition.lcdui.Canvas {
//---------------------------------------------------------
// static fields
/**
* color constant
*/
public static final int BLACK = 0;
/**
* color constant
*/
public static final int WHITE = 0xffffff;
//---------------------------------------------------------
// instance fields
/**
* a handle to the display.
*/
private Display myDisplay;
/**
* The data object that describes the maze configuration.
*/
private Grid myGrid;
/**
* Whether or not the currently displayed maze has
* been completed.
*/
private boolean myGameOver = false;
/**
* maze dimension: the width of the maze walls.
*/
private int mySquareSize;
/**
* maze dimension: the maximum width possible for the maze walls.
*/
private int myMaxSquareSize;
/**
* maze dimension: the minimum width possible for the maze walls.
*/
private int myMinSquareSize;
/**
* top corner of the display: x-coordiate
*/
private int myStartX = 0;
/**
* top corner of the display: y-coordinate
*/
private int myStartY = 0;
/**
* how many rows the display is divided into.
*/
private int myGridHeight;
/**
* how many columns the display is divided into.
*/
private int myGridWidth;
/**
* the maximum number columns the display can be divided into.
*/
private int myMaxGridWidth;
/**
* the minimum number columns the display can be divided into.
*/
private int myMinGridWidth;
/**
* previous location of the player in the maze: x-coordiate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myOldX = 1;
/**
* previous location of the player in the maze: y-coordinate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myOldY = 1;
/**
* current location of the player in the maze: x-coordiate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myPlayerX = 1;
/**
* current location of the player in the maze: y-coordinate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myPlayerY = 1;
//-----------------------------------------------------
// gets / sets
/**
* Changes the width of the maze walls and calculates how
* this change affects the number of rows and columns
* the maze can have.
* @return the number of columns now that the the
* width of the columns has been updated.
*/
int setColWidth(int colWidth) {
if(colWidth < 2) {
mySquareSize = 2;
} else {
mySquareSize = colWidth;
}
myGridWidth = getWidth() / mySquareSize;
if(myGridWidth % 2 == 0) {
myGridWidth -= 1;
}
myGridHeight = getHeight() / mySquareSize;
if(myGridHeight % 2 == 0) {
myGridHeight -= 1;
}
myGrid = null;
return(myGridWidth);
}
/**
* @return the minimum width possible for the maze walls.
*/
int getMinColWidth() {
return(myMinSquareSize);
}
/**
* @return the maximum width possible for the maze walls.
*/
int getMaxColWidth