Reputation: 561
I would like to do in java sth like this:
http://msdn.microsoft.com/pl-pl/library/c7ykbedk.aspx.
The program is waiting for the reply. I have one class which extends JFrame (PlayerWindow) and the second inner class extends JDialog (PlayerLogWindow). Another class (Player) contains main method and needs a written by user parameter in JDialog class. Using while(true) doesnt look right... Maybe sth like modal dialogs?
public class Player implements Runnable
{
private Socket socket;
private String host, nazwa;
private ObjectOutputStream output;
private ObjectInputStream input;
private static int port;
public Player(String host, int port) throws IOException
{
this.host = host;
this.port = port;
}
@Override
public void run()
{
String m1, m2;
try
{
socket = new Socket(host, port);
input = new ObjectInputStream(socket.getInputStream());
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
System.out.println("Client's host " + socket.getLocalAddress().getHostName());
nazwa = Communication.inString("Whats your name: ");
output.writeObject(nazwa);
do
{
m1 = (String) input.readObject();
System.out.println("\n" + "Data from server: " + m1);
m2 = Communication.inString("Write sth to server: ");
output.writeObject(nazwa + ": " + m2);
} while (!m2.equals("quit"));
output.close();
input.close();
socket.close();
} catch (Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args) throws IOException
{
PlayerWindow playerWindow = new PlayerWindow();
String host = InetAddress.getLocalHost().getHostName();
while(true)
{
port = playerWindow.port;
if (playerWindow.port != 0)
{
ShipServer server = new ShipServer(port, host);
Thread threadServer = new Thread(server);
threadServer.start();
break;
}
}
}
}
public class PlayerWindow extends JFrame
{
private PlayerLogWindow playerLog;
int port;
//GUI
private JMenuBar jMenuBar;
private JMenu jMenuFile, jMenuHelp;
private JMenuItem jMenuItemNewGame, jMenuItemExitGame, jMenuAbout;
public PlayerWindow()
{
createWindow();
createJMenu();
}
private void createWindow()
{
setTitle("Game");
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit t = getToolkit();
Dimension dim = t.getScreenSize();
setLocation(dim.width/2-400, dim.height/2-400);
pack();
setSize(1000, 800);
setVisible(true);
}
private void createJMenu()
{
JMenuBar jMenuBar = new JMenuBar();
setJMenuBar(jMenuBar);
jMenuFile = new JMenu("Start");
jMenuFile.setFont(new Font("Sansserif", Font.ROMAN_BASELINE, 18));
jMenuBar.add(jMenuFile);
jMenuItemNewGame = new JMenuItem("New game");
jMenuItemNewGame.setFont(new Font("Sansserif", Font.ROMAN_BASELINE, 18));
jMenuFile.add(jMenuItemNewGame);
jMenuFile.addSeparator();
jMenuItemExitGame = new JMenuItem("End");
jMenuItemExitGame.setFont(new Font("Sansserif", Font.ROMAN_BASELINE, 18));
jMenuFile.add(jMenuItemExitGame);
jMenuHelp = new JMenu("Help");
jMenuHelp.setFont(new Font("Sansserif", Font.ROMAN_BASELINE, 18));
jMenuBar.add(jMenuHelp);
jMenuAbout = new JMenuItem("About...");
jMenuAbout.setFont(new Font("Sansserif", Font.ROMAN_BASELINE, 18));
jMenuHelp.add(jMenuAbout);
playerWindowActionListener();
}
public void playerWindowActionListener()
{
jMenuItemNewGame.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
if(playerLog == null)
playerLog = new PlayerLogWindow();
playerLog.setVisible(true);
}
});
}
class PlayerLogWindow extends JDialog
{
//GUI
private JPanel jPanelMain;
private JButton jButtonConnect;
private JTextField jTextPort;
public PlayerLogWindow()
{
createWindow();
createComponents();
}
private void createWindow()
{
setTitle("Choose server");
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
Toolkit t = getToolkit();
Dimension dim = t.getScreenSize();
setLocation(dim.width/2-200, dim.height/2-200);
pack();
setSize(400, 200);
}
public void createComponents()
{
jPanelMain = new JPanel(new BorderLayout(5,5));
JLabel jLabelServer = new JLabel("Connect", JLabel.CENTER);
jLabelServer.setFont(new Font("Sanserif", Font.BOLD, 16));
JPanel jPanelPort = new JPanel(new FlowLayout());
JLabel jLabelPort = new JLabel("Port number", JLabel.CENTER);
jTextPort = new JTextField(10);
jPanelPort.add(jLabelPort);
jPanelPort.add(jTextPort);
jButtonConnect = new JButton("Start");
jPanelMain.add(jLabelServer, "North");
jPanelMain.add(jPanelPort);
jPanelMain.add(jButtonConnect, "South");
add(jPanelMain);
connectUser();
}
public void connectUser()
{
jButtonConnect.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
port = Integer.parseInt(jTextPort.getText());
dispose();
}
});
}
}
}
Upvotes: 0
Views: 4127
Reputation: 347184
What you really need is some way for the GUI to tell the connection manager that it can try and connect rather the trying to poll the UI. This allows you to disconnect/decouple the different portions of your program .
Start by creating the "connection" in a separate thread and then wait on a central lock. This will cause the thread to wait until it is notified, at which time it can make decision about what it should (try and connect or not).
In your main UI, once you've gathered the server properties, you would notify the global lock, which would wake up the connection thread
You might find Concurrency in Java of some use
Upvotes: 2