Results 1 to 7 of 7
  1. #1
    marcg11 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default Sockets and JFrames problem

    Hi, I'm new here and I have a problem about Java SE. The thing is that I'm coding a multiplayer chessgame through internet, before the game starts, there are some other JFrames (to choose ip, port, white or black pieces,..) until it reaches the Frame with the board and it's pieces.

    So the DatainputStream i read has to be in a loop becuase i don't know when the other user might make a move.

    Java Code:
    public class Main {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
              Intro i = new Intro();
        }
    }

    For example that's theMain class that gets an instance of the intro Frame. Then this intro Class when a button is pressed gets an instance of another Class called Menu which is used to fill out the port, ip, etc. Then with a button i calls the Game class that contains all of the code.

    Java Code:
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here             
          dispose();  // To dispose the last Frame             
          Game j;
          JFrame frame = new JFrame("Game - Server");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           j = new GameServer (frame);
           j.createAndShowGUI();
           j.connect();         
     }
    The connect method is simple:

    Java Code:
    server= new ServerSocket(5000);
    	s=servidor.accept();
    	os=s.getOutputStream();
    	is=s.getInputStream();
    			
    	dis=new DataInputStream(is);
           dos=new DataOutputStream(os);
            connection = true;
            while(connection){
                  // All the dis.reads...
            }

    But th problem is that when the Game class frame opens i doesn't show anything, but if I declare the variable 'j' inside the static void main in Main class it seems to word perfectly.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) You seem to be using JFrame in many places where a JDialog would be better.
    2) I think that you're tying up the Swing Event Dispatch Thread or EDT in an endless loop. Since this thread is the only one that Swing uses to interact with users and to draw the GUI widgets, tying it up will effectively freeze your program. You're probably going to have to use a background thread(s) such as a SwingWorker. Please have a look at this tutorial: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI with JFC/Swing)

    Much luck!

  3. #3
    marcg11 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks, I'll give it a look. Do you think it's becuase 1) or 2) ?

    About SwingWorker, jewh it's really out of my capacity. Do you know what should I add to my code in order to make it work?

    Oh god I only want an Idle function.
    Last edited by marcg11; 08-14-2009 at 07:27 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you think it's becuase 1) or 2) ?
    It's because of 2). 1) is more of a style issue.

    About SwingWorker, jewh it's really out of my capacity.
    I have to be honest by saying that I'm not a fan of Java students using NetBeans to generate GUI code. NetBeans hides a lot of the details happening behind the scenes in GUIs, and this is fine for the most basic of GUIs, but if you try to do anything more complex (such as what you are trying to do right now), these details become important and must be learned. If you are serious at all about learning Java Swing GUI coding, I strongly suggest that you go through the Sun Swing tutorials and learn to code Swing by hand. Then later once you're more familiar with Swing coding, by all means use NetBeans to help quickly create a GUI.

    Do you know what should I add to my code in order to make it work?
    You need a background thread for the background communication processes but you also need to take care to call Swing-manipulating code on the EDT. SwingWorker is probably an easier way to create a background thread that will work well with Swing programs.

  5. #5
    marcg11 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It's because of 2). 1) is more of a style issue.


    I have to be honest by saying that I'm not a fan of Java students using NetBeans to generate GUI code. NetBeans hides a lot of the details happening behind the scenes in GUIs, and this is fine for the most basic of GUIs, but if you try to do anything more complex (such as what you are trying to do right now), these details become important and must be learned. If you are serious at all about learning Java Swing GUI coding, I strongly suggest that you go through the Sun Swing tutorials and learn to code Swing by hand. Then later once you're more familiar with Swing coding, by all means use NetBeans to help quickly create a GUI.


    You need a background thread for the background communication processes but you also need to take care to call Swing-manipulating code on the EDT. SwingWorker is probably an easier way to create a background thread that will work well with Swing programs.
    So finally I've managed to make an idle with SwingWorker, it's infinite. I've put the typical DataInputStream.read() inside the idel but the Server aplication seems to have problems, like it's stuck, It doesn't let me click buttons, etc.... However the client aplication works fine once I'm connected.

    Oh and another thing, while the server aplication is waiting for the client to connect, the server aplication frame isn't painting anything. It's like if the frame was transparent because I see the other Windows behind.

    Hope you can help.
    Last edited by marcg11; 08-15-2009 at 01:09 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You still have a background process being called on the EDT. So either the SwingWorker has not been coded correctly, or you're not starting it correctly (by calling execute()). But without seeing your code I'm afraid that it's impossible to tell, and all we can do is guess.

    Best of luck

  7. #7
    marcg11 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    in this idle i only have all the readings. should i put the accept() method or it has to be outside the idle. i would post the code but i dont have internet at home.

    basically i have

    newserversocket(port)
    socket.accept()
    ...

    idle(
    datainputstream.read()
    )

    ps: if i delete the socket.accept() method the frame appears to run well, but of course client and server are not connected.


    Edit: Ok I have the code

    Java Code:
    @Override
        public void connect(){
            
             try{
                    bar = new JProgressBar();
    		server = new ServerSocket(this.port);
                    s=servidor.accept();
    		os=s.getOutputStream();
    		is=s.getInputStream();
    		dis=new DataInputStream(is);
    				dos=new DataOutputStream(os);
    
                  
                    if(worker==null){
    					worker = new MyWorker();
    					worker.addPropertyChangeListener(new PropertyChangeListener(){
    							public void propertyChange(PropertyChangeEvent evt) {
    								if ("progress".equals(evt.getPropertyName())){
                                                                        // Idle
                                                                        try{
                                                   
                                                                            for(int i = 0 ; i < 4 ; i++ ){
                                                                               if( intRecieved[i] == -1 ){ // 
                                                                                  intRecieved[i] = dis.read();
                                                                                }else{
                                                                                }
                                                                           }
    
                                                                        }catch(IOException e){
                                                                             System.out.println("Error d'I/O"+e.getMessage());
                                                                         }                                       
                                                 
                                                                    // More code ...
    						            }
                                        
    						 });
    				}
    		}
    		worker.execute();
                    
    
    	} catch (IOException e) {
    		System.out.println("Error d'I/O"+e.getMessage());
    	}
        }
    }
    
    
    
        class MyWorker extends SwingWorker<String, Void>{
    
    		@Override
    		protected String doInBackground() throws Exception { // I took that from an example, i added a line to be infinite
                            int i=0;
    
    			while(i<=100 && !isCancelled()){
    
    				bar.setValue(i);
    				setProgress(i);
    				i++;
                    if( i == 100) i = 0;
    				Thread.sleep(50);
    			}
                            
    			return "Idle";
    		}
    
    	}
    Last edited by marcg11; 08-15-2009 at 03:31 PM.

Similar Threads

  1. Problem (sockets)
    By Blacknight962 in forum New To Java
    Replies: 3
    Last Post: 08-16-2009, 01:56 AM
  2. problem with JFrames in different screens
    By petrosgraf in forum Java 2D
    Replies: 1
    Last Post: 04-17-2009, 08:04 PM
  3. jframes
    By ddj in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 04:15 PM
  4. problem with jframes
    By Robert in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2009, 04:05 AM
  5. problem with getting new JFrames
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 01:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •