📄 mainwindow.java
字号:
import java.awt.*;//import javax.swing.*;import java.awt.event.*;import java.net.*;import java.io.*;public class MainWindow extends Frame implements Runnable{ Go panelGo=new Go(); Image myImage; int PORT; Socket sendSocket;//主动连接Socket PrintWriter writer;//用来发送message boolean stopFlag; boolean isInitiative; Point messagePoint; Point goStartPoint=null; Point yellowPoint=null; boolean stepColor=true; Point LastPoint=null;//移除黄点时,判断位置变动 BorderLayout borderLayout1 = new BorderLayout(); Panel panel1 = new Panel(); Panel panel2 = new Panel(); BorderLayout borderLayout2 = new BorderLayout(); Panel panel3 = new Panel(); CheckboxGroup checkboxGroup1 = new CheckboxGroup(); Checkbox checkbox1 = new Checkbox(); Checkbox checkbox2 = new Checkbox(); Label label1 = new Label(); TextField textField1 = new TextField(); Button button1 = new Button(); Label label2 = new Label(); Choice choice1 = new Choice(); Button button2 = new Button(); GridLayout gridLayout1 = new GridLayout(); BorderLayout borderLayout3 = new BorderLayout(); public MainWindow() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { choice1.setBackground(new Color(236, 190, 98)); button1.setBackground(new Color(236, 190, 98)); // try // { // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // }catch(Exception e){e.printStackTrace();} this.setResizable(false); new Thread(this).start();//启动监听线程 this.PORT=1976; this.isInitiative=false;//是否主动连接 this.stopFlag=false;//是否继续监听的标志 this.choice1.addItem("黑"); this.choice1.addItem("白"); LastPoint=new Point(); messagePoint=new Point(); this.setSize(470,450); this.setTitle("黑白分明 Beta1.0 作者:邹宇凯"); this.panelGo.setEnabled(false);//开始之前屏蔽掉盘面 checkbox1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { checkbox1_mouseClicked(e); } }); this.goStartPoint=this.panelGo.getLocation();// this.setLayout(borderLayout1); panel1.setLayout(borderLayout2); checkbox1.setCheckboxGroup(checkboxGroup1); checkbox1.setLabel("单机"); checkbox2.setCheckboxGroup(checkboxGroup1); checkbox2.setLabel("联机"); checkbox2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { checkbox2_mouseClicked(e); } }); label1.setText("对方地址"); button1.setLabel("连接"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); label2.setText(" "); button2.setBackground(new Color(236, 190, 98)); button2.setLabel("开始"); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); panel3.setLayout(gridLayout1); gridLayout1.setRows(8); gridLayout1.setColumns(1); gridLayout1.setHgap(100); gridLayout1.setVgap(10); panel2.setLayout(borderLayout3); this.panel2.setSize(500,70); panelGo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { panelGo_mouseMoved(e); } }); panelGo.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { panelGo_mouseClicked(e); } }); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); panel3.setBackground(new Color(236, 190, 98)); panel3.add(checkbox1, null); panel3.add(checkbox2, null); panel3.add(label1, null); panel3.add(textField1, null); panel3.add(button1, null); panel3.add(choice1, null); panel3.add(button2, null); panel3.add(label2, null); this.panel1.add(this.panelGo,BorderLayout.CENTER); this.panel1.add(panel3, BorderLayout.EAST); this.add(panel2, BorderLayout.SOUTH); this.add(panel1, BorderLayout.CENTER); this.disableLink();//废掉控件 //this.button2.setEnabled(false);//废掉开始 this.checkboxGroup1.setSelectedCheckbox(this.checkbox1); this.yellowPoint=new Point(1000,1000);//初始化一个世纪外的黄点 this.centerWindow(); this.show(); myImage=this.createImage(16,16);//用来纪录有黄点之前的图像 } void centerWindow() { Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); int pX=(d.width-this.getWidth())/2; int pY=(d.height-this.getHeight())/2; this.setLocation(pX,pY); } public static void main(String args[]) { MainWindow main=new MainWindow(); } //监听线程 public void run() { try { ServerSocket serverSocket=new ServerSocket(PORT); Socket receiveSocket=null; receiveSocket=serverSocket.accept(); if(this.isInitiative)//如果已在进行,则不接受连接 this.stopFlag=true; this.checkboxGroup1.setSelectedCheckbox(this.checkbox2);//自动选择联机 this.button1.setEnabled(false); this.choice1.setEnabled(true); this.textField1.setEnabled(false); this.checkbox1.setEnabled(false); this.checkbox2.setEnabled(false); this.writer=new PrintWriter(receiveSocket.getOutputStream(),true); BufferedReader reader=new BufferedReader(new InputStreamReader(receiveSocket.getInputStream())); String message; while(!this.stopFlag) { this.panelGo.showError("接收连接成功"); message=reader.readLine(); this.doMessage(message); } reader.close(); receiveSocket.close(); serverSocket.close(); }catch(IOException ioe){this.panelGo.showError("意外中断");} } //处理接收到的东东 void doMessage(String message) { if(message.startsWith("start"))//判断开始 { this.panelGo.showError("对方已开始"); if(message.equals("start_black")) this.choice1.select("白"); else this.choice1.select("黑"); if(this.choice1.getSelectedItem().equals("黑"))//只要你是黑的,就先走 this.panelGo.setEnabled(true); this.paintMyColor();//表明颜色 this.disableLink(); } else//下子的信息 { int color=Integer.parseInt(message.substring(0,1)); this.messagePoint.x=Integer.parseInt(message.substring(1,3)); this.messagePoint.y=Integer.parseInt(message.substring(3,5)); this.panelGo.setEnabled(true);//解禁 this.panelGo.doStep(this.messagePoint,color); } } //为鼠标定位 void panelGo_mouseMoved(MouseEvent e) { Point realPoint=e.getPoint(); Point mousePoint=this.panelGo.getMousePoint(realPoint,this.goStartPoint); this.removeLastMousePoint(this.LastPoint,mousePoint); //this.LastPoint=mousePoint;大错误,使对象公用了一个地址 this.LastPoint.x=mousePoint.x; this.LastPoint.y=mousePoint.y; if(this.isPlace(mousePoint)) this.showMousePoint(mousePoint); } //加黄点的范围 boolean isPlace(Point p) { if(p.x>19||p.x<1||p.y<1||p.y>19) return false; int color; One one; one=(One)(this.panelGo.myHash.get(p)); color=one.color; if(color!=0) return false; return true; } void panelGo_mouseClicked(MouseEvent e) { if(this.isSingle()) { this.doSingle(); } else { this.doMultiple(); } } //开始 void button2_actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("开始")) { this.disableLink(); this.checkbox1.setEnabled(false); this.checkbox2.setEnabled(false); this.button2.setLabel("退出"); if(this.isSingle()) this.panelGo.setEnabled(true); else//联机版时 { if(this.choice1.getSelectedItem().equals("黑")) { this.writer.println("start_black"); } else this.writer.println("start_white"); } this.paintMyColor();//表明颜色 } else if(e.getActionCommand().equals("退出")) { this.dispose(); System.exit(0); } } //disable联机时用的控件 void disableLink() { this.textField1.setBackground(new Color(236, 190, 98)); this.textField1.setEnabled(false); this.choice1.setEnabled(false); this.button1.setEnabled(false); } //enable联机时的控件 void enableLink()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -