📄 gobangcanvas.java
字号:
package com.tianxia.qipai.view.game.gobang;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import com.tianxia.qipai.control.game.gobang.GobangControl;
import com.tianxia.qipai.event.GobangEvent;
import com.tianxia.qipai.event.MainGameEvent;
import com.tianxia.qipai.model.game.gobang.GobangDeal;
import com.tianxia.qipai.model.game.gobang.GobangTimer;
import com.tianxia.qipai.model.game.gobang.MouseRect;
import com.tianxia.qipai.model.game.gobang.MouseSprite;
import com.tianxia.qipai.model.game.gobang.SelectSprite;
import com.tianxia.qipai.model.game.gobang.TimeAdd;
import com.tianxia.qipai.model.util.CommStructNode;
import com.tianxia.qipai.model.util.GameEventDeal;
import com.tianxia.qipai.view.util.ChatControl;
import com.tianxia.qipai.view.util.ConfirmControl;
public class GobangCanvas extends GameCanvas implements GameEventDeal,TimeAdd{
/*************************************************************************************************
//各种图层单元大小和格数
* **********************************************************************************************/
private final static int QIZI_WIDTH = 9;//10; //棋子宽度 10px
private final static int QIZI_HEIGHT = 9;//10; //棋子高度 10px
private final static int QIPAN_WIDTH = 135;//150; //棋盘宽度
private final static int QIPAN_HEIGHT = 135;//150;//棋盘高度
private final static int QIZIWIDTH_IN_TILES = 15; //棋子图层宽度(以单元宽度计算)
private final static int QIZIHEIGHT_IN_TILES = 15; //棋子图层高度(以单元宽度计算)
private final static int BGTILE_WIDTH = 89; //背景 单元宽度 89px
private final static int BGTILE_HEIGHT = 55; //背景 单元高度 55px
private final static int BGWIDTH_IN_TILES = 2; //背景 游戏域宽度(以单元宽度计算) 16 .. N
private final static int BGHEIGHT_IN_TILES = 4; //背景 游戏域高度(以单元高度计算)
private final static int HAND_WIDTH = 20; //指针图片单元宽度
private final static int HAND_HEIGHT = 16; //指针图片单元高度
private final static int OPERATE_TILE_WIDTH = 27; //操作菜单的单元格宽度
private final static int OPERATE_TILE_HEIGHT = 18; //操作菜单的单元格宽度
private final static int OPERATE_WIDTH_TILES = 4; //功能操作菜单的横向和纵向单元格数
private final static int OPERATE_HEIGHT_TILES = 1;
private final static int MENU_RECT_WIDTH = 54; //菜单单元格图层的大小
private final static int MENU_RECT_HEIGHT = 18;
private final static int END_WIDTH = 20; //游戏结束单元图宽度
private final static int END_HEIGHT = 19; //游戏结束单元高度
private final static int COLOR_WIDTH = 21; // 棋子颜色显示图片的宽度
private final static int COLOR_HEIGHT = 21; // 棋子颜色显示图片的高度
public static int mainWidth; //屏幕宽度
public static int mainHeight; //屏幕高度
private Font chatfont = Font.getFont(0,0,Font.SIZE_SMALL);
//初始化为不使用窗口区域视野
private boolean userViewWindow = false;
/*************************************************************************************************
//各种图层和显示元件
* **********************************************************************************************/
//各种图片
private Image numimage; //数字图形(供显示时间用)
private LayerManager lm;
private Graphics g;
private static GobangCanvas instance = null;
private GobangControl gobangcontrol;
private Display display;
private TiledLayer qizilayer,timelayer,opptimelayer; //棋子图层、棋盘图层
private TiledLayer operatelayer; //操作选项图层
private TiledLayer graylayer; //操作选项置灰图层
private Sprite userheadsprite,oppheadsprite; //玩家、和对家图象精灵
private SelectSprite selectsprite;
private SelectSprite presprite; //上一步棋选种框精灵
private MouseSprite mousesprite; //棋盘外的指针精灵
private Sprite waitoppsprite; //等待对家响应精灵
private Sprite oppinfosprite,userinfosprite; //用户和对家的信息图层
private Sprite whitesprite,blacksprite; //棋子颜色精灵
private Sprite helpsprite; //帮助内容精灵
private Sprite waitsprite; //等待其他用户开始提示精灵
private TiledLayer gameendlayer; //游戏结束图层
private ConfirmControl agreencontrol; //确认选择框
private ConfirmControl promptcontrol; //确认提示框
private MenuControl menucontrol; //菜单控件
private ChatControl chatcontrol; //聊天控件
private MouseRect chatrect,operaterect,inforect,qipanrect; //聊天区域,菜单区域,用户信息区域,棋盘
/*************************************************************************************************
//各种图层的初始位置
* **********************************************************************************************/
private final static int ORIGIN_X = 0;//layermanager相对与屏幕窗口的原点位置
private final static int ORIGIN_Y = 0;
private final static int QIZI_X = 0; //棋子图层初始位置
private final static int QIZI_Y = 25;//30;
private final static int QIPAN_X = 0; //棋盘图层初始位置
private final static int QIPAN_Y = 25;//30;
private final static int SELECT_FIR_X = 0; //选择框的初始参考位置
private final static int SELECT_FIR_Y = 25;//30;
private final static int PRE_FIR_X = 0; //前一步棋标志框的初始参考位置
private final static int PRE_FIR_Y = 25;//30;
private final static int OPERATE_X = 0; //操作菜单的位置
private final static int OPERATE_Y = 160;//180;
private final static int CHAT_ORIGIN_X = 0; //聊天框控件的初始位置
private final static int CHAT_ORIGIN_Y = 0;
private final static int USER_ORIGIN_X = 145;//160; //用户信息的位置的各种初始数据
private final static int USER_ORIGIN_Y = 150;//175;
private final static int USER_TIME_X = USER_ORIGIN_X - 10; //用户计时图层的位置
private final static int USER_TIME_Y = USER_ORIGIN_Y - 20;
private final static int USER_COLOR_X = USER_ORIGIN_X; //用户棋子颜色图层的位置
private final static int USER_COLOR_Y = USER_ORIGIN_Y - 40;
private final static int USER_INFO_X = USER_ORIGIN_X -130; //用户信息精灵的图层位置
private final static int USER_INFO_Y = USER_ORIGIN_Y -85;
private final static int OPP_ORIGIN_X = 145;//160; //对家信息的位置的各种初始数据
private final static int OPP_ORIGIN_Y = 30;//25;
private final static int OPP_TIME_X = OPP_ORIGIN_X - 10; //对家计时图层的位置
private final static int OPP_TIME_Y = OPP_ORIGIN_Y + 20;
private final static int OPP_COLOR_X = OPP_ORIGIN_X; //对家棋子颜色图层位置
private final static int OPP_COLOR_Y = OPP_ORIGIN_Y + 40;
private final static int OPP_INFO_X = OPP_ORIGIN_X -130; //对家信息精灵图层位置
private final static int OPP_INFO_Y = OPP_ORIGIN_Y ;
private final static int HELP_INFO_X = 10; //帮助精灵的显示位置
private final static int HELP_INFO_Y = 20;
private final static int WAIT_START_X = 25; //等待其他用户开始提示框的位置
private final static int WAIT_START_Y = 90;
private final static int WAIT_OPPRESPOND_X = 20; //等待其他用户响应提示框的位置
private final static int WAIT_OPPRESPOND_Y = 90;
private final static int AGREEN_X = 30; //询问框的位置
private final static int AGREEN_Y = 30;
private final static int END_ORIGIN_X = QIPAN_X+QIPAN_WIDTH/2; //游戏结束提示图层的显示位置
private final static int END_ORIGIN_Y = QIPAN_Y+QIPAN_HEIGHT/2;
/*************************************************************************************************
//各种图片和显示控件的宽度和高度
* **********************************************************************************************/
public final static int BG_WIDTH = BGWIDTH_IN_TILES * BGTILE_WIDTH; //背景图层的宽度
public final static int BG_HEIGHT = BGHEIGHT_IN_TILES * BGTILE_HEIGHT; //背景图层的高度
private final static int OPERATE_WIDTH = 135;//150; //操作菜单区域的宽度和高度
private final static int OPERATE_HEIGHT = 40;
private final static int HELP_WIDTH = 130; //帮助信息精灵的宽度和高度
private final static int HELP_HEIGHT = 100;
private final static int CHATCONTROL_WIDTH = 176; // 聊天框控件的宽度和高度
private final static int CHATCONTROL_HEIGHT = 25;
private final static int AGREEN_WIDTH = 100; //询问框的宽度和高度
private final static int AGREEN_HEIGHT = 60;
private final static int PROMPT_WIDTH = 100; //提示确认框的宽度和高度
private final static int PROMPT_HEIGHT = 50;
private final static int INFO_IMAGE_WIDTH = 130; //用户信息显示框宽度和高度
private final static int INFO_IMAGE_HEIGHT = 90;
private final static int TIME_TILE_WIDTH = 11; //时间单元图片的宽度和高度
private final static int TIME_TILE_HEIGHT = 15;
private final static int WAIT_WIDTH = 100; //等待其他玩家开始提示框的宽和高
private final static int WAIT_HEIGHT = 20;
/*************************************************************************************************
//各种状态参数
* **********************************************************************************************/
//游戏状态
private final static int USERSIT = 0; //用户刚坐下
private final static int USERREADY = 1;//用户准备好
private final static int USERFALL = 2; //轮到用户走子
private final static int OPPFALL = 3; //轮到对手走子
//用户的状态
private final static String USERSTANDSTR = "00";
private final static String USERSITSTR ="01";
private final static String USERREADYSTR = "02";
private final static String USERSTARTSTR = "03";
//聊天框控件各个参数的配置
private final static int CHAT_MAX_ROW = 1; //聊天框控件显示的行数
private final static int CHAT_ARRY_NUM = 5; //聊天数组的记录数
private final static int CHAT_MAX_NUM = 10; //聊天框控件的每行显示的字数
// 信息区的各个标志
private final static int CHAT_RECT_FLAG = 0;
private final static int OPERATE_RECT_FLAG = 2;
private final static int INFO_RECT_FLAG = 3;
private final static int QIPAN_RECT_FLAG = 1;
//游戏菜单的各种状态
private final static int STATENUM=4; //游戏菜单的子项数
private final static int EXITMENUM = 0;
private final static int STANDUP = 1;
private final static int HELP_SHOW = 2;
private final static int EXIT_GAME = 3;
//棋子颜色
private final static int BLACK_COLOR = 1;
private final static int WHITE_COLOR = 2;
//选择确认框的标志
private final static int REGRET = 0; //悔棋
private final static int QUESTTIE = 1;///求和
//确认框控件的类型
private final static int CONFIRMTYPE =1; //确认框类型(同意、拒绝)
private final static int PROMPTTYPE = 3; //提示框类型
//确认框的参数配置
private final static int MAX_IN_ROW = 10; //确认框每行最多显示的字数
//游戏总时间设置
private final static int GAME_TIME = 999;
private int currcount = 0; //计时器
//0:开始 1:投降 2:悔棋 3:求和
private boolean operateflag[] = new boolean[OPERATE_WIDTH_TILES];
private GobangDeal gobangdeal;
private MainGameEvent maingameevent;
private CommStructNode commstructnode; //玩家信息
private CommStructNode oppuserinfo; //对家信息
private int xViewWindow; //LayerManager的偏移坐标
private int yViewWindow;
private int wViewWindow;
private int hViewWindow;
//各种标志控制
private boolean backflag; //返回按钮是否可以按下
private boolean initflag ;//是否是刚进入本界面
private boolean helpflag; //帮助菜单是否显示标志 true 正显示
private boolean qipanselectflag = false; //表示棋盘是否可以被选种
private int gamestate = 0; //游戏状态
private int usercolor; //用户的旗子颜色,1:黑色 2:白色
private int anticolor; //对家的棋子颜色
private boolean usertimeflag; //用户计时标志
private boolean opptimeflag; //对家计时标志
private int usertime; //用户的游戏时间
private int opptime; //对家的游戏时间
private int waitopptimer; //等待对家下子最长时间,超过这个时间重发一次下子操作
private boolean waitoppflag; //是否激活对waitopptimer的判断
private int stepnum; //当前步数 主要为悔棋设置
private boolean[][] chessesflag = new boolean[15][15];//棋子标志数组 true 表示可以下子 false 表示不能下子
private int agreetype; //确认选择框的类别标志 0:悔棋 1:求和
private boolean regetflag; //是否可以悔棋标志
private boolean sendflag; //已经发出一次数据请求标志
private int resendflag; //数据包重发记数
private GobangTimer gobangtimer;
public GobangCanvas(boolean arg0) {
super(arg0);
setFullScreenMode(true);
currcount = 0;
mainWidth = getWidth();
mainHeight = getHeight()+20;
//判断是否使用预览模式窗口
//根据显示设备,设置合适的最大区和显示视野
this.xViewWindow = 0;
if(BG_WIDTH > mainWidth){
//现有设备不能容纳所有游戏区域
userViewWindow = true;
this.wViewWindow = mainWidth;
}else{
//现有设备可以容纳所有游戏区域
this.wViewWindow = BG_WIDTH;
}
this.yViewWindow = 0;
if(BG_HEIGHT > mainHeight){
userViewWindow = true;
this.hViewWindow = mainHeight;
}else{
this.hViewWindow = BG_HEIGHT;
}
}
public static synchronized GobangCanvas getInstance(){
if(instance == null){
instance = new GobangCanvas(false);
}
return instance;
}
public void gameinit(){
lm = new LayerManager();
//设定图层显示方式
if(userViewWindow){
this.lm.setViewWindow(xViewWindow, yViewWindow, wViewWindow, hViewWindow);
}
g = this.getGraphics();
for(int i =0; i<QIZIWIDTH_IN_TILES ;i++)
for(int j = 0; j<QIZIHEIGHT_IN_TILES; j++){
chessesflag[i][j] = true;
}
//chatfont = Font.getDefaultFont();
System.out.println("gameinit: "+String.valueOf(gobangcontrol.rt.freeMemory()));
//画背景
Image tempimage=null;
try {
tempimage = Image.createImage("/png/floor.png"); //读取背景图片
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
TiledLayer bglayer = null;
bglayer = new TiledLayer(BGWIDTH_IN_TILES, BGHEIGHT_IN_TILES,tempimage, BGTILE_WIDTH, BGTILE_HEIGHT);
bglayer.fillCells(0,0,BGWIDTH_IN_TILES,BGHEIGHT_IN_TILES,1);
bglayer.setPosition(ORIGIN_X,ORIGIN_Y);
lm.insert(bglayer,0);
tempimage = null;
bglayer = null;
gobangcontrol.rt.gc();
//System.out.println("background: "+String.valueOf(gobangcontrol.rt.freeMemory()));
drawchatinfo(); //画聊天框控件
//System.out.println("chatcontrol: "+String.valueOf(gobangcontrol.rt.freeMemory()));
//画棋盘
try {
tempimage = Image.createImage("/png/chessboard.png"); //读取棋盘图片
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
TiledLayer qipanlayer = null;
qipanlayer = new TiledLayer(1,1,tempimage,tempimage.getWidth(),tempimage.getHeight());
qipanlayer.setCell(0,0,1);
qipanlayer.setPosition(QIPAN_X,QIPAN_Y);
lm.insert(qipanlayer,0); //插入棋盘图层
tempimage = null;
qipanlayer = null;
gobangcontrol.rt.gc();
//System.out.println("chessboard: "+String.valueOf(gobangcontrol.rt.freeMemory()));
//画功能操作图层
try {
tempimage = Image.createImage("/png/operateimage.png"); //读取功能操作图片
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
operatelayer = new TiledLayer(OPERATE_WIDTH_TILES,OPERATE_HEIGHT_TILES,tempimage,OPERATE_TILE_WIDTH,OPERATE_TILE_HEIGHT);
operatelayer.setPosition(OPERATE_X,OPERATE_Y);
lm.insert(operatelayer,0);
tempimage = null;
gobangcontrol.rt.gc();
//System.out.println("operateimage: "+String.valueOf(gobangcontrol.rt.freeMemory()));
//画功能操作按扭置灰图层
Image grayimage = Image.createImage(OPERATE_TILE_WIDTH,OPERATE_TILE_HEIGHT);
grayimage = this.setImageAlph(grayimage,false);
graylayer = new TiledLayer(OPERATE_WIDTH_TILES,OPERATE_HEIGHT_TILES,grayimage,OPERATE_TILE_WIDTH,OPERATE_TILE_HEIGHT);
graylayer.setPosition(OPERATE_X,OPERATE_Y);
lm.insert(graylayer,0);
operatelayerinit();
grayimage = null;
//System.out.println("operateimagehui: "+String.valueOf(gobangcontrol.rt.freeMemory()));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -