diff --git a/src/j7/SeaGameClient.java b/src/j7/SeaGameClient.java index 602c4a3..b0614d4 100644 --- a/src/j7/SeaGameClient.java +++ b/src/j7/SeaGameClient.java @@ -32,11 +32,21 @@ import javax.swing.JPanel; import javax.swing.JTextField; -public class SeaGameClient implements Runnable { +public class SeaGameClient implements Runnable, ActionListener{ + // mainメソッド + // SeaGameClientを起動します + public static void main(String[] arg){ + new SeaGameClient(); + } + + // up/down/left/rightが押されたときの処理:ボタンラベルの文字列をコマンドとして送信 + public void actionPerformed(ActionEvent e){ + sendCommand(e.getActionCommand()); + } + JFrame f;// クライアント情報表示用ウィンドウ JPanel p;// 上下左右の移動ボタンと海の状態を表示するパネル Canvas c;// 海の状態を表示するキャンバス - //--------------------------------------------------------------------- // GUI 画面の初期配置 public SeaGameClient () { @@ -47,42 +57,22 @@ // upボタンの作成 b = new JButton("up"); - b.addActionListener(new ActionListener(){ - // upボタンが押されたらupコマンドを送信 - public void actionPerformed(ActionEvent e){ - sendCommand("up"); - } - }); + b.addActionListener(this); p.add(b, BorderLayout.NORTH); // leftボタンの作成 b = new JButton("left"); - b.addActionListener(new ActionListener(){ - // leftボタンが押されたらleftコマンドを送信 - public void actionPerformed(ActionEvent e){ - sendCommand("left"); - } - }); + b.addActionListener(this); p.add(b, BorderLayout.WEST); // rightボタンの作成 b = new JButton("right"); - b.addActionListener(new ActionListener(){ - // rightボタンが押されたらrightコマンドを送信 - public void actionPerformed(ActionEvent e){ - sendCommand("right"); - } - }); + b.addActionListener(this); p.add(b, BorderLayout.EAST); // downボタンの作成 b = new JButton("down"); - b.addActionListener(new ActionListener(){ - // downボタンが押されたらdownコマンドを送信 - public void actionPerformed(ActionEvent e){ - sendCommand("down"); - } - }); + b.addActionListener(this); p.add(b, BorderLayout.SOUTH); // 海上の様子を表示するCanvasを作成 @@ -101,7 +91,7 @@ if(server == null) login(); } }); - f.add(b,BorderLayout.NORTH); + f.add(b, BorderLayout.NORTH); // フレームfにlogoutボタンの取り付け b = new JButton("logout"); @@ -110,7 +100,7 @@ logout(); } }); - f.add(b,BorderLayout.SOUTH); + f.add(b, BorderLayout.SOUTH); // フレームfを表示します f.setSize(335,345); @@ -121,10 +111,10 @@ Thread thread = null; public void run(){ while (thread != null){ - System.out.println("client "+name); + System.out.println("client "+playerName); try { Thread.sleep(500); - }catch(Exception e){ + } catch(Exception e){ } // repaintメソッドを用いて,サーバ上の情報を画面に出力します repaint(); @@ -171,7 +161,7 @@ int port = 9999; // 接続ポート BufferedReader in; // 入力ストリーム DataOutputStream out; // 出力ストリーム - String name; // ゲーム参加者の名前 + String playerName; // ゲーム参加者の名前 //--------------------------------------------------------------------- // サーバへのログイン処理 @@ -179,12 +169,12 @@ System.out.println("realLogin "+host+" "+name); try { // サーバとの接続 - this.name = name; + this.playerName = name; server = new Socket(host, port); in = new BufferedReader(new InputStreamReader(server.getInputStream())); out = new DataOutputStream(server.getOutputStream()); - Thread.sleep(1000); + Thread.sleep(500); // loginコマンドの送付 out.writeBytes("login " + name+"\n"); System.out.println("sent login name "+name); @@ -203,7 +193,6 @@ out.flush(); server.close(); }catch (Exception e){ - ; } System.exit(0); } // end of logout @@ -213,7 +202,6 @@ // サーバにstatコマンドを送付し,盤面の様子などの情報を取得 try { out.writeBytes("stat\n"); - out.flush(); } catch (IOException e1) { e1.printStackTrace(); } @@ -237,9 +225,8 @@ StringTokenizer st = new StringTokenizer(line); String obj_name = st.nextToken().trim(); // 名前を読み取ります - // 自分の船は赤(red)でし,他人の船は緑(green)で表示 - if (obj_name.equals(name)) //自分の船 + if (obj_name.equals(playerName)) //自分の船 g.setColor(Color.red); else // 他人の船 g.setColor(Color.green); @@ -283,28 +270,14 @@ } } // end of repaint //--------------------------------------------------------------------- - // サーバへコマンドを送信 - void sendCommand(String s){ - System.out.println(s); + // サーバへコマンドを送信(up/down/left/right) + void sendCommand(String command){ try { - if ("up".equals(s)){ - out.writeBytes("up\n"); - }else if ("down".equals(s)){ - out.writeBytes("down\n"); - }else if ("left".equals(s)){ - out.writeBytes("left\n"); - }else if ("right".equals(s)){ - out.writeBytes("right\n"); - } + out.writeBytes(command+"\n"); out.flush(); } catch (IOException e) { e.printStackTrace(); } } - // mainメソッド - // SeaGameClientを起動します - public static void main(String[] arg){ - new SeaGameClient(); - } } // end of class SeaGameClient diff --git a/src/j7/SeaGameServer.java b/src/j7/SeaGameServer.java index ff0291f..d2ac1c1 100644 --- a/src/j7/SeaGameServer.java +++ b/src/j7/SeaGameServer.java @@ -9,6 +9,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; @@ -184,7 +185,8 @@ // および適当なタイミングでの燃料タンクの逐次追加処理 public static void main(String[] arg){ try {// サーバソケットの作成 - serverSocket = new ServerSocket(DEFAULT_PORT); + serverSocket = new ServerSocket(); + serverSocket.bind(new InetSocketAddress("0.0.0.0", DEFAULT_PORT)); }catch (IOException e){ System.err.println("can't create server socket."); System.exit(1); @@ -194,7 +196,7 @@ public void run(){ while(true){ try { - sleep(10000);// スレッドetを10000ミリ秒休止させます + sleep(10000);// スレッドetを10000ミリ秒(=10秒)休止させます }catch(InterruptedException e){ break; }