-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScreen.java
139 lines (116 loc) · 5 KB
/
MainScreen.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.List;
import java.net.*;
public class MainScreen extends JFrame {
private JTextField ipAddressField; // New field to input IP address
public MainScreen() {
setTitle("Main Screen");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null); // Center the window
setLayout(new BorderLayout());
// Panel for IP address input
JPanel ipPanel = new JPanel(new FlowLayout());
JLabel ipLabel = new JLabel("Server IP Address:");
ipAddressField = new JTextField(15);
ipPanel.add(ipLabel);
ipPanel.add(ipAddressField);
// Panel for buttons
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton button1 = new JButton("Get Astronomy Picture of the Day");
JButton button2 = new JButton("Calculate Sun's Position now");
JButton button3 = new JButton("Explore today's Astronomy News");
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
// Add action listeners to buttons
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
connectToAPODService();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openSolarPositionCalculator(ipAddressField.getText().trim());
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
connectToNewsScraperService();
}
});
// Add panels to frame
add(ipPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
}
private void connectToAPODService() {
try {
String ipAddress = ipAddressField.getText().trim(); // Get IP address from input field
Socket socket = new Socket(ipAddress, 12345);
// Send a request to the server
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject("APOD_REQUEST");
out.flush();
// Receive APOD data from the server
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
APOD apod = (APOD) in.readObject();
// Display received APOD data
displayAPOD(apod);
// Close resources
in.close();
out.close();
socket.close();
} catch (ConnectException e) {
JOptionPane.showMessageDialog(MainScreen.this, "Server not found. Please check the IP address.", "Connection Error", JOptionPane.ERROR_MESSAGE);
}catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(MainScreen.this, "Error fetching APOD data!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void connectToNewsScraperService() {
try {
String ipAddress = ipAddressField.getText().trim(); // Get IP address from input field
Socket socket = new Socket(ipAddress, 12346);
// Send a request to the server
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject("NEWS_REQUEST");
out.flush();
// Receive scraped headlines from the server
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
List<String> headlines = (List<String>) in.readObject();
// Display scraped headlines in a new window
NewsScraperGui newsScraperGui = new NewsScraperGui(headlines);
newsScraperGui.setVisible(true);
// Close resources
in.close();
out.close();
socket.close();
} catch (ConnectException e) {
JOptionPane.showMessageDialog(MainScreen.this, "Server not found. Please check the IP address.", "Connection Error", JOptionPane.ERROR_MESSAGE);
}catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(MainScreen.this, "Error fetching news!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void openSolarPositionCalculator(String ipAdd) {
SolarPositionCalculator calculator = new SolarPositionCalculator(ipAdd);
calculator.setVisible(true);
}
private void displayAPOD(APOD apod) {
// Create an ApodGui instance
ApodGui apodGui = new ApodGui();
apodGui.displayImage(apod.getHdurl());
apodGui.displayExplanation(apod.getExplanation());
apodGui.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainScreen().setVisible(true);
}
});
}
}