-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoDServerGUI.java
146 lines (126 loc) · 5.28 KB
/
DoDServerGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class DoDServerGUI extends JFrame {
private static final long serialVersionUID = -5056371061826781622L;
private DoDServerController controller;
private GameLogic game;
private MapGrid mapGrid;
private String hostName;
private JScrollPane scrollPane;
private JLabel portLabel;
public static void main(String[] args) {
new DoDServerController().startGame();
}
public DoDServerGUI(DoDServerController controller, GameLogic game, int port) {
//serverSocket = new ServerSocket(portNumber);
super("Dungeon of Doom Server");
this.controller = controller;
this.game = game;
this.hostName = getHostName();
portLabel = new JLabel("Port: " + port);
}
private String getHostName() {
String hostname;
try {
hostname = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e){
hostname = "Unknown hostname";
}
return hostname;
}
public void createAndShowGUI() {
//new Thread(new DoDServerClientListener(serverSocket, game)).start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.out.println("closing server...");
}
});
this.addComponentsToPane(this.getContentPane());
this.pack();
//https://stackoverflow.com/questions/2442599/how-to-set-jframe-to-appear-centered-regardless-of-monitor-resolution
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
this.setVisible(true);
}
public void updateMapGrid() {
char[][] currentMap = game.getPopulatedMap();
if (mapGrid != null)
mapGrid.insertCharMap(currentMap);
}
private void addComponentsToPane(final Container pane) {
char[][] map = game.getPopulatedMap();
mapGrid = new MapGrid(map.length, map[0].length, 30);
mapGrid.insertCharMap(map);
scrollPane = new JScrollPane(mapGrid);
JPanel header = new JPanel();
header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
JLabel title = new JLabel("<html><h2>DoD Server</h2></html>");
JPanel titleContainer = new JPanel();
titleContainer.add(title);
header.add(titleContainer);
JPanel hostAndPortContainer = new JPanel();
hostAndPortContainer.setLayout(new BoxLayout(hostAndPortContainer, BoxLayout.X_AXIS));
hostAndPortContainer.setBorder(new EmptyBorder(0, 50, 10, 50));
String host = "Hostname: " + hostName;
JLabel hostLabel = new JLabel(host);
hostAndPortContainer.add(hostLabel);
hostAndPortContainer.add(Box.createHorizontalGlue());
hostAndPortContainer.add(portLabel);
JButton editPort = new JButton("Edit port");
hostAndPortContainer.add(editPort);
editPort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newPortNumber = JOptionPane.showInputDialog("New port");
if (newPortNumber != null && !newPortNumber.equals("")) {
String result = controller.attemptServerStart(newPortNumber);
if (result != null) {
JOptionPane.showMessageDialog(pane, result, "Error", JOptionPane.ERROR_MESSAGE);
System.out.println(result);
}
}
}
});
header.add(hostAndPortContainer);
final JButton button = new JButton("Hide");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setText(button.getText().equals("Hide") ? "Show" : "Hide");
scrollPane.setVisible(!scrollPane.isVisible());
Dimension beforePackSize = getSize();
pack();
setSize(new Dimension(beforePackSize.width, getHeight()));
}
});
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int dialogResult = JOptionPane.showConfirmDialog (null, "Would you really like to quit?","Warning",JOptionPane.YES_NO_OPTION);
if(dialogResult == JOptionPane.YES_OPTION){
setVisible(false);
dispose();
controller.endGame();
}
}
});
header.add(button);
header.add(quitButton);
pane.add(header, BorderLayout.PAGE_START);
pane.add(scrollPane, BorderLayout.CENTER);
}
public void updatePortLabel(String newPort) {
portLabel.setText("Port: " + newPort);
}
}