-
Notifications
You must be signed in to change notification settings - Fork 17
/
Asteroidlist.java
97 lines (80 loc) · 3.22 KB
/
Asteroidlist.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
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import java.util.HashMap;
import java.util.List;
class AsteroidList {
private List<Asteroid> asteroids;
private int numberOfAsteroids;
JFrame asteroidList;
private JTable table;
// public static void main(String[] args) {
//
// AsteroidList window = new AsteroidList();
// window.asteroidList.setVisible(true);
// }
public AsteroidList(List<Asteroid> asteroids, int numberOfAsteroids) {
this.asteroids = asteroids;
this.numberOfAsteroids = numberOfAsteroids;
assert(asteroids.size() == numberOfAsteroids);
initFrame();
initDate();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 80, 458, 200);
asteroidList.getContentPane().add(scrollPane);
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(new String[] {
"Asteroid Name ", "Closest Approach (KM)", "Diameter (KM)"
});
table = new JTable(tableModel);
for (int i=0;i<asteroids.size(); i++ ) {
Asteroid asteroid = asteroids.get(i);
Object rowData[] = {asteroid.getName(), asteroid.getMissing_approach(), asteroid.getDiameter()};
tableModel.addRow(rowData);
}
HashMap<Integer, Integer> colWidths = getColWidths();
for (int i = 0; i < 3; i++) {
table.getColumnModel().getColumn(i).setResizable(false);
table.getColumnModel().getColumn(i).setPreferredWidth(colWidths.get(i));
}
scrollPane.setViewportView(table);
addHazardWarning();
}
// Initializes the frame by setting a title, layout, and other details.
public void initFrame() {
asteroidList = new JFrame();
asteroidList.setTitle("Asteroid List");
asteroidList.setBounds(0, 20, 500, 600);
asteroidList.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
asteroidList.getContentPane().setLayout(null);
getColWidths();
}
public HashMap<Integer, Integer> getColWidths() {
HashMap<Integer, Integer> colWidths = new HashMap<Integer, Integer>();
colWidths.put(0, 142);
colWidths.put(1, 147);
colWidths.put(2, 159);
return colWidths;
}
// Adds the date to the GUI.
public void initDate() {
String Date = new String("12/08/19");
JLabel date = new JLabel("DATE : "+ Date);
date.setBounds(78, 13, 304, 51);
date.setHorizontalAlignment(SwingConstants.CENTER);
date.setFont(new Font("Tahoma", Font.BOLD, 31));
asteroidList.getContentPane().add(date);
}
public void addHazardWarning() {
String hazard = new String("NONE");
JLabel hazardous = new JLabel("HAZARDOUS ASTEROIDS : " + hazard);
hazardous.setBounds(12, 354, 458, 81);
hazardous.setHorizontalAlignment(SwingConstants.CENTER);
hazardous.setFont(new Font("Tahoma", Font.BOLD, 20));
asteroidList.getContentPane().add(hazardous);
}
}