| | |
Issue with MouseListener
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 12
Reputation:
Solved Threads: 0
Hey!!
I am trying to make a 2-player game; where one player plays after the next. I have used socket programming in the code; so that, when the client (player 1) clicks on the window, the co-ordinates are sent to the server (player 2) and vice-versa. (For now, both player screen are displayed on the same host.)
So, here's the issue: Suppose I click somewhere on the client's screen - lets call it click 1. The co-ordinates of click 1 are correctly sent to the server. Now suppose I click again on the client's screen (call it click 2) - ideally nothing should happen since it's not player 1's turn - and for now, it doesn't. So far, so good. Now, suppose I click on the server's screen (call it click 3) - click 3's co-ordinates are sent to the client correctly. However, now, without clicking anywhere at all, the client sends click 2's co-ordinates to the server!! I don't want the last part to happen - i.e. when it's not someone's turn to play, all the mouse clicks on that screen should be ignored.
I can't figure this out!! I am using removeMouseListener right after a click is done (provided its the right person's turn), and I am not calling the addMouseListener until after co-ordinates are received. So, why is this happening?!! I am not very good at jav coding, so even if it is a very dumb error, please be patient and let me know!! I've already spent hours trying to debug this, to no avail!!
Thanks,
Chand
I am trying to make a 2-player game; where one player plays after the next. I have used socket programming in the code; so that, when the client (player 1) clicks on the window, the co-ordinates are sent to the server (player 2) and vice-versa. (For now, both player screen are displayed on the same host.)
So, here's the issue: Suppose I click somewhere on the client's screen - lets call it click 1. The co-ordinates of click 1 are correctly sent to the server. Now suppose I click again on the client's screen (call it click 2) - ideally nothing should happen since it's not player 1's turn - and for now, it doesn't. So far, so good. Now, suppose I click on the server's screen (call it click 3) - click 3's co-ordinates are sent to the client correctly. However, now, without clicking anywhere at all, the client sends click 2's co-ordinates to the server!! I don't want the last part to happen - i.e. when it's not someone's turn to play, all the mouse clicks on that screen should be ignored.
I can't figure this out!! I am using removeMouseListener right after a click is done (provided its the right person's turn), and I am not calling the addMouseListener until after co-ordinates are received. So, why is this happening?!! I am not very good at jav coding, so even if it is a very dumb error, please be patient and let me know!! I've already spent hours trying to debug this, to no avail!!
Thanks,
Chand
•
•
Join Date: Nov 2009
Posts: 12
Reputation:
Solved Threads: 0
Here's the Code:
Java Syntax (Toggle Plain Text)
package game; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class Clientms2 extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; JFrame container; static InetAddress address; static Socket s; static String host = "localhost"; static BufferedOutputStream bos; static OutputStreamWriter osw; static BufferedInputStream bis; static InputStreamReader isr; static int port = 5656; static String message; static boolean ClientTurn_flag = true; //static MouseListener l; static int tempx, tempy; static int mouseX, mouseY; static StringBuffer x, y; public Clientms2() { super("Client 1: Player 1"); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); addMouseListener(this); } public static void main(String args[]) { Clientms2 obj = new Clientms2(); obj.setVisible(true); } @Override public void mouseClicked(MouseEvent e) { if (ClientTurn_flag) { mouseX = e.getX(); mouseY = e.getY(); removeMouseListener(this); try { address = InetAddress.getByName(host); s = new Socket(address, port); } catch (IOException e1) { e1.printStackTrace(); } System.out.println("CLIENT SENDS: (" + mouseX + ", " + mouseY + ")"); send(mouseX, mouseY); receive(); addMouseListener(this); } else { //do nothing: It is not the Client's turn to play } //ClientTurn_flag = !ClientTurn_flag; } public void receive() { if (ClientTurn_flag == false) { try { System.out.println("Client waiting to receive..."); do { bis = new BufferedInputStream(s.getInputStream()); isr = new InputStreamReader(bis, "US-ASCII"); } while(isr.ready() == true); x = new StringBuffer(); y = new StringBuffer(); while((tempx = isr.read()) != 13) x.append((char) tempx); String str = x.toString(); mouseX = Integer.parseInt(str); while((tempy = isr.read()) != 13) y.append((char) tempy); str = y.toString(); mouseY = Integer.parseInt(str); System.out.println("CLIENT CONSOLE, SERVER CLICKED: (" + mouseX + ", " + mouseY + ")"); } catch (IOException e) { } ClientTurn_flag = true; System.out.println("Receive() -> C's Flag: " + ClientTurn_flag); System.out.println("Waiting for Client's Mouse Click..."); } } private void send(int xcor, int ycor) { try { bos = new BufferedOutputStream(s.getOutputStream()); osw = new OutputStreamWriter(bos, "US-ASCII"); message = (Integer.toString(xcor)) + (char) 13; osw.write(message); osw.flush(); message = (Integer.toString(ycor)) + (char) 13; osw.write(message); osw.flush(); } catch (IOException e) { e.printStackTrace(); } ClientTurn_flag = false; System.out.println("Send() -> C's Flag: " + ClientTurn_flag); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }
Last edited by chandini.david; 23 Days Ago at 5:50 pm. Reason: Cleaned up the code
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
0
#3 23 Days Ago
•
•
•
•
I can't figure this out!! I am using removeMouseListener right after a click is done (provided its the right person's turn)
With that said, I'll try to help you debug the code you currently have, but I can't promise that I'll be able to fix whatever it is.
Last edited by BestJewSinceJC; 23 Days Ago at 9:06 pm.
Out.
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
1
#4 23 Days Ago
I looked through your code, and I don't see a reason why you should be continuously adding and removing the mouse listener. If there is no reason to do so, don't do it. (I.e. it adds nothing to your program's logic, so what use does it have? Your ClientTurn_flag takes care of whose turn it is; you don't need to remove the listener). Other than that I changed the placement of one of your statements, it will likely do nothing though . . If you post the Server's code as well, I will run it and I'll help you debug it.
Java Syntax (Toggle Plain Text)
package game; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class Clientms2 extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; JFrame container; static InetAddress address; static Socket s; static String host = "localhost"; static BufferedOutputStream bos; static OutputStreamWriter osw; static BufferedInputStream bis; static InputStreamReader isr; static int port = 5656; static String message; static boolean ClientTurn_flag = true; //static MouseListener l; static int tempx, tempy; static int mouseX, mouseY; static StringBuffer x, y; public Clientms2() { super("Client 1: Player 1"); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); addMouseListener(this); } public static void main(String args[]) { Clientms2 client = new Clientms2(); client.setVisible(true); } @Override public void mouseClicked(MouseEvent e) { if (ClientTurn_flag) { mouseX = e.getX(); mouseY = e.getY(); removeMouseListener(this); try { address = InetAddress.getByName(host); s = new Socket(address, port); } catch (IOException e1) { e1.printStackTrace(); } System.out.println("CLIENT SENDS: (" + mouseX + ", " + mouseY + ")"); send(mouseX, mouseY); //try this ClientTurn_flag = false; receive(); addMouseListener(this); } else { //do nothing: It is not the Client's turn to play } //ClientTurn_flag = !ClientTurn_flag; } public void receive() { if (ClientTurn_flag == false) { try { System.out.println("Client waiting to receive..."); do { bis = new BufferedInputStream(s.getInputStream()); isr = new InputStreamReader(bis, "US-ASCII"); } while(isr.ready() == true); x = new StringBuffer(); y = new StringBuffer(); while((tempx = isr.read()) != 13) x.append((char) tempx); String str = x.toString(); mouseX = Integer.parseInt(str); while((tempy = isr.read()) != 13) y.append((char) tempy); str = y.toString(); mouseY = Integer.parseInt(str); System.out.println("CLIENT CONSOLE, SERVER CLICKED: (" + mouseX + ", " + mouseY + ")"); } catch (IOException e) { } ClientTurn_flag = true; System.out.println("Receive() -> C's Flag: " + ClientTurn_flag); System.out.println("Waiting for Client's Mouse Click..."); } } private void send(int xcor, int ycor) { try { bos = new BufferedOutputStream(s.getOutputStream()); osw = new OutputStreamWriter(bos, "US-ASCII"); message = (Integer.toString(xcor)) + (char) 13; osw.write(message); osw.flush(); message = (Integer.toString(ycor)) + (char) 13; osw.write(message); osw.flush(); } catch (IOException e) { e.printStackTrace(); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }
Out.
•
•
Join Date: Nov 2009
Posts: 12
Reputation:
Solved Threads: 0
0
#5 23 Days Ago
Heyy! Thanks a lot for taking the time to go through it!!
Here's the server side code:
Here's the server side code:
Java Syntax (Toggle Plain Text)
package game; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.*; import java.net.*; import javax.swing.*; public class Serverms2 extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; JFrame container; static Socket s; static ServerSocket ss; static BufferedOutputStream bos; static OutputStreamWriter osw; static BufferedInputStream bis; static InputStreamReader isr; static StringBuffer x, y; static int port = 5656; static int ID, count = 0; static int tempx, tempy; static int mouseX, mouseY; static String message; static boolean ServerTurn_flag = false; public Serverms2() { super("Server: Player 2"); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); addMouseListener(this); } public static void main(String args[]) { Serverms2 obj = new Serverms2(); obj.setVisible(true); try { ss = new ServerSocket(port); } catch (IOException e1) { e1.printStackTrace(); } if(ServerTurn_flag == false) { try { s = ss.accept(); bis = new BufferedInputStream(s.getInputStream()); isr = new InputStreamReader(bis, "US-ASCII"); x = new StringBuffer(); y = new StringBuffer(); while((tempx = isr.read()) != 13) x.append((char) tempx); String str = x.toString(); mouseX = Integer.parseInt(str); while((tempy = isr.read()) != 13) y.append((char) tempy); str = y.toString(); mouseY = Integer.parseInt(str); System.out.println("SERVER CONSOLE, CLIENT CLICKED: (" + mouseX + ", " + mouseY + ")"); } catch (IOException e) { e.printStackTrace(); } ServerTurn_flag = true; } } @Override public void mouseClicked(MouseEvent e) { if(ServerTurn_flag) { mouseX = e.getX(); mouseY = e.getY(); removeMouseListener(this); System.out.println("SERVER SENDS: (" + mouseX + ", " + mouseY + ")"); send(mouseX, mouseY); receive(); addMouseListener(this); } else { //do nothing: It is not the Server's turn to play } } public void receive() { try { if (ServerTurn_flag == false) { System.out.println("Server waiting to receive..."); s = ss.accept(); bis = new BufferedInputStream(s.getInputStream()); isr = new InputStreamReader(bis, "US-ASCII"); x = new StringBuffer(); y = new StringBuffer(); while((tempx = isr.read()) != 13) x.append((char) tempx); String str = x.toString(); mouseX = Integer.parseInt(str); while((tempy = isr.read()) != 13) y.append((char) tempy); str = y.toString(); mouseY = Integer.parseInt(str); System.out.println("SERVER CONSOLE, CLIENT CLICKED: (" + mouseX + ", " + mouseY + ")"); ServerTurn_flag = true; } } catch (IOException e) { } System.out.println("Receive() -> Serv's Flag: " + ServerTurn_flag); System.out.println("Waiting for Server's Mouse Click..."); } private void send(int xcor, int ycor) { try { bos = new BufferedOutputStream(s.getOutputStream()); osw = new OutputStreamWriter(bos, "US-ASCII"); message = (Integer.toString(xcor)) + (char) 13; osw.write(message); osw.flush(); //System.out.println(message); message = (Integer.toString(ycor)) + (char) 13; osw.write(message); osw.flush(); bos.flush(); //System.out.println(message); } catch (IOException e) { e.printStackTrace(); } ServerTurn_flag = false; System.out.println("Send() -> Serv's Flag: " + ServerTurn_flag); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
0
#8 23 Days Ago
I found the problem. Your code is way more complex than it needs to be. Essentially all you *need* in the client is the "clientTurnFlag", the send method, and the receive method. Initially in your main method you don't do anything except for opening the client's window and setting it visible. Your problem was basically setting the flags to false at the incorrect times. Here is your (much updated) code. I removed a lot of unnecessary code. There are logically only a few places where you should be setting your flags to true and false. Btw, you only need to accept the socket once - you shouldn't be accepting the same connection over and over again on your ServerSocket.
And server:
Java Syntax (Toggle Plain Text)
package game; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class Clientms2 extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; JFrame container; static InetAddress address; static Socket s; static String host = "localhost"; static BufferedOutputStream bos; static OutputStreamWriter osw; static BufferedInputStream bis; static InputStreamReader isr; static int port = 5656; static String message; static boolean ClientTurn_flag = true; //static MouseListener l; static int tempx, tempy; static int mouseX, mouseY; static StringBuffer x, y; public Clientms2() { super("Client 1: Player 1"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); addMouseListener(this); try { address = InetAddress.getByName(host); s = new Socket(address, port); } catch(Exception e){System.out.println("You fail at socket programs sucka!!!");} } public static void main(String args[]) { Clientms2 client = new Clientms2(); client.setVisible(true); } @Override public void mouseClicked(MouseEvent e) { if (ClientTurn_flag) { mouseX = e.getX(); mouseY = e.getY(); send(mouseX, mouseY); ClientTurn_flag = false; System.out.println("End of client's turn, client waiting to receive."); receive(); } } public void receive() { try { do { bis = new BufferedInputStream(s.getInputStream()); isr = new InputStreamReader(bis, "US-ASCII"); } while(isr.ready() == true); x = new StringBuffer(); y = new StringBuffer(); while((tempx = isr.read()) != 13) x.append((char) tempx); String str = x.toString(); mouseX = Integer.parseInt(str); while((tempy = isr.read()) != 13) y.append((char) tempy); str = y.toString(); mouseY = Integer.parseInt(str); } catch (IOException e) { } ClientTurn_flag = true; } private void send(int xcor, int ycor) { try { bos = new BufferedOutputStream(s.getOutputStream()); osw = new OutputStreamWriter(bos, "US-ASCII"); message = (Integer.toString(xcor)) + (char) 13; osw.write(message); osw.flush(); message = (Integer.toString(ycor)) + (char) 13; osw.write(message); osw.flush(); } catch (IOException e) { e.printStackTrace(); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }
And server:
Java Syntax (Toggle Plain Text)
package game; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.*; import java.net.*; import javax.swing.*; public class Serverms2 extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; JFrame container; static Socket s; static ServerSocket ss; static BufferedOutputStream bos; static OutputStreamWriter osw; static BufferedInputStream bis; static InputStreamReader isr; static StringBuffer x, y; static int port = 5656; static int ID, count = 0; static int tempx, tempy; static int mouseX, mouseY; static String message; static boolean ServerTurn_flag = false; public Serverms2() { super("Server: Player 2"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); addMouseListener(this); try { ss = new ServerSocket(port); } catch (IOException e1) { e1.printStackTrace(); } } public static void main(String args[]) { Serverms2 server = new Serverms2(); server.setVisible(true); try { s = ss.accept(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } server.receive(); } @Override public void mouseClicked(MouseEvent e) { if(ServerTurn_flag) { mouseX = e.getX(); mouseY = e.getY(); //System.out.println("SERVER SENDS: (" + mouseX + ", " + mouseY + ")"); send(mouseX, mouseY); System.out.println("End of server's turn, server waiting to receive."); ServerTurn_flag = false; receive(); } } public void receive() { try { bis = new BufferedInputStream(s.getInputStream()); isr = new InputStreamReader(bis, "US-ASCII"); x = new StringBuffer(); y = new StringBuffer(); while((tempx = isr.read()) != 13) x.append((char) tempx); String str = x.toString(); mouseX = Integer.parseInt(str); while((tempy = isr.read()) != 13) y.append((char) tempy); str = y.toString(); mouseY = Integer.parseInt(str); } catch (IOException e) { } ServerTurn_flag = true; } private void send(int xcor, int ycor) { try { bos = new BufferedOutputStream(s.getOutputStream()); osw = new OutputStreamWriter(bos, "US-ASCII"); message = (Integer.toString(xcor)) + (char) 13; osw.write(message); osw.flush(); //System.out.println(message); message = (Integer.toString(ycor)) + (char) 13; osw.write(message); osw.flush(); bos.flush(); //System.out.println(message); } catch (IOException e) { e.printStackTrace(); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }
Last edited by BestJewSinceJC; 23 Days Ago at 12:18 am.
Out.
•
•
Join Date: Nov 2009
Posts: 12
Reputation:
Solved Threads: 0
0
#9 23 Days Ago
Hello!!
Thanks for trying to help, but i'm afraid it still doesn't work!
Here's what's happening: I clicked in the client window, and "End of client's turn, client waiting to receive." is printed on the client console.
Then, I click again a couple of times on the client window - nothing is printed. Okay, that's good.
After that, however, when I click on the server screen again, along with printing "End of server's turn, server waiting to receive." on the server console, it also prints "End of client's turn, client waiting to receive." on the client console!! I added some more printf's and the client is actually sending the co-ords of the location of the first of the series of clicks I did out of turn.
I've gone over the code many, many times now, and I just don't see why it still doesn't work!!
Thanks,
Chand
Thanks for trying to help, but i'm afraid it still doesn't work!

Here's what's happening: I clicked in the client window, and "End of client's turn, client waiting to receive." is printed on the client console.
Then, I click again a couple of times on the client window - nothing is printed. Okay, that's good.
After that, however, when I click on the server screen again, along with printing "End of server's turn, server waiting to receive." on the server console, it also prints "End of client's turn, client waiting to receive." on the client console!! I added some more printf's and the client is actually sending the co-ords of the location of the first of the series of clicks I did out of turn.
I've gone over the code many, many times now, and I just don't see why it still doesn't work!!
Thanks,
Chand
![]() |
Similar Threads
- Windows Explorer Issue: my "Address" bar is gone, how do I get it back? (Windows NT / 2000 / XP)
- Power boot up issue (Motherboards, CPUs and RAM)
- Getting Windows to see a SATA drive (Storage)
- BitchX + Terminal issue (Mac Software)
- Kernel Version of Mandrake and error message (*nix Software)
- Memory Issue's! Please Help!! (Motherboards, CPUs and RAM)
- Internet Explorer Connection Issue (Web Browsers)
- Netgear router issue...read this if you have one (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: Final Year Project! Please help!
- Next Thread: Java object persistance
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows






