📄 gamecanvas.java
字号:
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class GameCanvas extends Canvas implements CommandListener
{
private MyMidlet game;
private boolean dinfo = true;
private Image LRCardImage = null;
private Image UPCardImage = null;
private Image backImage = null;
private Image backGround = null;
private Image messageBak = null;
private Image messageImage = null;
private Image showButtond;
private Image tempButtonImg = null;
private Image panel;
private Image scoreImage;
private Image scoreGround;
private Image infoImage;
private Image[] playerImg = new Image[2];
private Graphics bufferg;
private Graphics messageBuffer;
private Graphics scoreBuffer;
private final int NUM = 4;
private final int cardWidth;
private final int cardHeight;
private Vector cardsRound;
private int roundNum;// 局数
private int roundKey;
private final int WIDTH = getWidth();
private final int HEIGHT = getHeight();
private int messageWidth;
private int messageHeight;
private int selectIndex = -1;
private int cardCount = 13;// 牌数
private int LCount = 13;// 左面玩家的牌数
private int RCount = 13;// 右面玩家的牌数
private int UCount = 13;// 对面玩家的牌数
private boolean myTurn = false;// 是不是到我出牌
private Card[] myCard = new Card[cardCount];
private Card[][] scores = new Card[NUM][14];
private String[] playerName = new String[NUM];
private Hashtable scoreAmount = new Hashtable();
private int desknum = -1; // 桌子序号
private int seatPos = -1; // 座位序号
private boolean banker = false;// 判断自己是不是庄
private Client client;
protected Command exitCmd, start;
private boolean keyPress = true;
private boolean gameOver = false;
public void init()
{
banker = false;
myTurn = false;
this.addCommand(start);
}
public void reset()
{
cardCount = 13;
scoreAmount.clear();
cardsRound.removeAllElements();
roundKey = 0;
roundNum = 0;
cls();
scoreCls();
for (int i = 0; i < NUM; i++)
for (int j = 0; j < 14; j++)
scores[i][j] = new Card(-1);
keyPress = true;
gameOver = false;
banker = false;
myTurn = false;
}
public GameCanvas(MyMidlet game, Client client)// 构造函数
{
try
{
infoImage = Image.createImage("/message/bg_playerinfo.png");
playerImg[0] = Image.createImage("/message/nan.png");
playerImg[1] = Image.createImage("/message/nv.png");
LRCardImage = Image.createImage("/cards/h.png");
UPCardImage = Image.createImage("/cards/0.png");
messageImage = Image.createImage("/message.png");
showButtond = Image.createImage("/button/show.png");
if (WIDTH <= 180)
{
scoreGround = Image.createImage("/score180.png");
backGround = Image.createImage("/back.png");
}
else if (WIDTH >= 240)
{
scoreGround = Image.createImage("/score240.png");
backGround = Image.createImage("/bigBack.png");
}
}
catch (Exception e)
{
System.out.println("error image...");
e.printStackTrace();
}
if (messageImage != null)
{
messageWidth = messageImage.getWidth();
messageHeight = messageImage.getHeight();
}
else
{
messageWidth = -1;
messageHeight = -1;
}
cardWidth = UPCardImage.getWidth();
cardHeight = UPCardImage.getHeight();
panel = Image.createImage(WIDTH, HEIGHT);
messageBak = Image.createImage(messageWidth, messageHeight);
scoreImage = Image.createImage(scoreGround.getWidth(), scoreGround
.getHeight());
scoreBuffer = scoreImage.getGraphics();
scoreCls();
messageBuffer = messageBak.getGraphics();
bufferg = panel.getGraphics();
cls();
cardsRound = new Vector();
roundKey = 0;
roundNum = 0;
for (int i = 0; i < NUM; i++)
{
playerName[i] = "P" + i;
for (int j = 0; j < 14; j++)
scores[i][j] = new Card(-1);
}
this.game = game;
this.client = client;
exitCmd = new Command("退出", Command.EXIT, 0);
start = new Command("开始", Command.OK, 1);
addCommand(start);
addCommand(exitCmd);
setCommandListener(this);
}
public int getDeskIndex()
{
return desknum;
}
public void setDeskIndex(int i)
{
desknum = i;
}
public void setSeatPos(int i)
{
seatPos = i;
}
public int getSeatPos()
{
return seatPos;
}
private void scoreCls()
{
scoreBuffer.drawImage(scoreGround, 0, 0, Graphics.LEFT | Graphics.TOP);
}
private void cls()
{
bufferg.drawImage(backGround, WIDTH / 2, HEIGHT / 2, Graphics.HCENTER
| Graphics.VCENTER);
}
protected void paint(Graphics g)
{
drawPlayer(gameOver);
g.drawImage(panel, 0, 0, Graphics.LEFT | Graphics.TOP);
if (dinfo)
drawInfo(g);
}
public void commandAction(Command c, Displayable d)
{
if (c == exitCmd)
{
client.sendMessage("exit");
seatPos = -1;
desknum = -1;
MyMidlet.display.setCurrent(game.getPlayerList());
}
else if (c == start)
{
banker = false;
reset();
this.removeCommand(start);
client.sendMessage("start");
}
}
private void drawPlayer(boolean show)
{
if (!gameOver)
{
for (int i = 0; i < NUM; i++)
{
char[] playerChar = playerName[i].toCharArray();
int wid = Font.getDefaultFont().charsWidth(playerChar, 0,
playerChar.length - 1);
if (i == seatPos)
{
bufferg.drawString(playerName[i], (WIDTH - wid) / 2, HEIGHT
- Font.getDefaultFont().getHeight() - cardHeight
- 10, Graphics.LEFT | Graphics.TOP);
}
else if (i == (seatPos + 1) % NUM)
{
bufferg.drawString(playerName[i], WIDTH - wid - 25,
HEIGHT / 2 - 10, Graphics.LEFT | Graphics.TOP);
}
else if (i == (seatPos + 2) % NUM)
{
bufferg.drawString(playerName[i], (WIDTH - wid) / 2, 10,
Graphics.LEFT | Graphics.TOP);
}
else if (i == (seatPos + 3) % NUM)
{
bufferg.drawString(playerName[i], 25, HEIGHT / 2 - 10,
Graphics.LEFT | Graphics.TOP);
}
}
}
}
private void drawUBackCard()
{
int px = WIDTH / 2 - 13 * 10 / 2;
int x = WIDTH / 2 - cardCount * 10 / 2;
int y = 5;
int oldx = bufferg.getClipX();
int oldy = bufferg.getClipY();
int oldw = bufferg.getClipWidth();
int oldh = bufferg.getClipHeight();
bufferg.setClip(px, y, WIDTH - px, UPCardImage.getHeight());
cls();
try
{
for (int i = 0; i < UCount; i++)
{
bufferg.drawImage(UPCardImage, x, y, Graphics.LEFT
| Graphics.TOP);
x += 10;
}
}
catch (Exception e)
{
e.printStackTrace();
}
repaint();
bufferg.setClip(oldx, oldy, oldw, oldh);
}
private void drawLBackCard()
{
int py = HEIGHT / 2 - 13 * 7 / 2 - 8;
int y = HEIGHT / 2 - UCount * 7 / 2 - 8;
int x = 10;
int oldx = bufferg.getClipX();
int oldy = bufferg.getClipY();
int oldw = bufferg.getClipWidth();
int oldh = bufferg.getClipHeight();
bufferg.setClip(x, py, LRCardImage.getWidth(), 13 * 7 + 7);
cls();
try
{
for (int i = 0; i < LCount; i++)
{
bufferg.drawImage(LRCardImage, x, y, Graphics.LEFT
| Graphics.TOP);
y += 7;
}
}
catch (Exception e)
{
e.printStackTrace();
}
repaint();
bufferg.setClip(oldx, oldy, oldw, oldh);
}
private void drawRBackCard()
{
int py = HEIGHT / 2 - 13 * 7 / 2 - 8;
int y = HEIGHT / 2 - cardCount * 7 / 2 - 8;
int x = WIDTH - LRCardImage.getWidth() - 10;
int oldx = bufferg.getClipX();
int oldy = bufferg.getClipY();
int oldw = bufferg.getClipWidth();
int oldh = bufferg.getClipHeight();
bufferg.setClip(x, py, LRCardImage.getWidth(), 13 * 7 + 7);
cls();
try
{
for (int i = 0; i < RCount; i++)
{
bufferg.drawImage(LRCardImage, x, y, Graphics.LEFT
| Graphics.TOP);
y += 7;
}
}
catch (Exception e)
{
e.printStackTrace();
}
repaint();
bufferg.setClip(oldx, oldy, oldw, oldh);
}
protected synchronized void keyPressed(int keyCode) // 处理按键
{
int action = getGameAction(keyCode);
if (action == Canvas.LEFT)
{
// 如果按键是左键
if (getSelectIndex() <= 0)
{
setSelectIndex(cardCount - 1);
}
else
{
setSelectIndex(getSelectIndex() - 1);
}
drawCards();
}
else if (action == Canvas.RIGHT)
{
// 如果按键是右键
if (getSelectIndex() >= cardCount - 1)
{
setSelectIndex(0);
}
else
{
setSelectIndex(getSelectIndex() + 1);
}
drawCards();
}
else if (action == Canvas.UP)
{
// 如果按键是上键
dinfo = !dinfo;
repaint();
}
else if (action == Canvas.DOWN)
{
// 如果按键是下键
dinfo = !dinfo;
repaint();
}
else if (action == Canvas.FIRE && myTurn && keyPress && !gameOver)
{
// 如果按键是select(开火)键
deal();
drawCards();
}
}
private void showButton(boolean b)
{
if (tempButtonImg == null)
tempButtonImg = Image.createImage(panel);
int x = (WIDTH - showButtond.getWidth()) / 2;
int y = HEIGHT - cardHeight - showButtond.getHeight() - 10 - 8;
int oldx = bufferg.getClipX();
int oldy = bufferg.getClipY();
int oldw = bufferg.getClipWidth();
int oldh = bufferg.getClipHeight();
bufferg.setClip(x, y, showButtond.getWidth(), showButtond.getHeight());
if (b)
{
bufferg.drawImage(showButtond, x, y, Graphics.LEFT | Graphics.TOP);
}
else
{
if (tempButtonImg != null)
bufferg.drawImage(tempButtonImg, 0, 0, Graphics.LEFT
| Graphics.TOP);
tempButtonImg = null;
}
repaint(x, y, showButtond.getWidth(), showButtond.getHeight());
bufferg.setClip(oldx, oldy, oldw, oldh);
}
private boolean haveSuit(int key)// 是否有相应的花牌
{
for (int i = 0; i < cardCount; i++)
{
if ((myCard[i] != null) && (myCard[i].getSuitIndex() == key))
{
return true;
}
}
return false;
}
public Card show(int index, Vector v)// 出牌规则
{
boolean isFirst = false;
if ((v.size() >= 4) || (v.size() == 0))
{
isFirst = true;
roundKey = myCard[index].getSuitIndex();
}
try
{
Card c = null;
int rec = -1;
if (myCard[index] != null)
{
c = myCard[index];
rec = index;
}
if ((roundNum == 0) && (c != null) && isFirst
&& (c.getValue() != 0))// 第一张必须梅花2
{
alert();
return null;
}
if ((c != null) && !isFirst && haveSuit(roundKey) && // 如果有适合花色的牌没出,发出警告
(c.getSuitIndex() != roundKey))
{
alert();
return null;
}
if (c != null)
{
myCard[rec] = null;
// 排序
for (int i = rec + 1; i < cardCount; i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -