?? 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 + -