-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
162 lines (131 loc) · 3.45 KB
/
Server.java
File metadata and controls
162 lines (131 loc) · 3.45 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class Server extends JFrame {
static String yourname;
public Server(String title)
{
output = new TextArea (15,40);
output.setEditable (false);
output.setFont(f);
output.setForeground(Color.blue);
this.yourname="Farmer";
setTitle(title);
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu colorMenu = new JMenu("Color");
JMenu helpMenu = new JMenu("Help");
fileMenu.setMnemonic('F');
colorMenu.setMnemonic('C');
helpMenu.setMnemonic('H');
addMenuItem(fileMenu,exitAction = new FileAction("Exit"));
aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
addMenuItem(helpMenu,aboutAction = new AboutAction("About"));
menuBar.add(fileMenu);
menuBar.add(helpMenu);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
class AboutAction extends AbstractAction
{
JOptionPane opt;
String name;
public AboutAction(String Name)
{
this.name=Name;
}
public void actionPerformed(ActionEvent ae)
{
{
JOptionPane.showMessageDialog (opt,"FarmAssist\nCopyright","About FarmAssist",JOptionPane.INFORMATION_MESSAGE);
}
}
}
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
dispose();
System.exit(0);
}
super.processWindowEvent(e);
}
private JMenuItem addMenuItem(JMenu menu,Action action)
{
JMenuItem item = menu.add(action);
KeyStroke keyStroke = (KeyStroke)action.getValue("j");
if(keyStroke != null)
item.setAccelerator(keyStroke);
return item;
}
private JMenuBar menuBar = new JMenuBar();
private JMenuItem aboutItem;
class FileAction extends AbstractAction
{
public FileAction(String NAME,KeyStroke keyStroke)
{
super(NAME);
}
String name;
public FileAction(String name)
{
super(name);
this.name=name;
}
public void actionPerformed(ActionEvent e)
{
String name= (String)getValue(NAME);
if(name.equals(exitAction.getValue(NAME)))
{
dispose();
System.exit(0);
}
}
}
class ColorAction extends AbstractAction
{
public ColorAction(String name,Color color)
{
super(name);
this.color=color;
}
public void actionPerformed(ActionEvent e)
{
elementColor = color;
getContentPane().setBackground(color);
}
private Color color;
}
private AboutAction aboutAction;
private FileAction exitAction;
private Color elementColor;
private ColorAction redAction,yellowAction,greenAction,blueAction,magentaAction,cyanAction,blackAction,grayAction,darkGrayAction,pinkAction,orangeAction,whiteAction;
public static TextArea output;
Font f=new Font("SansSerif",Font.PLAIN,16);
public static void main (String args[]) throws IOException {
Server ServerWindow = new Server("FarmAssist: Server Window");
Toolkit theKit = ServerWindow.getToolkit();
Dimension wndSize = theKit.getScreenSize();
ServerWindow.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2);
ServerWindow.setVisible(true);
ServerWindow.getContentPane().add ("North", output);
ServerWindow.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
ServerWindow.pack();
if (args.length != 1)
throw new IllegalArgumentException ("Syntax: Server <port>");
int port = Integer.parseInt (args[0]);
String logins;
ServerSocket server = new ServerSocket (port);
while (true) {
Socket client = server.accept ();
Handler handler = new Handler (client,yourname);
handler.start ();
output.append ("\n Accepted from " + client.getInetAddress ()+"\n");
}
}
}