-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternetSpeedMeterFrame.java
More file actions
187 lines (160 loc) · 6.95 KB
/
Copy pathInternetSpeedMeterFrame.java
File metadata and controls
187 lines (160 loc) · 6.95 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class InternetSpeedMeterFrame extends JFrame {
private boolean isRunning = false;
private final SpeedMeterPanel speedMeter;
private final JLabel statusLabel;
private final JFrame startPageFrame;
public InternetSpeedMeterFrame(JFrame startPageFrame) {
super("Internet Speed Checker");
this.startPageFrame = startPageFrame;
setResizable(false);
setSize(500, 500);
getContentPane().setBackground(Color.BLACK);
setLayout(new BorderLayout());
// Panel for Heading and Description
JPanel textPanel = new JPanel(new GridLayout(3, 1)); // Changed to 3,1 to match other frames
textPanel.setBackground(Color.BLACK);
JLabel heading = new JLabel("INTERNET SPEED CHECKER", SwingConstants.CENTER);
heading.setFont(new Font("Monospaced", Font.BOLD, 24));
heading.setForeground(Color.WHITE);
// Added an empty label to maintain spacing consistency with other frames
JLabel emptyLabel = new JLabel("Click START to begin test", SwingConstants.CENTER);
emptyLabel.setFont(new Font("Arial", Font.BOLD, 16));
emptyLabel.setForeground(Color.YELLOW);
statusLabel = new JLabel("", SwingConstants.CENTER);
statusLabel.setFont(new Font("Arial", Font.BOLD, 16)); // Updated to match other frames
statusLabel.setForeground(Color.GREEN); // Changed to green to match other frames
textPanel.add(heading);
textPanel.add(emptyLabel);
textPanel.add(statusLabel);
// Speed Meter Panel
speedMeter = new SpeedMeterPanel(statusLabel);
speedMeter.setBackground(Color.BLACK);
speedMeter.setPreferredSize(new Dimension(400, 200)); // Updated to match other frames
// Button Panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
buttonPanel.setBackground(Color.BLACK);
// Start Button
JButton startButton = new JButton("START");
startButton.setFocusable(false);
startButton.setBackground(Color.GREEN);
startButton.setForeground(Color.BLACK);
startButton.setFont(new Font("Arial", Font.BOLD, 20));
// Back Button
JButton backButton = new JButton("BACK");
backButton.setFocusable(false);
backButton.setBackground(Color.RED);
backButton.setForeground(Color.WHITE);
backButton.setFont(new Font("Arial", Font.BOLD, 20));
// Start Button Action
startButton.addActionListener(e -> {
if (!isRunning) {
isRunning = true;
emptyLabel.setText("Testing connection speed...");
emptyLabel.setForeground(Color.YELLOW);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
while (isRunning) {
int pingValue = getPing();
SwingUtilities.invokeLater(() -> {
speedMeter.setSpeed(pingValue);
if (!statusLabel.getText().contains("No Internet")) {
emptyLabel.setText("Test running");
emptyLabel.setForeground(Color.GREEN);
} else {
emptyLabel.setText("Connection error. Please try again.");
emptyLabel.setForeground(Color.RED);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
});
}
});
// Back Button Action
backButton.addActionListener(e -> {
isRunning = false;
dispose();
startPageFrame.setVisible(true);
});
buttonPanel.add(startButton);
buttonPanel.add(backButton);
add(textPanel, BorderLayout.NORTH);
add(speedMeter, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private static int getPing() {
try {
String ipAddress = "8.8.8.8";
InetAddress inet = InetAddress.getByName(ipAddress);
long startTime = System.nanoTime();
boolean reachable = inet.isReachable(5000);
long endTime = System.nanoTime();
if (reachable) {
return (int) ((endTime - startTime) / 1000000);
} else {
return 500;
}
} catch (IOException e) {
return 500;
}
}
}
class SpeedMeterPanel extends JPanel {
private double angle = 180;
private int ping = 0;
private final JLabel statusLabel;
public SpeedMeterPanel(JLabel statusLabel) {
this.statusLabel = statusLabel;
setPreferredSize(new Dimension(400, 300));
}
public void setSpeed(int ping) {
this.ping = Math.max(0, ping);
int maxPing = 300;
this.angle = 180 - ((Math.min(ping, maxPing) * 180.0) / maxPing);
repaint();
if (ping >= 500) {
statusLabel.setText("No Internet Connection");
statusLabel.setForeground(Color.RED);
} else {
statusLabel.setText(ping + " ms");
statusLabel.setForeground(Color.GREEN);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2 + 50;
int radius = 100;
// Drawing semi-circle
g2.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(3));
g2.drawArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, 0, 180);
// Labels for scale
g2.setFont(new Font("Arial", Font.BOLD, 14));
g2.drawString("0", centerX - radius - 15, centerY + 5);
g2.drawString("150", centerX - 20, centerY - radius - 5);
g2.drawString("300+", centerX + radius - 10, centerY + 5);
// needle
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(2));
double radian = Math.toRadians(angle);
int needleX = centerX + (int) (radius * Math.cos(radian));
int needleY = centerY - (int) (radius * Math.sin(radian));
g2.drawLine(centerX, centerY, needleX, needleY);
}
}