📄 gameframe.java
字号:
package myprojects.mygame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Stack;
class GameFrame extends JFrame {
JPanel cpLoading=new JPanel();
JLabel LoadingPic=new JLabel(new ImageIcon("Images/loading_scr.jpg"));
ChoosePanel choosePnl=null;
PlayPanel playPanel=null;
void fsetPlayPanel(){
playPanel=new PlayPanel();
playPanel.mySet();
}
void fshowPlayPanel(){
this.fsetPlayPanel();
this.setContentPane(playPanel.cpPlay);
this.show();
}
void setcpLoading(){
cpLoading.setLayout(new BorderLayout());
//cpLoading.setVisible(true);
//cpLoading.setBackground(Color.red);
cpLoading.add(LoadingPic);
}
void showcpLoading(){
setContentPane(cpLoading);
show();
}
void fsetcpChoose(){
choosePnl=new ChoosePanel();
choosePnl.mySet();
GameFrame.this.show();
}
void fshowcpChoose(){
fsetcpChoose();
setContentPane(choosePnl.cpChoose);
GameFrame.this.show();
System.out.println("choose");
}
JPanel cpRegister=new JPanel();
JLabel RegisterPic=new JLabel(new ImageIcon("Images/splash.jpg"));
JButton btnHostgame=new JButton(new ImageIcon("Images/HostGameS.jpg"));
JButton btnJoingame=new JButton(new ImageIcon("Images/JoinGameS.jpg"));
JButton btnExitgame=new JButton(new ImageIcon("Images/ExitGameS.jpg"));
void setcpRegister(){
cpRegister.setLayout(null);
RegisterPic.setLocation(0,0);
RegisterPic.setSize(800,600);
cpRegister.add(RegisterPic);
RegisterPic.setLayout(null);
RegisterPic.setVisible(true);
///*
btnHostgame.setBorder(null);
btnJoingame.setBorder(null);
btnExitgame.setBorder(null);
//*/
btnHostgame.setSize(btnHostgame.getPreferredSize());
btnJoingame.setSize(btnJoingame.getPreferredSize());
btnExitgame.setSize(btnExitgame.getPreferredSize());
btnHostgame.setRolloverIcon(new ImageIcon("Images/HostGameM.jpg"));
btnJoingame.setRolloverIcon(new ImageIcon("Images/JoinGameM.jpg"));
btnExitgame.setRolloverIcon(new ImageIcon("Images/ExitGameM.jpg"));
btnHostgame.setPressedIcon(new ImageIcon("Images/HostGameP.jpg"));
btnJoingame.setPressedIcon(new ImageIcon("Images/JoinGameP.jpg"));
btnExitgame.setPressedIcon(new ImageIcon("Images/ExitGameP.jpg"));
int btngap=btnHostgame.getHeight()/2;
btnHostgame.setLocation(400-btnHostgame.getWidth()/2,350);
btnJoingame.setLocation(400-btnHostgame.getWidth()/2,400);
btnExitgame.setLocation(400-btnHostgame.getWidth()/2,450);
btnHostgame.setVisible(true);
btnJoingame.setVisible(true);
btnExitgame.setVisible(true);
RegisterPic.add(btnHostgame);
btnHostgame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ex){
//ChatServer host=new ChatServer();
//host.start();
hostDialog d=new hostDialog(GameFrame.this,"CreatGame");
}
});
RegisterPic.add(btnJoingame);
btnJoingame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ex){
joinDialog d=new joinDialog(GameFrame.this,"CreatGame"); }
});
RegisterPic.add(btnExitgame);
btnExitgame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ex){
System.exit(0); }
});
}
void showcpRegister(){
setContentPane(cpRegister);
show();
}
JPanel cpChoose=new JPanel();
JPanel pnlTitle=new JPanel();
JLabel choosePic=new JLabel(new ImageIcon("Images/choosetoken.jpg"));
JLabel lblChooseSuspext=new JLabel("Choose A Supect",JLabel.CENTER);
JLabel lblsbsGame=new JLabel("XX's game");
JLabel lblIpAdress=new JLabel("IP Adress=X X X X");
JPanel pnlSuspect=new JPanel();
JLabel tableHead=new JLabel(
stdlength("Order",15)+stdlength("Player Name",20)+stdlength("Suspect",30));
//new ImageIcon("Images/join.gif"),JLabel.LEFT);
JLabel[] table={
new JLabel(
stdlength("1",15) + stdlength("Empty",20) + stdlength("Tina Time Traveler",30) ,
new ImageIcon("Images/tina.gif"),JLabel.LEFT),
new JLabel(new ImageIcon("Images/sam.gif")),
new JLabel(new ImageIcon("Images/mona.gif")),
new JLabel(new ImageIcon("Images/george.gif")),
new JLabel(new ImageIcon("Images/uhura.gif")),
new JLabel(new ImageIcon("Images/steve.gif"))
};
JPanel pnlMessage=new JPanel();
void setcpChoose(){
cpChoose.setBorder(BorderFactory.createEtchedBorder());
cpChoose.setLayout(null);
choosePic.setLocation(0,0);
choosePic.setSize(794,566);
choosePic.setVisible(true);
cpChoose.add(choosePic);
pnlTitle.setLayout(new BorderLayout());
lblChooseSuspext.setOpaque(false);
lblChooseSuspext.setForeground(Color.magenta);
lblChooseSuspext.setFont(new Font("Times New Roman",Font.BOLD,30));
lblsbsGame.setOpaque(false);
lblsbsGame.setForeground(Color.magenta);
lblsbsGame.setFont(new Font("Times New Roman",Font.BOLD,25));
lblIpAdress.setOpaque(false);
lblIpAdress.setForeground(Color.magenta);
lblIpAdress.setFont(new Font("Times New Roman",Font.BOLD,25));
pnlTitle.add(lblChooseSuspext,"North");
pnlTitle.add(lblIpAdress,"East");
pnlSuspect.setLayout(new BoxLayout(pnlSuspect,BoxLayout.Y_AXIS));
tableHead.setBorder(BorderFactory.createEtchedBorder());
tableHead.setFont(new Font("Times New Roman",Font.BOLD,26));
tableHead.setForeground(Color.lightGray);
pnlSuspect.add(tableHead);
table[0].setForeground(Color.red);
table[1].setForeground(Color.blue);
table[2].setForeground(Color.yellow);
table[3].setForeground(Color.green);
table[4].setForeground(Color.magenta);
table[5].setForeground(Color.white);
for(int i=0;i<6;i++){
table[i].setBorder(BorderFactory.createEtchedBorder());
table[i].setFont(new Font("Times New Roman",Font.BOLD,26));
table[i].setOpaque(false);
table[i].addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
JLabel s=(JLabel)(e.getSource());
s.setOpaque(true);
s.repaint();
}
public void mouseExited(MouseEvent e){
JLabel s=(JLabel)(e.getSource());
s.setOpaque(false);
s.repaint();
}
});
pnlSuspect.add(table[i]);
}
pnlTitle.setOpaque(false);
pnlSuspect.setOpaque(false);
pnlMessage.setOpaque(false);
choosePic.setLayout(new BorderLayout());
pnlTitle.setBorder(BorderFactory.createEtchedBorder());
pnlSuspect.setBorder(BorderFactory.createEtchedBorder());
pnlMessage.setBorder(BorderFactory.createEtchedBorder());
choosePic.add(pnlTitle,"North");
choosePic.add(pnlSuspect,"Center");
choosePic.add(pnlMessage,"South");
}
void showcpChoose(){
System.out.println("choose");
setContentPane(cpChoose);
show();
}
JPanel cpPlay=new JPanel();
JPanel pnlDataCheck=new JPanel();
JPanel pnlDataPic=new JPanel();
JRootPane gameboard=new JRootPane();
JPanel[][] block=new JPanel[20][25];
Color blockLineColor=Color.lightGray;//new Color(180,180,250);
Color blockBodyColor=new Color(150,255,255);
JLabel blbGameStatus=new JLabel();
JPanel cpNote=new JPanel();
JPanel pnl5Button=new JPanel();
JPanel pnlSenMessage=new JPanel();
JTextArea txtMessage=new JTextArea();
JButton btnSend=new JButton();
JTextField txtWrite=new JTextField();
void setcpPlay(){
cpPlay.setLayout(null);
pnlDataCheck.setBounds(0,0,300,400);
pnlDataPic.setBounds(2,2,280,398);
pnlDataPic.setBorder(BorderFactory.createEtchedBorder());
pnlDataPic.add(new JLabel(new ImageIcon("Images/space_pickle_data.jpg")));
setblbGameStatus();
setcpNote();
setpnl5Button();
setpnlSenMessage();
cpPlay.add(pnlDataPic);
setgameboard();
cpPlay.add(gameboard);
cpPlay.add(blbGameStatus);
cpPlay.add(pnl5Button);
cpPlay.add(pnlSenMessage);
}
void setblbGameStatus(){
blbGameStatus.setBounds(0,400,300,200);
}
void setgameboard(){
gameboard.setBounds(290,0,500,400);
setgameboardcp();
setgameboardgp();
}
void setgameboardcp(){
Container cp=gameboard.getContentPane();
cp.setBackground(Color.red);
cp.add(new JLabel(new ImageIcon("Images/gameboard.jpg")));
}
void setgameboardgp(){
JPanel gp=new JPanel();
gameboard.setGlassPane(gp);
gp.setVisible(true);
gp.setOpaque(false);
gp.setLayout(new GridLayout(20,25));
//gp.add(new JButton("asfasdf"));
for(int i=0;i<block.length;++i){
for(int j=0;j<block[i].length;++j){
block[i][j]=new JPanel();
final int fi=i,fj=j;
block[i][j].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
block[fi][fj].setOpaque(!block[fi][fj].isOpaque());
block[fi][fj].repaint();
}
});
block[i][j].setBackground(blockBodyColor);
block[i][j].setOpaque(false);
block[i][j].setBorder(
BorderFactory.createLineBorder(blockLineColor)
);
gp.add(block[i][j]);
}
}
for(int i=0;i<4;i++)
for(int j=0;j<6;j++) block[i][j].setBorder(null);
for(int i=0;i<5;++i) block[4][i].setBorder(null);
block[9][0].setBorder(null);
block[10][0].setBorder(null);
for(int i=7;i<13;++i)
for(int j=1;j<6;++j) block[i][j].setBorder(null);
for(int i=17;i<20;++i)
for(int j=0;j<6;++j) block[i][j].setBorder(null);
for(int i=15;i<17;++i)
for(int j=0;j<5;++j) block[i][j].setBorder(null);
for(int i=10;i<14;++i)block[0][i].setBorder(null);
for(int i=1;i<5;++i)
for(int j=8;j<16;++j) block[i][j].setBorder(null);
for(int i=19;i<25;++i)
for(int j=0;j<4;++j)block[i][j].setBorder(null);
}
void setcpNote(){
cpNote.setBounds(0,400,300,200);
}
void setpnl5Button(){
pnl5Button.setBounds(300,400,130,200);
pnl5Button.setLayout(new BoxLayout(pnl5Button,BoxLayout.Y_AXIS));
JButton btnEndTurn=new JButton(new ImageIcon("Images/End Turn Blue S.jpg"));
JButton btnHideData=new JButton(new ImageIcon("Images/Hide Data Blue S.jpg"));
JButton btnGameStatus=new JButton(new ImageIcon("Images/Game Status Blue S.jpg"));
JButton btnHypothesis=new JButton(new ImageIcon("Images/Hypothesis Blue S.jpg"));
JButton btnSolution=new JButton(new ImageIcon("Images/Solution Blue S.jpg"));
btnEndTurn.setPressedIcon(new ImageIcon("Images/End Turn Blue P.jpg"));
btnHideData.setPressedIcon(new ImageIcon("Images/Hide Data Blue P.jpg"));
btnGameStatus.setPressedIcon(new ImageIcon("Images/Game Status Blue P.jpg"));
btnHypothesis.setPressedIcon(new ImageIcon("Images/Hypothesis Blue P.jpg"));
btnSolution.setPressedIcon(new ImageIcon("Images/Solution Blue P.jpg"));
btnEndTurn.setBorder(null);
btnHideData.setBorder(null);
btnGameStatus.setBorder(null);
btnHypothesis.setBorder(null);
btnSolution.setBorder(null);
btnEndTurn.setMargin(new Insets(9,2,9,2));
btnHideData.setMargin(new Insets(9,2,9,2));
btnGameStatus.setMargin(new Insets(9,2,9,2));
btnHypothesis.setMargin(new Insets(9,2,9,2));
btnSolution.setMargin(new Insets(9,2,9,2));
pnl5Button.add(btnEndTurn);
pnl5Button.add(btnHideData);
pnl5Button.add(btnGameStatus);
pnl5Button.add(btnHypothesis);
pnl5Button.add(btnSolution);
}
void setpnlSenMessage(){
pnlSenMessage.setBounds(430,400,370,200);
pnlSenMessage.setLayout(new BorderLayout());
//pnlSenMessage.setLayout(null);
JScrollPane scrText=new JScrollPane(txtMessage);
scrText.setBounds(0,0,320,200);
pnlSenMessage.add(scrText);
}
void showcpPlay(){
System.out.println("ShowPlay");
setContentPane(cpPlay);
show();
}
public GameFrame(){
setTitle("GameFrame");
setSize(new Dimension(800, 600));
//this.setResizable(false);
this.addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e){
GameFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
String stdlength(String s,int l){
String space="";
int rest=l-s.length();
if(rest>0){
for(int i=0;i<rest;++i){
space+=" ";
}
}
return s+space;
}
}
class MyGlassPane extends JPanel{
public MyGlassPane(){
//setOpaque(false);
}
}
class Person{
int ID;
String IP=null;
String name=null;
myPosition position=new myPosition();
int term=Person.isIdle;
boolean isMoved=false;
public void moveTo(myPosition d){
position.x=d.x;
position.y=d.y;
}
public void moveTo(int x_d,int y_d){
position.x=x_d;
position.y=y_d;
}
///dicide whether the event can do or not
public static final int isIdle=0,isTip=2,
isClick=4,isEndingTurn=8;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -