我一直在研究我的程序,我的一个问题与此类似。除了我想要保持不变的尺寸的组件用于图像。当我有适当的数据时,它会显示一张地图;当我有不正确的数据时,它会显示 jolly roger 标志的图像。
首先,我从链接中选择了答案,但似乎混合这样的布局设置会导致获取我的图像延迟大约 10 秒(之前大约一秒)。所以我删除了它,然后我按照那个链接上的最后一个答案(idk how to link to the anchor of his answer)。在那之后,同样严重的延迟发生了,所以我恢复了一切并再次运行程序,给出了同样的延迟!
谁能想到执行时间突然改变的原因?或者也许我应该优化我的代码,特别是在我的 getGeoLoc() 方法中?这是问题所在的课程:
public class ThreatPanel {
JPanel DarkPanel = new JPanel(); //panel to house all display components using GridBagLayout
public GridBagLayout gridBag = new GridBagLayout();
public GridBagConstraints gbc = new GridBagConstraints();
//Parameters for determining the threat level
final int TEST = 0;
final int TEST2 = 100;
final int TEST3 = 200;
//Variables used for finding the geo location and displaying the google map image
private String geoTester;
private String city;
private String region;
private String latitude;
private String longitude;
private Image img;
private ImageIcon icon;
URL geoLocRetriever;
Color severeColor = new Color(225,69,00); // a more distinguishable 'orange'
Color borderColor = new Color(235,235,235); // color used for each components border
//Target declarations.
JLabel Target = new JLabel("TARGET");
JLabel TargetServerData;
//Client declaration
JLabel ClientIdData = new JLabel("filler"); //filler
//ServerID declarations
JLabel ServerIdData;
//Attacker declarations.
JLabel Attacker = new JLabel("ATTACKER");
JLabel AttackerData;
//Geo Location declarations
JLabel GeoLocData;
JLabel GeoLocImg;
JLabel GeoLocLabel = new JLabel("LOCATION");
//Threat Level declarations.
JLabel ThreatLevel = new JLabel("THREAT");
JLabel ThreatLevelData;
//Number of Attacks declarations
JLabel NumberOfAttacks = new JLabel("ATTACKS");
JLabel NumberOfAttacksData;
//DUMMY
//JLabel dummy1 = new JLabel();
//JLabel dummy2 = new JLabel();
public ThreatPanel()
{
DarkPanel.setLayout(gridBag);
DarkPanel.setBackground(Color.BLACK);
//Create new JLabels for the data that is to be continuously updated
TargetServerData = new JLabel();
AttackerData = new JLabel();
ThreatLevelData = new JLabel();
NumberOfAttacksData = new JLabel();
ServerIdData = new JLabel();
GeoLocData = new JLabel();
GeoLocImg = new JLabel();
// Component settings. After each add(), GridBagConstraint (gbc) returns to default.
// gridx and gridy refer to the cells position, similar to Excel or HTML tables.
// weightx and weighty determines how to distribute space among columns and rows.
// gridheight and gridwidth define how many cells a component occupies.
// fill determines how the component will fill the display area.
//FIRST COLUMN//////////////
Target.setFont(new Font("Arial", Font.BOLD, 85));
Target.setForeground(Color.GREEN);
Target.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
Target.setBorder(BorderFactory.createMatteBorder(3, 3, 2, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(Target, gbc);
ClientIdData.setForeground(Color.WHITE);
ClientIdData.setFont(new Font("Arial", Font.BOLD, 100));
ClientIdData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 1;
ClientIdData.setBorder(BorderFactory.createMatteBorder(0, 3, 0, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(ClientIdData, gbc);
TargetServerData.setForeground(Color.WHITE);
TargetServerData.setFont(new Font("Arial", Font.BOLD, 110));
TargetServerData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 2;
TargetServerData.setBorder(BorderFactory.createMatteBorder(0, 3, 0, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(TargetServerData, gbc);
ServerIdData.setForeground(Color.WHITE);
ServerIdData.setFont(new Font("Arial", Font.BOLD, 100));
ServerIdData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 3;
ServerIdData.setBorder(BorderFactory.createMatteBorder(0, 3, 0, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(ServerIdData, gbc);
Attacker.setFont(new Font("Arial", Font.BOLD, 85));
Attacker.setForeground(Color.GREEN);
Attacker.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 4;
Attacker.setBorder(BorderFactory.createMatteBorder(2, 3, 2, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(Attacker, gbc);
AttackerData.setForeground(Color.RED);
AttackerData.setFont(new Font("Arial", Font.BOLD, 110));
AttackerData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 5;
AttackerData.setBorder(BorderFactory.createMatteBorder(0, 3, 0, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(AttackerData, gbc);
GeoLocData.setFont(new Font("Arial", Font.BOLD, 70));
GeoLocData.setForeground(Color.RED);
GeoLocData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 6;
GeoLocData.setBorder(BorderFactory.createMatteBorder(0, 3, 3, 2, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(GeoLocData, gbc);
//SECOND COLUMN/////////////////
ThreatLevel.setFont(new Font("Arial", Font.BOLD, 85));
ThreatLevel.setForeground(Color.GREEN);
ThreatLevel.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.0;
ThreatLevel.setBorder(BorderFactory.createMatteBorder(3, 0, 2, 3, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(ThreatLevel, gbc);
ThreatLevelData.setFont(new Font("Arial", Font.BOLD, 100));
ThreatLevelData.setForeground(Color.BLACK);
ThreatLevelData.setOpaque(true);
ThreatLevelData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 1;
gbc.gridy = 1;
ThreatLevelData.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 3, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(ThreatLevelData, gbc);
NumberOfAttacks.setFont(new Font("Arial", Font.BOLD, 85));
NumberOfAttacks.setForeground(Color.GREEN);
NumberOfAttacks.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 1;
gbc.gridy = 2;
NumberOfAttacks.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 3, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(NumberOfAttacks, gbc);
NumberOfAttacksData.setFont(new Font("Arial", Font.BOLD, 100));
NumberOfAttacksData.setForeground(Color.RED);
NumberOfAttacksData.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 1;
gbc.gridy = 3;
NumberOfAttacksData.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 3, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(NumberOfAttacksData, gbc);
GeoLocLabel.setFont(new Font("Arial", Font.BOLD, 85));
GeoLocLabel.setForeground(Color.GREEN);
GeoLocLabel.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 1;
gbc.gridy = 4;
GeoLocLabel.setBorder(BorderFactory.createMatteBorder(2, 0, 2, 3, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(GeoLocLabel, gbc);
GeoLocImg.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridheight = 2;
gbc.gridx = 1;
gbc.gridy = 5;
GeoLocImg.setBorder(BorderFactory.createMatteBorder(0, 0, 3, 3, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(GeoLocImg, gbc);
/*
//DUMMY ROW AND COLUMN used in order to preset fixed size for map component
dummy1.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridheight = 2;
gbc.gridx = 2;
gbc.gridy = 5;
gbc.insets = new Insets(300,0,0,0);
dummy1.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(dummy1, gbc);
dummy2.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 1;
gbc.gridy = 7;
gbc.insets = new Insets(0,500,0,0);
dummy2.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, borderColor));
gbc.fill = GridBagConstraints.BOTH;
DarkPanel.add(dummy2, gbc);
*/
MainDisplay.frame.add(DarkPanel);
}
public void getGeoLoc()
{
String locHolder;
try {
geoLocRetriever = new URL("http://ip-api.com/line/"+ MainDisplay.getAttackerIpHolder());
} catch (MalformedURLException e) {
e.printStackTrace();
}
InputStream stream = null;
try {
stream = geoLocRetriever.openStream(); //read from url
} catch (IOException e1) {
e1.printStackTrace();
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
if (stream != null) try {
final BufferedInputStream input = new BufferedInputStream(stream);
final byte[] reader = new byte[16384];
int r = 0;
while ((r = input.read(reader, 0, 16384)) != -1)
buffer.write(reader, 0, r);
buffer.flush();
} catch(IOException e) {
e.printStackTrace();
} finally {
if(stream != null) try {
stream.close();
} catch(IOException e) {
e.printStackTrace();
}
}
locHolder = new String(buffer.toByteArray());
String[] lines = locHolder.split("\n"); //arranges data from stream into an array
geoTester = lines[0];
if (geoTester == "success"){ //test the first returned line for success or failure to avoid null outputs in the panel
city = lines[5] + ",";
region = lines[4];
latitude = lines[7];
longitude = lines[8];
String temp;
temp="https://maps.googleapis.com/maps/api/staticmap?center=" +latitude +"," +longitude +"&zoom=7&size=500x300&markers=color:red|label:A|" +latitude +"," +longitude +"&sensor=false";
URL mapurl = null;
try {
mapurl = new URL(temp);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
img = ImageIO.read(mapurl);
} catch (IOException e) {
e.printStackTrace();
}
icon = new ImageIcon(img); //ImageIcon with the google map image
}
else if (geoTester == "fail");{ // if fail, the jlabel for the map will become error image
try {
img = ImageIO.read(new URL ("file:src/jollyroger.jpg"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
icon = new ImageIcon(img);
city = "To "; //to be used for error feedback
region = "Search"; //to be used for error feedback
}
}
public void ShowThreats(){
String targetEnd = MainDisplay.getTargetIpHolder();
if (targetEnd == null){
TargetServerData.setText("NULL VALUE");
}
else TargetServerData.setText(targetEnd);
String attackerEnd = MainDisplay.getAttackerIpHolder();
if(attackerEnd == null){
AttackerData.setText("No IP Stored");
}
else AttackerData.setText(attackerEnd);
String serverIdEnd = MainDisplay.getServerIdHolder();
ServerIdData.setText(serverIdEnd);
String numAttacksEnd = MainDisplay.getNumAttacksHolder();
if(numAttacksEnd == null){
NumberOfAttacksData.setText("N/A");
}
else NumberOfAttacksData.setText(numAttacksEnd);
int threatLevelEnd = MainDisplay.getThreatLevelHolder();
if ((threatLevelEnd > TEST ) && (threatLevelEnd < TEST2)){
ThreatLevelData.setText("WARNING");
ThreatLevelData.setForeground(Color.YELLOW);
}
else if ((threatLevelEnd > TEST2 ) && (threatLevelEnd < TEST3)){
ThreatLevelData.setText("SEVERE");
ThreatLevelData.setBackground(severeColor);
}
else if (threatLevelEnd > TEST3){
ThreatLevelData.setText("CRITICAL");
ThreatLevelData.setBackground(Color.RED);
}
else{
ThreatLevelData.setText("N/A");
ThreatLevelData.setBackground(Color.PINK);
}
getGeoLoc();
String geoLocEnd = city + region;
GeoLocData.setText(geoLocEnd);
GeoLocImg.setIcon(icon);
}
}