📄 table.java
字号:
package netversion;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.*;
public class Table extends JPanel implements GameConstant{
private static final String serverIP = "172.17.74.93";
private static final Color bkColor
= new Color(81,113,158);
private static final int TIP_SIZE = 30;
private Image tablesImg;
private Image tablesEnImg;
private Image personImg;
private int imgWidth,imgHeight;
private int rows,cols;
private int[][][] isOcuupiedTab;
private Point[][] roomNumberPoint;
//两个入口
private Location[][][] roomEntrance;
private MsgManager msgMan;
//当前鼠标指示房间
private volatile Location originPoint;
private volatile Location curPoint;
private volatile Location clickPoint;
public Table(int rows,int cols){
//未进入房间前不画指示
this.originPoint = null;
this.curPoint = null;
this.rows = rows;
this.cols = cols;
this.tablesImg = new ImageIcon("Image/tables.jpg").getImage();
this.tablesEnImg = new ImageIcon("Image/tablen.jpg").getImage();
this.personImg = new ImageIcon("Image/personImg.jpg").getImage();
imgWidth = tablesImg.getWidth(null);
imgHeight = tablesImg.getHeight(null);
this.setPreferredSize(new Dimension(width(),height()));
prepareData();
hookEvent();
}
//////////////
private void hookEvent(){
this.addMouseMotionListener(
new MyMouseMotionAdapter());
this.addMouseListener(new MyMouseAdapter());
//用于更新桌面
new Thread(new Runnable(){
public void run(){
while(true){
initOcuupiedTab();
Table.this.paintTab();
MyUtilities.delay(2000);
}
}
}).start();
}
////////////
private void prepareData(){
this.isOcuupiedTab = new int[rows][cols][2];
this.msgMan = new MsgManager(serverIP,7000);
initOcuupiedTab();
this.roomNumberPoint = new Point[rows][cols];
this.roomEntrance = new Location[rows][cols][2];
for(int i=0;i < rows; ++i){
for(int j=0;j < cols; ++j){
this.roomNumberPoint[i][j] =
new Point( (int)((j+0.45)*this.imgWidth),
(int)((i+0.8)*this.imgHeight));
//左边的入口
this.roomEntrance[i][j][0] =
new Location( j*this.imgWidth+1,
(int)(i*this.imgHeight+40),0);
//右边的入口
this.roomEntrance[i][j][1] =
new Location((int)((j+1)*this.imgWidth-TIP_SIZE-1),
(int)(i*this.imgHeight+40),1);
}
}
}
private void initOcuupiedTab() {
String requestTabInfo = this.msgMan.requestTabInfo();
StringTokenizer st = new StringTokenizer(requestTabInfo,"&");
for(int i=0;i <rows; ++i){
for(int j=0;j <cols; ++j){
for(int k=0;k <2; ++k){
this.isOcuupiedTab[i][j][k] =
Integer.valueOf(st.nextToken());
}
}
}
}
public int width(){
return cols*imgWidth;
}
public int height(){
return rows*imgHeight;
}
public void paintTab(){
paint(this.getGraphics());
}
public void paint(Graphics g){
Image curImg;
for(int i=0;i < rows; ++i){
for(int j=0; j < cols;++j){
curImg = (this.isOcuupiedTab[i][j][0]==OCCUPIED
&&this.isOcuupiedTab[i][j][1]==OCCUPIED)
?this.tablesImg:this.tablesEnImg;
//房间图标
g.drawImage( curImg,j*this.imgWidth, i*this.imgHeight, null);
//房间编号
g.setColor(Color.white);
g.setFont(new Font("Dialog",Font.BOLD,12));
g.drawString(String.valueOf(i*cols+j+1),
roomNumberPoint[i][j].x,roomNumberPoint[i][j].y);
//画入口标记和人头像
g.setColor(bkColor);
for(int k=0; k < 2; ++k){
g.draw3DRect(this.roomEntrance[i][j][k].x,
this.roomEntrance[i][j][k].y,
TIP_SIZE,TIP_SIZE,true);
//人头像
if(isOcuupiedTab[i][j][k]==OCCUPIED)
g.drawImage(personImg,
this.roomEntrance[i][j][k].x,
this.roomEntrance[i][j][k].y,
null);
}
}
}
}
public void repaint(){
Graphics g = this.getGraphics();
//画进入时候的标记
if(this.clickPoint!=null){
g.drawImage(this.personImg,
this.roomEntrance[clickPoint.x][clickPoint.y][clickPoint.right].x,
this.roomEntrance[clickPoint.x][clickPoint.y][clickPoint.right].y,
null);
//隐藏窗口了,进入房间.
MyUtilities.playSound("Sound/button.wav");
//占位了
this.isOcuupiedTab[clickPoint.x][clickPoint.y][clickPoint.right] = OCCUPIED;
this.clickPoint = null;
return;
}
//画进入房间的提示
if(this.curPoint!=null){
g.setColor(Color.white);
g.draw3DRect(this.roomEntrance[curPoint.x][curPoint.y][curPoint.right].x,
this.roomEntrance[curPoint.x][curPoint.y][curPoint.right].y,
TIP_SIZE,TIP_SIZE,true);
}
if(this.originPoint!=null){
//擦除上次印迹
g.setColor(bkColor);
g.draw3DRect(this.roomEntrance[originPoint.x][originPoint.y][originPoint.right].x,
this.roomEntrance[originPoint.x][originPoint.y][originPoint.right].y,
TIP_SIZE,TIP_SIZE,true);
}
}
//判断一点是否为桌子的入口
public Location getPointLocation(Point p){
int roomY = p.x/this.imgWidth;
int roomX = p.y/this.imgHeight;
if(roomX>=rows||roomY>=cols)
return null;
//一左一右
Rectangle rect[] = new Rectangle[2];
for(int i=0; i < 2; ++i){
rect[i] = new Rectangle(
roomEntrance[roomX][roomY][i].x,
roomEntrance[roomX][roomY][i].y,
TIP_SIZE,TIP_SIZE);
if(rect[i].contains(p))
return new Location(roomX,roomY,i);
}
return null;
}
private class MyMouseAdapter extends MouseAdapter{
public void mouseClicked(MouseEvent e){
Location temp;
if( (temp=Table.this.getPointLocation(e.getPoint()))!=null&&
Table.this.isOcuupiedTab[temp.x][temp.y][temp.right]==EMPTY){
Table.this.clickPoint = temp;
//更新服务器
msgMan.refreshTab(clickPoint.x,clickPoint.y,clickPoint.right, OCCUPIED);
Table.this.repaint();
//进入游戏主界面了
new GameFrame(temp,msgMan);
}
}
}
private class MyMouseMotionAdapter extends MouseMotionAdapter{
@Override
public void mouseMoved(MouseEvent e) {
Location temp;
if( (temp=Table.this.getPointLocation(e.getPoint()))!=null){
if(Table.this.curPoint!=null&&!Table.this.curPoint.equals(temp))
Table.this.originPoint = curPoint;
//鼠标移开了,需要擦除
Table.this.curPoint = temp;
Table.this.repaint();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -