Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
521 lines (437 loc) · 20.2 KB
/
Copy pathMain.java
File metadata and controls
521 lines (437 loc) · 20.2 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
/**
* DiabetesRiskPredictor.java
* Main GUI application for diabetes risk prediction.
*/
public class Main extends JFrame {
// Palette -----------------------------------------------------
private static final Color BG_SIDEBAR = new Color(245, 248, 252);
private static final Color BG_MAIN = Color.WHITE;
private static final Color ACCENT_BLUE = new Color(52, 107, 193);
private static final Color TEXT_DARK = new Color(30, 40, 55);
private static final Color TEXT_MUTED = new Color(110, 120, 140);
private static final Color BORDER_COLOR = new Color(210, 218, 230);
private static final Color GREEN = new Color(22, 135, 90);
private static final Color AMBER = new Color(200, 120, 10);
private static final Color RED = new Color(185, 40, 40);
// == Input fields -----------------------------------------------------
private JTextField tfPregnancies, tfGlucose, tfBloodPressure,
tfSkinThickness, tfInsulin, tfBMI,
tfPedigree, tfAge, tfName;
// Result display -----------------------------------------------------
private JProgressBar riskBar;
private JLabel lblRiskPercent, lblRiskLevel;
private JTextArea taMessage;
// Domain objects
private final RiskCalculator calculator = new LogisticRiskCalculator();
private final PatientRecord records = new PatientRecord();
// -----------------------------------------------------
public Main() {
setTitle("Diabetes Risk Predictor"); //Titel
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // normal
setSize(1000, 760); //scale
setLocationRelativeTo(null);
setLayout(new BorderLayout(0, 0));
records.loadFromFile(); // load saved records on start
add(buildSidebar(), BorderLayout.WEST); //
add(buildMainPanel(), BorderLayout.CENTER);
setVisible(true);
}
// -----------------------------------------------------
// SIDEBAR
// -----------------------------------------------------
private JPanel buildSidebar() {
JPanel sidebar = new JPanel();
sidebar.setPreferredSize(new Dimension(220, 0));
sidebar.setBackground(BG_SIDEBAR);
sidebar.setLayout(new BoxLayout(sidebar, BoxLayout.Y_AXIS));
sidebar.setBorder(new CompoundBorder(
new MatteBorder(0, 0, 0, 1, BORDER_COLOR),
new EmptyBorder(20, 14, 20, 14)));
// App branding -----------------------------------------------------
JLabel appIcon = new JLabel("🏥");
appIcon.setFont(new Font("SansSerif", Font.PLAIN, 36));
appIcon.setAlignmentX(Component.LEFT_ALIGNMENT);
sidebar.add(appIcon); // adding the icon
sidebar.add(Box.createVerticalStrut(4));
JLabel appName = new JLabel("Diabetes Risk");
appName.setFont(new Font("SansSerif", Font.BOLD, 17));
appName.setForeground(ACCENT_BLUE);
appName.setAlignmentX(Component.LEFT_ALIGNMENT);
sidebar.add(appName);
JLabel appSub = new JLabel("Predictor"); //the suptitel
appSub.setFont(new Font("SansSerif", Font.PLAIN, 15));
appSub.setForeground(TEXT_MUTED);
appSub.setAlignmentX(Component.LEFT_ALIGNMENT);
sidebar.add(appSub);
sidebar.add(Box.createVerticalStrut(18));
sidebar.add(sidebarDivider());
// How It Works -----------------------------------------------------
sidebar.add(sidebarSectionTitle("How It Works"));
sidebar.add(Box.createVerticalStrut(4));
sidebar.add(sidebarText(
"- Enter your health measurements.\n " +" \n- The app applies a weighted " +
"statistical model to estimate your likelihood of diabetes."));
sidebar.add(Box.createVerticalStrut(14));
sidebar.add(sidebarDivider());
// Risk Levels -----------------------------------------------------
sidebar.add(sidebarSectionTitle("Risk Levels"));
sidebar.add(Box.createVerticalStrut(6));
sidebar.add(riskLegendRow("●", "Low ( < 30% )", GREEN));
sidebar.add(Box.createVerticalStrut(4));
sidebar.add(riskLegendRow("●", "Medium ( 30% – 70% )", AMBER));
sidebar.add(Box.createVerticalStrut(4));
sidebar.add(riskLegendRow("●", "High ( > 70% )", RED));
sidebar.add(Box.createVerticalStrut(14));
sidebar.add(sidebarDivider());
// Disclaimer -----------------------------------------------------
JTextArea disc = sidebarText(
"⚠️ This tool provides estimates only. It is NOT a substitute " +
"for professional medical advice. Always consult a doctor.");
disc.setForeground(new Color(160, 100, 20));
sidebar.add(disc);
sidebar.add(Box.createVerticalGlue());
// Record count -----------------------------------------------------
JLabel recLabel = new JLabel("Saved records: " + records.size());
recLabel.setFont(new Font("SansSerif", Font.PLAIN, 11));
recLabel.setForeground(TEXT_MUTED);
recLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
sidebar.add(recLabel);
return sidebar;
}
// -----------------------------------------------------
// MAIN PANEL
// -----------------------------------------------------
private JScrollPane buildMainPanel() {
JPanel main = new JPanel();
main.setBackground(BG_MAIN);
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(new EmptyBorder(24, 30, 24, 30));
// Title
JLabel title = new JLabel("Diabetes Risk Prediction");
title.setFont(new Font("SansSerif", Font.BOLD, 26));
title.setForeground(TEXT_DARK);
title.setAlignmentX(Component.LEFT_ALIGNMENT);
main.add(title);
JLabel subtitle = new JLabel("Enter your health details below to receive a personalised risk estimate.");
subtitle.setFont(new Font("SansSerif", Font.PLAIN, 13));
subtitle.setForeground(TEXT_MUTED);
subtitle.setAlignmentX(Component.LEFT_ALIGNMENT);
main.add(subtitle);
main.add(Box.createVerticalStrut(20));
main.add(buildInputPanel());
main.add(Box.createVerticalStrut(18));
main.add(buildResultsPanel());
main.add(Box.createVerticalStrut(18));
main.add(buildTipsPanel());
main.add(Box.createVerticalStrut(18));
JLabel footer = new JLabel("Developed for BIT1123 Object-Oriented Programming");
footer.setFont(new Font("SansSerif", Font.PLAIN, 11));
footer.setForeground(TEXT_MUTED);
footer.setAlignmentX(Component.LEFT_ALIGNMENT);
main.add(footer);
JScrollPane scroll = new JScrollPane(main);
scroll.setBackground(BG_MAIN);
scroll.getVerticalScrollBar().setUnitIncrement(12);
scroll.setBorder(null);
return scroll;
}
// Input Form -----------------------------------------------------
private JPanel buildInputPanel() {
JPanel wrapper = new JPanel();
wrapper.setBackground(BG_MAIN);
wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.Y_AXIS));
wrapper.setBorder(panelBorder("Your Health Information"));
wrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
// Name field (single row)
JPanel nameRow = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
nameRow.setBackground(BG_MAIN);
nameRow.add(fieldLabel("Patient Name "));
tfName = new JTextField(20);
tfName.setText(" ");
nameRow.add(tfName);
nameRow.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(nameRow);
wrapper.add(Box.createVerticalStrut(10));
// Grid: 4 rows × 4 cols (label, field, label, field)
JPanel grid = new JPanel(new GridLayout(4, 4, 14, 10));
grid.setBackground(BG_MAIN);
tfPregnancies = addField(grid, "Pregnancies");
tfGlucose = addField(grid, "Glucose (mg/dL)");
tfBloodPressure = addField(grid, "Blood Pressure (mmHg)");
tfSkinThickness = addField(grid, "Skin Thickness (mm)");
tfInsulin = addField(grid, "Insulin (μU/mL)");
tfBMI = addField(grid, "BMI");
tfPedigree = addField(grid, "Diabetes Pedigree");
tfAge = addField(grid, "Age (years)");
// Default sample values
tfPregnancies.setText(" ");
tfGlucose.setText(" ");
tfBloodPressure.setText(" ");
tfSkinThickness.setText(" ");
tfInsulin.setText(" ");
tfBMI.setText(" ");
tfPedigree.setText(" ");
tfAge.setText(" ");
grid.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(grid);
wrapper.add(Box.createVerticalStrut(14));
// Buttons row -----------------------------------------------------
JPanel btnRow = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
btnRow.setBackground(BG_MAIN);
JButton btnPredict = styledButton(" Predict Risk", ACCENT_BLUE);
btnPredict.addActionListener(e -> predict());
btnRow.add(btnPredict);
btnRow.add(Box.createHorizontalStrut(10));
JButton btnSave = styledButton(" Save Record", new Color(60, 130, 80));
btnSave.addActionListener(e -> saveCurrentRecord());
btnRow.add(btnSave);
btnRow.add(Box.createHorizontalStrut(10));
JButton btnReset = styledButton(" Reset", new Color(130, 130, 140));
btnReset.addActionListener(e -> resetFields());
btnRow.add(btnReset);
btnRow.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(btnRow);
return wrapper;
}
// Results Panel -----------------------------------------------------
private JPanel buildResultsPanel() {
JPanel wrapper = new JPanel();
wrapper.setBackground(BG_MAIN);
wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.Y_AXIS));
wrapper.setBorder(panelBorder("Prediction Result"));
wrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
lblRiskPercent = new JLabel(" ");
lblRiskPercent.setFont(new Font("SansSerif", Font.BOLD, 36));
lblRiskPercent.setForeground(TEXT_MUTED);
lblRiskPercent.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(lblRiskPercent);
wrapper.add(Box.createVerticalStrut(6));
riskBar = new JProgressBar(0, 100);
riskBar.setStringPainted(false);
riskBar.setPreferredSize(new Dimension(0, 16));
riskBar.setMaximumSize(new Dimension(Integer.MAX_VALUE, 16));
riskBar.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(riskBar);
wrapper.add(Box.createVerticalStrut(6));
lblRiskLevel = new JLabel("Run a prediction to see results");
lblRiskLevel.setFont(new Font("SansSerif", Font.PLAIN, 13));
lblRiskLevel.setForeground(TEXT_MUTED);
lblRiskLevel.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(lblRiskLevel);
wrapper.add(Box.createVerticalStrut(10));
taMessage = new JTextArea(2, 40);
taMessage.setEditable(false);
taMessage.setLineWrap(true);
taMessage.setWrapStyleWord(true);
taMessage.setFont(new Font("SansSerif", Font.BOLD, 14));
taMessage.setBackground(new Color(245, 248, 252));
taMessage.setBorder(new CompoundBorder(
new LineBorder(BORDER_COLOR, 1, true),
new EmptyBorder(10, 14, 10, 14)));
taMessage.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(taMessage);
return wrapper;
}
// Prevention Tips Panel -----------------------------------------------------
private JPanel buildTipsPanel() {
JPanel wrapper = new JPanel();
wrapper.setBackground(BG_MAIN);
wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.Y_AXIS));
wrapper.setBorder(panelBorder("Prevention Tips"));
wrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
StringBuilder sb = new StringBuilder();
for (HealthTip tip : HealthTip.getDefaultTips()) {
sb.append(tip.toString()).append("\n");
}
JTextArea taTips = new JTextArea(sb.toString().trim());
taTips.setEditable(false);
taTips.setFont(new Font("SansSerif", Font.PLAIN, 13));
taTips.setForeground(TEXT_DARK);
taTips.setBackground(new Color(240, 248, 240));
taTips.setBorder(new EmptyBorder(10, 14, 10, 14));
taTips.setAlignmentX(Component.LEFT_ALIGNMENT);
wrapper.add(taTips);
return wrapper;
}
// -----------------------------------------------------
// ACTIONS
// -----------------------------------------------------
private void predict() {
Patient patient = readFields();
if (patient == null) return; // validation failed
RiskResult result = calculator.calculate(patient);
updateResults(result);
}
private void saveCurrentRecord() {
Patient patient = readFields();
if (patient == null) return;
RiskResult result = calculator.calculate(patient);
boolean saved = records.addRecord(patient, result);
if (!saved) {
JOptionPane.showMessageDialog(this,
"Duplicate record detected!\nA patient with the same name and age already exists.",
"Duplicate", JOptionPane.WARNING_MESSAGE);
return;
}
JOptionPane.showMessageDialog(this,
"Record saved successfully! (" + records.size() + " total)",
"Saved", JOptionPane.INFORMATION_MESSAGE);
}
private void resetFields() {
tfName.setText(" ");
tfPregnancies.setText(" ");
tfGlucose.setText(" ");
tfBloodPressure.setText(" ");
tfSkinThickness.setText(" ");
tfInsulin.setText(" ");
tfBMI.setText(" ");
tfPedigree.setText(" ");
tfAge.setText(" ");
lblRiskPercent.setText(" ");
lblRiskPercent.setForeground(TEXT_MUTED);
riskBar.setValue(0);
riskBar.setForeground(ACCENT_BLUE);
lblRiskLevel.setText("Run a prediction to see results");
lblRiskLevel.setForeground(TEXT_MUTED);
taMessage.setText("");
taMessage.setBackground(new Color(245, 248, 252));
taMessage.setBorder(new CompoundBorder(
new LineBorder(BORDER_COLOR, 1, true),
new EmptyBorder(10, 14, 10, 14)));
}
// ── Parse input fields into a Patient object ──────────────────
private Patient readFields() {
try {
int pregnancies = Integer.parseInt(tfPregnancies.getText().trim());
double glucose = Double.parseDouble(tfGlucose.getText().trim());
double bp = Double.parseDouble(tfBloodPressure.getText().trim());
double skin = Double.parseDouble(tfSkinThickness.getText().trim());
double insulin = Double.parseDouble(tfInsulin.getText().trim());
double bmi = Double.parseDouble(tfBMI.getText().trim());
double pedigree = Double.parseDouble(tfPedigree.getText().trim());
int age = Integer.parseInt(tfAge.getText().trim());
String name = tfName.getText().trim().isEmpty() ? "Anonymous" : tfName.getText().trim();
return new Patient(name, age, glucose, bp, bmi, insulin, skin, pedigree, pregnancies);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,
"Please enter valid numbers in all fields.",
"Input Error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
// sending result to UI -----------------------------------------------------
private void updateResults(RiskResult result) {
double risk = result.getRiskScore();
RiskLevel level = result.getRiskLevel();
Color color;
switch (level) {
case LOW: color = GREEN; break;
case MEDIUM: color = AMBER; break;
default: color = RED; break;
}
lblRiskPercent.setText(String.format("%.1f%%", risk));
lblRiskPercent.setForeground(color);
riskBar.setValue((int) Math.round(risk));
riskBar.setForeground(color);
lblRiskLevel.setText("Risk Level: " + level.getLabel());
lblRiskLevel.setForeground(color);
taMessage.setText(result.getMessage());
taMessage.setForeground(color);
taMessage.setBackground(tintColor(color, 0.07f));
taMessage.setBorder(new CompoundBorder(
new LineBorder(color, 1, true),
new EmptyBorder(10, 14, 10, 14)));
}
// -----------------------------------------------------
// UI HELPERS
// -----------------------------------------------------
private JTextField addField(JPanel grid, String label) {
grid.add(fieldLabel(label));
JTextField tf = new JTextField();
tf.setFont(new Font("SansSerif", Font.PLAIN, 13));
grid.add(tf);
return tf;
}
private JLabel fieldLabel(String text) {
JLabel l = new JLabel(text);
l.setFont(new Font("SansSerif", Font.PLAIN, 13));
l.setForeground(TEXT_DARK);
return l;
}
private JButton styledButton(String text, Color bg) {
JButton b = new JButton(text);
b.setFont(new Font("SansSerif", Font.BOLD, 13));
b.setBackground(bg);
b.setForeground(Color.WHITE);
b.setFocusPainted(false);
b.setBorder(new EmptyBorder(8, 18, 8, 18));
b.setOpaque(true);
return b;
}
private TitledBorder panelBorder(String title) {
TitledBorder b = BorderFactory.createTitledBorder(
new LineBorder(BORDER_COLOR, 1, true), title);
b.setTitleFont(new Font("SansSerif", Font.BOLD, 13));
b.setTitleColor(ACCENT_BLUE);
return b;
}
private JLabel sidebarSectionTitle(String text) {
JLabel l = new JLabel(text.toUpperCase());
l.setFont(new Font("SansSerif", Font.BOLD, 11));
l.setForeground(ACCENT_BLUE);
l.setAlignmentX(Component.LEFT_ALIGNMENT);
return l;
}
private JTextArea sidebarText(String text) {
JTextArea ta = new JTextArea(text);
ta.setEditable(false);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.setFont(new Font("SansSerif", Font.PLAIN, 12));
ta.setForeground(TEXT_DARK);
ta.setBackground(BG_SIDEBAR);
ta.setBorder(new EmptyBorder(4, 0, 4, 0));
ta.setAlignmentX(Component.LEFT_ALIGNMENT);
return ta;
}
private JSeparator sidebarDivider() {
JSeparator sep = new JSeparator();
sep.setForeground(BORDER_COLOR);
sep.setMaximumSize(new Dimension(Integer.MAX_VALUE, 10));
return sep;
}
private JPanel riskLegendRow(String dot, String text, Color color) {
JPanel row = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 0));
row.setBackground(BG_SIDEBAR);
row.setAlignmentX(Component.LEFT_ALIGNMENT);
row.setMaximumSize(new Dimension(Integer.MAX_VALUE, 24));
JLabel dotLbl = new JLabel(dot);
dotLbl.setFont(new Font("SansSerif", Font.BOLD, 16));
dotLbl.setForeground(color);
JLabel txtLbl = new JLabel(text);
txtLbl.setFont(new Font("SansSerif", Font.PLAIN, 12));
txtLbl.setForeground(TEXT_DARK);
row.add(dotLbl);
row.add(txtLbl);
return row;
}
// Returns a very light tint of the given colour for message box backgrounds
private Color tintColor(Color base, float amount) {
int r = (int) (base.getRed() + (255 - base.getRed()) * (1 - amount));
int g = (int) (base.getGreen() + (255 - base.getGreen()) * (1 - amount));
int b = (int) (base.getBlue() + (255 - base.getBlue()) * (1 - amount));
return new Color(
Math.min(r, 255),
Math.min(g, 255),
Math.min(b, 255));
}
// -----------------------------------------------------
// MAIN
// -----------------------------------------------------
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}