📄 drawindexmidlet.java
字号:
package draw;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class DrawIndexMidlet extends MIDlet {
private Display disDoor;
private DoorCanvas doorCanvas;
public DrawIndexMidlet() {
doorCanvas = new DoorCanvas();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
disDoor = Display.getDisplay(this);
disDoor.setCurrent(doorCanvas);
}
class DoorCanvas extends GameCanvas implements Runnable {
int imgNum = 9;
int xSpace = 0;
int ySpace = 0;
int xStep = 0;
int yStep = 0;
int xYellowRange = 0;
int yYellowRange = 0;
private Graphics g;
int windowWidth = 0;
int windowHeight = 0;
int index = 0;
int imgOneBackGroundWidth = 0;
int imgOneFrontHeight = 0;
int imgOneFrontWidth = 0;
int hImgSum = 0;
int wImgSum = 0;
Image imgBludBackground = null;
Image imgYellowBackground = null;
Image imgOneFrontArr[] = new Image[imgNum];
int xArr[] = new int[imgNum];
int yArr[] = new int[imgNum];
public void divideImage() {
try {
Image imgAllBackGround = Image
.createImage("/iconBackground.png");
int imgAllBackGroundWidth = imgAllBackGround.getWidth();
int imgAllBackGroundHeight = imgAllBackGround.getHeight();
imgOneBackGroundWidth = imgAllBackGroundWidth / 2;
int imgOneBackGroundHeight = imgAllBackGroundHeight;
imgBludBackground = Image.createImage(imgAllBackGround, 0, 0,
imgOneBackGroundWidth, imgOneBackGroundHeight,
Sprite.TRANS_NONE);
imgYellowBackground = Image.createImage(imgAllBackGround,
imgOneBackGroundWidth, 0, imgOneBackGroundWidth,
imgOneBackGroundHeight, Sprite.TRANS_NONE);
Image imgAllFront = Image.createImage("/iconFront.png");
int imgAllFrontWidth = imgAllFront.getWidth();
int imgAllFrontHeight = imgAllFront.getHeight();
imgOneFrontWidth = imgAllFrontWidth / imgNum;
imgOneFrontHeight = imgAllFrontHeight;
// 计算图片的行数和列数
int x = 0;
for (int i = 0; i < imgNum; i++) {
imgOneFrontArr[i] = Image.createImage(imgAllFront, x, 0,
imgOneFrontWidth, imgOneFrontHeight,
Sprite.TRANS_NONE);
x += imgOneFrontWidth;
}
wImgSum = windowWidth / imgOneFrontWidth;
if (wImgSum >= 3) {
wImgSum = 3;
} else {
wImgSum = 2;
}
double hSum = imgNum / wImgSum;
String strHImgSum = String.valueOf(hSum);
String subStr = strHImgSum.substring(2);
if ("0".equals(subStr)) {
hImgSum = imgNum / wImgSum;
} else {
hImgSum = imgNum / wImgSum + 1;
}
// 计算图片之间的空隙
xSpace = (windowWidth / wImgSum - imgOneFrontWidth) / 2;
ySpace = (windowHeight / hImgSum - imgOneFrontHeight) / 2;
// 计算步长
xStep = imgOneFrontWidth + 2 * xSpace;
yStep = imgOneFrontHeight + 2 * ySpace;
// 检验是否可以一屏显示所有项
if (ySpace < 0) {
wImgSum = 2;
double dimgNum2 = imgNum;
double dwImgSum2 = wImgSum;
double hSum2 = dimgNum2 / dwImgSum2;
String strHImgSum2 = String.valueOf(hSum2);
String subStr2 = strHImgSum2.substring(2);
if ("0".equals(subStr2)) {
hImgSum = imgNum / wImgSum;
} else {
hImgSum = imgNum / wImgSum + 1;
}
xSpace = (windowWidth / wImgSum - imgOneFrontWidth) / 2;
int hImg = windowHeight / imgOneFrontHeight;
ySpace = (windowHeight - imgOneFrontHeight * hImg)
/ (2 * hImg);
xStep = imgOneFrontWidth + 2 * xSpace;
yStep = imgOneFrontHeight + 2 * ySpace;
}
// 第一次画图片
int y2 = 0, x2 = 0;
for (int i = 0; i < hImgSum; i++) {
x2 += xSpace;
y2 += ySpace;
for (int j = 0; j < wImgSum; j++) {
xArr[index] = x2;
yArr[index] = y2;
if (index == 0) {
g.drawImage(imgYellowBackground, x2, y2,
Graphics.TOP | Graphics.LEFT);
} else {
g.drawImage(imgBludBackground, x2, y2, Graphics.TOP
| Graphics.LEFT);
}
g.drawImage(imgOneFrontArr[index], x2, y2, Graphics.TOP
| Graphics.LEFT);
index++;
x2 = x2 + imgOneFrontWidth + 2 * xSpace;
if (j == wImgSum - 1) {
x2 = 0;
}
if (index == imgNum) {
y2 = 0;
x2 = 0;
index = 0;
break;
}
}// end inner for
if (index == imgNum) {
y2 = 0;
x2 = 0;
index = 0;
break;
}
y2 = y2 + ySpace + imgOneFrontHeight;
}// end outer for
xYellowRange = xArr[imgNum - 1];
yYellowRange = yArr[imgNum - 1];
} catch (Exception e) {
e.printStackTrace();
}
}
// ***************************************
// ********* wImgSum == 3 ****************
// ***************************************
// key be pressed to draw yellow background x,y
// when wImgSum == 3
int y3 = 0, x3 = 0;
// key be pressed to draw blue background x,y
// when wImgSum == 3
int y4 = 0, x4 = 0;
public void rePaintDoorW3() {
x4 = 0;
y4 = 0;
g.setColor(255, 255, 255);
g.fillRect(0, 0, windowWidth, windowHeight);
for (int i = 0; i < hImgSum; i++) {
x4 += xSpace;
y4 += ySpace;
for (int j = 0; j < wImgSum; j++) {
if (x4 == x3 & y4 == y3) {
g.drawImage(imgYellowBackground, x3, y3, Graphics.TOP
| Graphics.LEFT);
} else {
g.drawImage(imgBludBackground, x4, y4, Graphics.TOP
| Graphics.LEFT);
}
g.drawImage(imgOneFrontArr[index], x4, y4, Graphics.TOP
| Graphics.LEFT);
index++;
x4 = x4 + imgOneFrontWidth + 2 * xSpace;
if (j == wImgSum - 1) {
x4 = 0;
}
if (index == imgNum) {
x4 = xSpace;
y4 = ySpace;
index = 0;
break;
}
}// end inner for
if (index == imgNum) {
x4 = xSpace;
y4 = ySpace;
index = 0;
break;
}
y4 = y4 + ySpace + imgOneFrontHeight;
}// end outer for
}// rePaintDoorW3
// ***************************************
// ************ wImgSum == 2 ***************
// ***************************************
// key be pressed to draw yellow background x,y
// when wImgSum == 3
int y5 = 0, x5 = 0;
// key be pressed to draw blue background x,y
// when wImgSum == 3
int y6 = 0, x6 = 0;
int firstImgIndex = 0;
public void rePaintDoorW2() {
x6 = 0;
y6 = 0;
g.setColor(255, 255, 255);
g.fillRect(0, 0, windowWidth, windowHeight);
this.flushGraphics();
firstImgIndex = index;
int tempIndex = 0;
tempIndex = index + 4;
for (int j = index, i = 0; i < 4; j++, i++) {
if (xArr[i] == x5 && yArr[i] == y5) {
g.drawImage(imgYellowBackground, x5, y5, Graphics.TOP
| Graphics.LEFT);
} else {
g.drawImage(imgBludBackground, xArr[i], yArr[i],
Graphics.TOP | Graphics.LEFT);
}
g.drawImage(imgOneFrontArr[j], xArr[i], yArr[i], Graphics.TOP
| Graphics.LEFT);
index++;
x6 = x6 + imgOneFrontWidth + 2 * xSpace;
if (j == wImgSum - 1) {
x6 = 0;
}
if (tempIndex == index || index == imgNum) {
x6 = xSpace;
y6 = ySpace;
break;
}
}// end for
y6 = y6 + ySpace + imgOneFrontHeight;
index = firstImgIndex;
this.flushGraphics();
}// rePaintDoorW2
// DOWN_PRESSED
int isFirstScreen = 0;// 判断是否是第一屏 [0][1][2][3]
// UP_PRESSED
int isLastScreen = 0;// 判断是否是第一屏 [8][7][6]
public void run() {
/*
*
* wImgSum == 2
*
*/
if (wImgSum == 2) {
try {
x5 = xSpace;
y5 = ySpace;
while (true) {
int keyStates = getKeyStates();
if ((keyStates & this.LEFT_PRESSED) != 0) {
keyStates = -1;
x5 -= xStep;
if (x5 > 0) {
rePaintDoorW2();
} else {
x5 += xStep;
rePaintDoorW2();
}
Thread.sleep(300);
}
else if ((keyStates & this.RIGHT_PRESSED) != 0) {
keyStates = -1;
x5 += xStep;
if (x5 <= xArr[3]) {
rePaintDoorW2();
} else {
x5 -= xStep;
rePaintDoorW2();
}
Thread.sleep(300);
}
else if ((keyStates & this.UP_PRESSED) != 0) {
keyStates = -1;
if(y5==yArr[3]){
y5 -= yStep;
}else{
if (index - 2 >= 0) {
index -= 2;
}
}
if (y5 > 0) {
rePaintDoorW2();
} else {
y5 += yStep;
rePaintDoorW2();
}
if(index == 0){
isFirstScreen = 0;
}
Thread.sleep(300);
}
else if ((keyStates & this.DOWN_PRESSED) != 0) {
keyStates = -1;
if (isFirstScreen == 0) {
isFirstScreen = 1;
} else {
if (index + 2 < imgNum - 1) {
index += 2;
if (index == 6) {
if(x5>xStep){
x5 -= xStep;
}
isLastScreen = 1;
}
}
}
y5 += yStep;
System.out
.println("DOWN_PRESSED: index = " + index);
System.out.println("yStep = " + yStep
+ " DOWN_PRESSED : x5 = " + x5
+ " ---- y5 = " + y5);
if (y5 <= yArr[3]) {
rePaintDoorW2();
} else {
y5 -= yStep;
rePaintDoorW2();
}
Thread.sleep(300);
}// end else if
}// end while
} catch (Exception e) {
e.printStackTrace();
}
}// end if wImgSum==2
/*
*
* wImgSum == 3
*
*/
if (wImgSum == 3) {
x3 = xSpace;
y3 = ySpace;
try {
while (true) {
int keyStates = getKeyStates();
if ((keyStates & this.LEFT_PRESSED) != 0) {
System.out.println("LEFT_PRESSED: x3 = " + x3
+ " ---- y3 = " + y3);
keyStates = -1;
x3 -= xStep;
if (x3 >= 0) {
g.setColor(255, 255, 255);
this.flushGraphics();
rePaintDoorW3();
this.flushGraphics();
} else {
x3 = xSpace;
}
Thread.sleep(300);
}
else if ((keyStates & this.RIGHT_PRESSED) != 0) {
keyStates = -1;
x3 += xStep;
System.out.println("xStep = " + xStep
+ " RIGHT_PRESSED : x3 = " + x3
+ " ---- y3 = " + y3);
if (x3 < windowWidth) {
if (!((x3 >= xYellowRange) && (y3 >= yYellowRange))) {
System.out
.println("right img in window===============");
g.setColor(255, 255, 255);
this.flushGraphics();
rePaintDoorW3();
this.flushGraphics();
} else {
System.out
.println("right img out of window===============");
x3 = xYellowRange;
y3 = yYellowRange;
g.setColor(255, 255, 255);
this.flushGraphics();
rePaintDoorW3();
this.flushGraphics();
}
} else {
x3 -= xStep;
}
Thread.sleep(300);
}
else if ((keyStates & this.UP_PRESSED) != 0) {
System.out.println("UP_PRESSED: x3 = " + x3
+ " ---- y3 = " + y3);
keyStates = -1;
y3 -= yStep;
if (y3 >= 0) {
g.setColor(255, 255, 255);
this.flushGraphics();
rePaintDoorW3();
this.flushGraphics();
} else {
y3 = ySpace;
}
Thread.sleep(300);
}
else if ((keyStates & this.DOWN_PRESSED) != 0) {
keyStates = -1;
y3 += yStep;
System.out.println("DOWN_PRESSED : x3 = " + x3
+ " ---- y3 = " + y3);
if (y3 < imgOneFrontHeight * hImgSum) {
if (!((x3 >= xYellowRange) && (y3 >= yYellowRange))) {
System.out.println("down img inner");
g.setColor(255, 255, 255);
this.flushGraphics();
rePaintDoorW3();
this.flushGraphics();
} else {
System.out.println("down img outer");
x3 = xYellowRange;
y3 = yYellowRange;
g.setColor(255, 255, 255);
this.flushGraphics();
rePaintDoorW3();
this.flushGraphics();
}
} else {
y3 -= yStep;
}
Thread.sleep(300);
}
}// end while
} catch (InterruptedException e) {
e.printStackTrace();
}
}// end if wImgSum==3
}
public DoorCanvas() {
super(true);
setFullScreenMode(true);// 实现全屏
g = this.getGraphics();
windowWidth = this.getWidth();
windowHeight = this.getHeight();
System.out.println(windowWidth + "----" + windowWidth);
divideImage();
Thread threadChangeBackground = new Thread(this);
threadChangeBackground.start();
}
}// end inner class DoorCanvas
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -