📄 chessframe.java
字号:
//ChessFrame.java
package fivechess;
import javax.swing.JFrame;
import java.awt.HeadlessException;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public class ChessFrame extends JFrame implements ActionListener {
//背景音乐
private Sound backsound = new Sound("back.mid");
private String[] strsize = { "20*15", "30*20", "40*30" };
private String[] strmode = { "人机对弈", "双人对弈" };
private int width, height;
private ChessModel cm;
private MainPanel mp;
private final int DEFAULT_PORT = 5454;
private ServerSocket serversocket;
private Socket clientsocket;
private SocketThread socketthread;
private InputStream in;
private OutputStream out;
private JMenuItem serverMenuItem;
private JMenuItem connectMenuItem;
private JMenuItem disconnectMenuItem;
private JMenuItem boardMenuItem;
private JMenuItem localchessMenu;
public static boolean iscomputer = true, checkcomputer = true;
public static boolean isnet = false;
public static boolean isserver = true;
public static boolean istoken = true;
public ChessFrame(String title) {
super(title);
cm = new ChessModel(1);
mp = new MainPanel(this, cm);
Container con = this.getContentPane();
con.add(mp, "Center");
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
MapSize(20, 15);
JMenuBar mbar = new JMenuBar();
this.setJMenuBar(mbar);
JMenu gameMenu = new JMenu("游戏");
mbar.add(
makeMenu(
gameMenu,
new Object[] { "开局", "棋盘", null, "音乐", null, "退出" },
this));
boardMenuItem = gameMenu.getItem(1);
JMenu lookMenu = new JMenu("视图");
mbar.add(
makeMenu(
lookMenu,
new Object[] { "Metal", "Motif", "Window" },
this));
boardMenuItem = gameMenu.getItem(1);
JMenu localMenu = new JMenu("本地对弈");
mbar.add(makeMenu(localMenu, new Object[] { "本地对弈" }, this));
localchessMenu = localMenu;
JMenu netMenu = new JMenu("网络对弈");
mbar.add(
makeMenu(netMenu, new Object[] { "启动服务", "连接服务", "中断连接" }, this));
serverMenuItem = netMenu.getItem(0);
connectMenuItem = netMenu.getItem(1);
disconnectMenuItem = netMenu.getItem(2);
disconnectMenuItem.setEnabled(false);
JMenu helpMenu = new JMenu("帮助");
mbar.add(makeMenu(helpMenu, new Object[] { "关于" }, this));
backsound.hasPlayed = true;
backsound.loop();
this.show();
}
public JMenu makeMenu(Object parent, Object items[], Object target) {
JMenu m = null;
if (parent instanceof JMenu)
m = (JMenu) parent;
else if (parent instanceof String)
m = new JMenu((String) parent);
else
return null;
for (int i = 0; i < items.length; i++)
if (items[i] == null)
m.addSeparator();
else if (items[i] == "棋盘") {
JMenu jm = new JMenu("棋盘");
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem rmenu;
for (int j = 0; j < strsize.length; j++) {
rmenu = makeRadioButtonMenuItem(strsize[j], target);
if (j == 0)
rmenu.setSelected(true);
jm.add(rmenu);
group.add(rmenu);
}
m.add(jm);
} else if (items[i] == "本地对弈") {
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem rmenu;
for (int h = 0; h < strmode.length; h++) {
rmenu = makeRadioButtonMenuItem(strmode[h], target);
if (h == 0)
rmenu.setSelected(true);
m.add(rmenu);
group.add(rmenu);
}
} else
m.add(makeMenuItem(items[i], target));
return m;
}
public JMenuItem makeMenuItem(Object item, Object target) {
JMenuItem r = null;
if (item instanceof String)
r = new JMenuItem((String) item);
else if (item instanceof JMenuItem)
r = (JMenuItem) item;
else
return null;
if (target instanceof ActionListener)
r.addActionListener((ActionListener) target);
return r;
}
public JRadioButtonMenuItem makeRadioButtonMenuItem(
Object item,
Object target) {
JRadioButtonMenuItem r = null;
if (item instanceof String)
r = new JRadioButtonMenuItem((String) item);
else if (item instanceof JRadioButtonMenuItem)
r = (JRadioButtonMenuItem)(JRadioButtonMenuItem) item;
else
return null;
if (target instanceof ActionListener)
r.addActionListener((ActionListener) target);
return r;
}
public void MapSize(int w, int h) {
setSize(w * 20 + 5, h * 20 + 100);
if (this.checkcomputer)
this.iscomputer = true;
else
this.iscomputer = false;
mp.setModel(cm);
mp.repaint();
}
public boolean getiscomputer(){
return this .iscomputer;
}
public void restart() {
int degree = cm.getDegree();
if (degree <= 3 && degree >= 1) {
cm = new ChessModel(degree);
MapSize(cm.getWidth(), cm.getHeight());
} else {
System.out.println("\u81EA\u5B9A\u4E49");
}
}
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
try {
if (arg.equals("Windows"))
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsalookAndFeel");
else if (arg.equals("Motif"))
UIManager.setLookAndFeel(
"com.sun.java.swing.plat.motif.MotiflookAndFeel");
else
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception ee) {
System.out.print(ee);
}
if (arg.equals("20*15")) {
this.width = 20;
this.height = 15;
cm = new ChessModel(1);
MapSize(this.width, this.height);
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.endsWith("30*20")) {
this.width = 30;
this.height = 20;
cm = new ChessModel(2);
MapSize(this.width, this.height);
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("40*30")) {
this.width = 40;
this.height = 30;
cm = new ChessModel(3);
MapSize(this.width, this.height);
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("人机对弈")) {
this.checkcomputer = true;
this.iscomputer=true;
cm = new ChessModel(cm.getDegree());
MapSize(cm.getWidth(), cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("双人对弈") ){
this.checkcomputer = false;
this.iscomputer = false;
cm = new ChessModel(cm.getDegree());
MapSize(cm.getWidth(), cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("启动服务")) {
if (JOptionPane
.showConfirmDialog(
this,
"确定要启动服务器,允许其他玩家连接本机吗?",
"启动服务器",
JOptionPane.YES_NO_OPTION)
== JOptionPane.YES_OPTION) {
try {
serversocket = new ServerSocket(DEFAULT_PORT);
System.out.println("服务器启动,正在监听》》》》》》》");
} catch (IOException ex) {
System.out.println(ex);
}
try {
clientsocket = serversocket.accept();
System.out.println("正在接受连接》》》》》》》");
in = clientsocket.getInputStream();
out = clientsocket.getOutputStream();
this.isserver = true;
this.isnet = true;
this.istoken = true;
socketthread = new SocketThread(this, mp);
socketthread.start();
JOptionPane.showMessageDialog(
this,
"连接建立成功,可以开始游戏!",
"连接成功",
JOptionPane.INFORMATION_MESSAGE);
serverMenuItem.setEnabled(false);
connectMenuItem.setEnabled(false);
boardMenuItem.setEnabled(false);
disconnectMenuItem.setEnabled(true);
localchessMenu.setEnabled(false);
} catch (IOException ex) {
System.out.println(ex);
}
try {
serversocket.close();
System.out.println("服务器关闭》》》》》》》");
} catch (IOException ioe) {
System.out.println(ioe);
}
}
MapSize(cm.getWidth(), cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("连接服务")) {
String input = null;
InetAddress ia;
input =
JOptionPane.showInputDialog(
this,
"请输入服务器的IP地址:",
"连接服务器",
JOptionPane.QUESTION_MESSAGE);
if (input != null) {
if (input.length() == 0)
JOptionPane.showMessageDialog(
this,
"错误!输入的IP地址不能为空",
"错误",
JOptionPane.ERROR_MESSAGE);
else {
try {
ia = InetAddress.getByName(input);
clientsocket = new Socket(ia, DEFAULT_PORT);
in = clientsocket.getInputStream();
out = clientsocket.getOutputStream();
this.isserver = false;
this.isnet = true;
this.istoken = false;
socketthread = new SocketThread(this, mp);
socketthread.start();
JOptionPane
.showMessageDialog(
this,
"连接建立成功 可以开始游戏!",
"连接成功",
JOptionPane.INFORMATION_MESSAGE) ;
serverMenuItem.setEnabled(false);
connectMenuItem.setEnabled(false);
disconnectMenuItem.setEnabled(true);
boardMenuItem.setEnabled(false);
localchessMenu.setEnabled(false);
} catch (UnknownHostException ex) {
System.out.println(ex);
JOptionPane.showConfirmDialog(
this,
"输入的IP有误,或输入的IP不可连接",
"错误",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(
this,
"连接建立失败 请重试",
" 失败",
JOptionPane.WARNING_MESSAGE);
}
}
}
MapSize(cm.getWidth(), cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("中断连接")) {
byte msg[] = new byte[2];
msg[0] = -1;
msg[1] = -1;
try {
System.out.write(msg);
} catch (IOException ex) {
System.out.println(ex);
}
try {
Thread.sleep(500);
socketthread.StopRun();
this.isnet = false;
JOptionPane.showMessageDialog(
this,
"连接中断",
"网络中断",
JOptionPane.ERROR_MESSAGE);
serverMenuItem.setEnabled(true);
connectMenuItem.setEnabled(true);
disconnectMenuItem.setEnabled(false);
boardMenuItem.setEnabled(true);
localchessMenu.setEnabled(true);
} catch (InterruptedException ito) {
System.out.println(ito);
}
MapSize(cm.getWidth(), cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if (arg.equals("开局")) {
if (!this.isnet)
restart();
else {
byte msg[] = new byte[2];
msg[0] = -1;
msg[1] = 0;
try {
out.write(msg);
System.out.println("开局信息发送出>>>>>");
} catch (IOException ex) {
System.out.println(ex);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ito) {
System.out.println(ito);
}
if (this.isserver)
this.istoken = true;
else
this.istoken = false;
restart();
}
}
if (arg.equals("音乐")) {
if (backsound.hasPlayed) {
backsound.stop();
} else {
backsound.loop();
}
}
if (arg.equals("关于"))
JOptionPane.showMessageDialog(
this,
"五子棋",
"关于",
JOptionPane.INFORMATION_MESSAGE);
if (arg.equals("退出"))
System.exit(0);
}
public InputStream getin() {
return in;
}
public OutputStream getout() {
return out;
}
public InputStream in ;
public OutputStream out;
public Socket getsocket() {
return clientsocket;
}
public void setEnabledMenuItems() {
serverMenuItem.setEnabled(true);
connectMenuItem.setEnabled(true);
disconnectMenuItem.setEnabled(false);
boardMenuItem.setEnabled(true);
localchessMenu.setEnabled(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -