This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.java
159 lines (140 loc) · 4.31 KB
/
Dashboard.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
* {@code Dashboard} class provides the dashboard page and all associated
* utilities for the same. It displays statistics of a player only if he has
* played atleast one game.
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions.
* </p>
*
* @author Emmanuel Jojy
*
*/
public class Dashboard implements ActionListener {
/**
* Pluggable panel which is plugged in to the frame generated by the {@code GUI}
* class.
*/
JPanel p;
private JButton game, log, about;
private JLabel detail, credit;
private String name, time;
private boolean newUser = true;
private int tot, win, loss;
/**
* Default constrctor provided for {@code Dashboard} class. Initializes the
* pluggable JPanel p.
*
* To be invoked only from {@code GUI} class.
*/
public Dashboard() {
p = new JPanel();
p.setSize(1366, 720);
p.setLayout(null);
name = GUI.name;
tot = 0;
win = 0;
loss = 0;
JLabel head = new JLabel("Hello, @" + name.toLowerCase(), JLabel.CENTER);
head.setBounds(283, 10, 800, 40);
head.setFont(new Font("Consolas", Font.PLAIN, 28));
p.add(head);
analyse();
if (newUser) {
JLabel msg = new JLabel("IT'S EMPTY IN HERE. PLAY A GAME!", JLabel.CENTER);
msg.setBounds(283, 250, 800, 40);
msg.setFont(new Font("Calibri", Font.ITALIC, 24));
p.add(msg);
} else {
JPanel min = new JPanel();
min.setBounds(483, 90, 400, 290);
min.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
min.setLayout(null);
JLabel score = new JLabel("ACCURACY: " + (float) (win * 100.00 / tot) + "%", JLabel.CENTER);
score.setBounds(20, 40, 350, 30);
score.setFont(new Font("Calibri", Font.PLAIN, 20));
min.add(score);
JLabel last = new JLabel("LAST PLAYED: " + time.substring(0, 10), JLabel.CENTER);
last.setBounds(20, 80, 350, 30);
last.setFont(new Font("Calibri", Font.PLAIN, 20));
min.add(last);
JLabel total = new JLabel("GAMES PLAY: " + tot, JLabel.CENTER);
total.setBounds(20, 120, 350, 30);
total.setFont(new Font("Calibri", Font.PLAIN, 20));
min.add(total);
JLabel winner = new JLabel("GAMES WON: " + win, JLabel.CENTER);
winner.setBounds(20, 160, 350, 30);
winner.setFont(new Font("Calibri", Font.PLAIN, 20));
min.add(winner);
JLabel lost = new JLabel("GAMES LOST: " + loss, JLabel.CENTER);
lost.setBounds(20, 200, 350, 30);
lost.setFont(new Font("Calibri", Font.PLAIN, 20));
min.add(lost);
JLabel left = new JLabel("GAMES LEFT: " + (tot - win - loss), JLabel.CENTER);
left.setBounds(20, 240, 350, 30);
left.setFont(new Font("Calibri", Font.PLAIN, 20));
min.add(left);
p.add(min);
}
game = new JButton("NEW GAME");
game.setBounds(483, 420, 100, 30);
game.addActionListener(this);
p.add(game);
about = new JButton("ABOUT");
about.setBounds(633, 420, 100, 30);
about.addActionListener(this);
p.add(about);
log = new JButton("LOG OUT");
log.setBounds(783, 420, 100, 30);
log.addActionListener(this);
p.add(log);
detail = new JLabel("HangMan, a word guessing game. Created as part of Java Final Project.", JLabel.CENTER);
detail.setFont(new Font("Calibri", Font.PLAIN, 20));
detail.setBounds(383, 490, 600, 30);
detail.setVisible(false);
p.add(detail);
credit = new JLabel("Adhil | Bivin | Emmanuel | Garry", JLabel.CENTER);
credit.setFont(new Font("Calibri", Font.PLAIN, 20));
credit.setBounds(383, 520, 600, 30);
credit.setVisible(false);
p.add(credit);
}
private void analyse() {
String query = "SELECT * FROM game WHERE NAME = '" + name + "';";
try {
ResultSet res = Main.st.executeQuery(query);
res.next();
time = res.getString("TIMESTAMP");
tot = res.getInt("TOTAL");
win = res.getInt("WIN");
loss = res.getInt("LOSS");
newUser = false;
} catch (SQLException e) {
}
}
/**
* Overriden method of {@code ActionListener}. Refer original documentation of
* the same for more details.
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == game)
GUI.game();
if (e.getSource() == log)
GUI.login();
if (e.getSource() == about) {
if (detail.isVisible()) {
detail.setVisible(false);
credit.setVisible(false);
} else {
detail.setVisible(true);
credit.setVisible(true);
}
}
}
}