📄 tframe.java~215~
字号:
package mymatrix;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import dialog.*;
import Server.*;
import client.*;
import java.util.*;
public class TFrame extends JFrame implements NetRead{
String hostname;
MyServer server;
MyClient client;
public Panel currentPane;
public Tetrics m_tetrics;
public PTetrics p_tetrics;
public CTetrics c_tetrics;
// public int gamelevel;
// public int c_gamelevel;
boolean isServer=false;
public static int NOCONNECT = 0, SERVER = 1, CLIENT = 2;
public int m_nNetStatus = NOCONNECT;
public static int NET_RIVAL=0,P_RIVAL=1,COMPUTER_RIVAL=2;
public int m_nRivalStatus=COMPUTER_RIVAL;
public Sound back;
public boolean isPlay=false;
public boolean isSoundEfect=false;
public String filename;
JPanel contentPane;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
//is the computer
// static boolean isComputer=true;
//Construct the frame
public TFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
// main function
public static void main(String[] args) {
TFrame mframe = new TFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// set location (center
Dimension frameSize = mframe.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
mframe.setLocation( (screenSize.width - frameSize.width) / 8,
(screenSize.height - frameSize.height) / 8);
mframe.setVisible(true);
mframe.setSize(800, 600);
//mframe.setSize(750, 550);
mframe.show();
}
//Component initialization
private void jbInit() throws Exception {
m_tetrics = new Tetrics(this);
c_tetrics= new CTetrics(this);
p_tetrics= new PTetrics(this);
addMenu();
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setTitle("Main Frame");
statusBar.setBackground(Color.pink);
statusBar.setBorder(BorderFactory.createRaisedBevelBorder());
statusBar.setMinimumSize(new Dimension(227, 20));
statusBar.setOpaque(true);
statusBar.setPreferredSize(new Dimension(227, 20));
statusBar.setRequestFocusEnabled(true);
statusBar.setHorizontalAlignment(SwingConstants.CENTER);
statusBar.setText("Please wait to connect or connect to a server!");
statusBar.setVerticalTextPosition(javax.swing.SwingConstants.CENTER);
contentPane.add(statusBar, BorderLayout.SOUTH);
setTerics(m_nRivalStatus);
filename="back1.mid";
}
public void setTerics(int n){
if(n==2)
currentPane=p_tetrics;
else if(n==1)
currentPane=c_tetrics;
else if(n==0)
currentPane=m_tetrics;
contentPane.add(currentPane,BorderLayout.CENTER);
contentPane.updateUI();
}
public void removeTerics(){
contentPane.remove(currentPane);
}
public void readStr(String str) {
//如果读来的数据是对方的状态信息
if (str.startsWith("Status:")) {
int[] nRivalField = new int[Tetrics.m_nCols * (Tetrics.m_nRows + 4)];
str = str.substring(7, str.length());
StringTokenizer st = new StringTokenizer(str, "|");
int i = 0;
try {
while (st.hasMoreTokens()) {
nRivalField[i] = Integer.parseInt(st.nextToken());
i++;
}
}
catch (Exception e) {}
i = 0;
for (int col = 0; col < Tetrics.m_nCols; col++)
for (int row = 0; row < Tetrics.m_nRows; row++) {
m_tetrics.m_nRivalField[col][row] = nRivalField[i];
i++;
}
}
//如果读来的数据是对方消去一行的消息。
else if (str.startsWith("RemoveLine:")) {
str = str.substring(11, str.length());
m_tetrics.addRandomLine(Integer.parseInt(str));
}
//如果读来的数据是对方开始游戏的信息
else if (str.equals("StartGame")) {
m_tetrics.start();
}
//如果读来的数据是对方暂停游戏的信息
else if (str.equals("PauseGame")) {
m_tetrics.pause();
}
//如果读来的数据是对方中止游戏的信息
else if (str.equals("StopGame")) {
m_tetrics.stop();
}
else if(str.equals("quit")){
m_tetrics.stop();
if(this.isServer)
statusBar.setText("the client has quit!");
else
statusBar.setText("the server has quit!");
}
//如果读来的数据是对方要求改变游戏级别的信息
else if (str.startsWith("Level:")) {
str = str.substring(6, str.length());
try {
m_tetrics.setPlayLevel(Integer.parseInt(str));
}
catch (Exception e) {}
}
//如果读来的数据是对方对你谈话的内容
else if (str.startsWith("Talk:")) {
str = str.substring(5, str.length());
statusBar.setText("对手:" + str + "\n");
}
//如果读来的数据是对方的游戏已经结束的信息
else if (str.startsWith("GameOver:")) {
str = str.substring(9, str.length());
if (m_tetrics.m_bGameInPlay) {
m_tetrics.m_bGameInPlay = false;
m_tetrics.stop();
sendStr("GameOver:" + m_tetrics.m_nTheScore);
}
try {
int nRivalScore = Integer.parseInt(str);
new GameOverD(m_tetrics.m_nTheScore, nRivalScore);
//System.out.print(m_tetrics.m_nTheScore);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void showMessage(String str) {
statusBar.setText(str + "\n");
}
/**
* 将信息发给对方
*/
public void sendStr(String str) {
if (this.isServer) {
if (this.server != null)
server.writeStr(str + "\n");
}
else {
if (this.client != null)
client.writeStr(str + "\n");
}
}
public void start(){
back=new Sound(filename,1);
back.start(1);
}
public void stop(){
if(this.isPlay)
back.stop();
}
public void jMenuFileExit_actionPerformed(ActionEvent e) {
/* if(sread.isAlive())
sread.stop();
if(cread.isAlive())
cread.stop();*/
this.sendStr("quit");
if (this.m_nNetStatus == this.SERVER) this.server.close();
if (this.m_nNetStatus == this.CLIENT) this.client.close();
System.exit(0);
}
//About action performed
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
private void addMenu()
{
MenuBar menuBar=new MenuBar();
MenuListener menuListener=new MenuListener(this);
MenuShortcut ms=new MenuShortcut(KeyEvent.VK_S);
Menu menu1=new Menu("游戏");
Menu menu2=new Menu("控制");
Menu menu4=new Menu("关于");
menuBar.add(menu1);
menuBar.add(menu2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -