📄 gameframe.java
字号:
package netversion;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.StringTokenizer;
public class GameFrame extends JFrame implements GameConstant{
private static final ImageIcon table
= new ImageIcon("Image/chessTable.jpg");
private static final ImageIcon startPressedImg
= new ImageIcon("Image/startPressed.jpg");
private static final int GAP = 50;
private InnerPane panel;
private Location loc;
private Timer timer;
private MsgManager msgMan;
public GameFrame(Location loc,MsgManager msgMan){
this.loc = loc;
this.msgMan = msgMan;
buildGUI();
hookEvent();
}
private void hookEvent() {
this.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
if(timer.isRepeats()){
timer.stop();
}
}
});
}
private void buildGUI(){
this.panel = new InnerPane();
this.setLayout(new BorderLayout());
this.getContentPane().add(panel,BorderLayout.CENTER);
this.setSize(new Dimension(table.getIconWidth()+2*GAP,
table.getIconHeight()+2*GAP));
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private class InnerPane extends JPanel{
private final Color bkColor
= new Color(9,91,132);
private final int RECTX = 288;
private final int RECTY = 688;
private final int RECTW = 56;
private final int RECTH = 26;
public InnerPane(){
this.setPreferredSize(new Dimension(table.getIconWidth()+2*GAP,
table.getIconHeight()+2*GAP));
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Rectangle rect = new Rectangle(RECTX,RECTY,RECTW,RECTH);
if(rect.contains(e.getPoint())){
MyUtilities.playSound("Sound/button.wav");
drawStart();
waitStart();
}
}
});
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e){
//GameFrame.this.setTitle(e.getPoint().x+" "+e.getPoint().y);
}
});
}
public void paint(Graphics g){
//背景色填充
g.setColor(bkColor);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(table.getImage(), GAP, GAP, null);
}
public void drawStart(){
Graphics g = this.getGraphics();
g.drawImage(startPressedImg.getImage(), RECTX, RECTY, null);
}
//开启对手有没开始的线程
private void waitStart(){
System.out.println(loc == null);
msgMan.refreshTab(loc.x, loc.y, loc.right, STARTED);
timer = new Timer(
2000,new ActionListener(){
public void actionPerformed(ActionEvent e){
String status = msgMan.getTabStatus(
loc.x, loc.y, (loc.right+1)%2);
StringTokenizer st = new StringTokenizer(status,"&");
int started = Integer.parseInt(st.nextToken());
if(started == STARTED){
//双方开始了啊
GameFrame.this.setTitle(status);
msgMan.connectOther(
MyUtilities.getIPFromString((String)st.nextElement()));
}
}
});
timer.setRepeats(true);
timer.start();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -