-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telegraph.java
103 lines (83 loc) · 2.55 KB
/
Telegraph.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
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
/*
* The main class from the telegraph project.
* Opens two windows on the screen and starts morse
* code exchange between them.
*/
public class Telegraph extends JFrame
implements ActionListener
{
private Telegraph otherStation;
private JTextField inputText;
private JTextArea codedText;
private JTextField receivedText;
private static final Font courier16 = new Font("Monospaced", Font.PLAIN, 16);
public Telegraph(String name)
{
super(name);
inputText = new JTextField("Enter a message", 30);
inputText.setFont(courier16);
inputText.selectAll();
inputText.addActionListener(this);
codedText = new JTextArea(4, 30);
codedText.setEditable(false);
codedText.setLineWrap(true);
codedText.setFont(courier16);
receivedText = new JTextField(30);
receivedText.setBackground(Color.yellow);
receivedText.setEditable(false);
receivedText.setFont(courier16);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
c.add(inputText);
c.add(codedText);
c.add(receivedText);
}
public void connect(Telegraph other)
{
otherStation = other;
}
public void send(String message)
{
inputText.setText("");
receivedText.setText("");
String code = MorseCode.encode(message);
codedText.setText("[" + code + "] >>>");
otherStation.receive(code);
}
public void receive(String code)
{
codedText.setText(">>> [" + code + "]");
String message = MorseCode.decode(code);
receivedText.setText(message);
}
public void actionPerformed(ActionEvent e)
{
send(inputText.getText());
}
/******************************************************************/
/*************** main ****************/
/******************************************************************/
public static void main(String[] args)
{
Telegraph new_york = new Telegraph("New York");
new_york.setBounds(50, 150, 300, 200);
new_york.setDefaultCloseOperation(EXIT_ON_CLOSE);
Telegraph london = new Telegraph("London");
london.setBounds(400, 300, 300, 200);
london.setDefaultCloseOperation(EXIT_ON_CLOSE);
london.connect(new_york);
new_york.connect(london);
MorseCode.start();
new_york.setVisible(true);
london.setVisible(true);
}
}