-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResults.java
202 lines (161 loc) · 7.31 KB
/
Results.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
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
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Results extends JFrame implements ActionListener, MouseListener {
private int importoGiocato, importoGuadagnato,saldo;
private String name;
private Color buttonsColor = new Color(87,87,87);
private ImageIcon background = new ImageIcon("backgrounds/results.png");
private Color buttonsColorHover = new Color(120,120,120);
//Label e bottoni
private JLabel mainLabel = new JLabel();
private JLabel titleLabel = new JLabel("RISULTATI");
private JLabel subLabel = new JLabel();
private JButton saveButton = new JButton("SCARICA ⬇");
private JButton continuaButton = new JButton();
//FONT
private Font titleFont = new Font("LEMON MILK",Font.BOLD,50);
private Font buttonsFont = new Font("LEMON MILK",Font.BOLD,20);
public Results(int importoGiocato, int importoGuadagnato,int saldo,String name){
this.importoGuadagnato=importoGuadagnato;
this.importoGiocato = importoGiocato;
this.name=name;
this.saldo=saldo;
this.setTitle("RISULTATI");
this.setResizable(false);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setSize(1300,900);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setVisible(true);
mainLabel.setIcon(background);
mainLabel.setBounds(0, 0, 1300, 900);
titleLabel.setBounds(284, 22, 730, 90);
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setHorizontalTextPosition(JLabel.CENTER);
titleLabel.setFont(titleFont);
titleLabel.setForeground(Color.WHITE);
subLabel.setBounds(393, 227, 513, 270);
if(this.importoGuadagnato>0){
subLabel.setBackground(new Color(86,188,108));
this.saldo-=importoGiocato;
}
else subLabel.setBackground(Color.RED);
subLabel.setForeground(Color.WHITE);
subLabel.setFont(buttonsFont);
subLabel.setOpaque(true);
subLabel.setVerticalAlignment(JLabel.CENTER);
subLabel.setHorizontalAlignment(JLabel.CENTER);
subLabel.setHorizontalTextPosition(JLabel.CENTER);
subLabel.setVerticalTextPosition(JLabel.CENTER);
subLabel.setText("<html>IMPORTO GIOCATO: "+this.importoGiocato+"<br>" + "IMPORTO GUADAGNATO: "+this.importoGuadagnato+"<br>" + "<br>" + "PROFITTO = "+this.importoGuadagnato+"</html>");
saveButton.setBounds(549, 554, 203, 93);
saveButton.setFont(buttonsFont);
saveButton.setVerticalTextPosition(JLabel.CENTER);
saveButton.setHorizontalTextPosition(JLabel.CENTER);
saveButton.setOpaque(true);
saveButton.setFocusable(false);
saveButton.setBorder(null);
saveButton.setForeground(Color.WHITE);
saveButton.setBackground(buttonsColor);
saveButton.addActionListener(this);
saveButton.addMouseListener(this);
continuaButton.setBounds(515, 676, 268, 118);
continuaButton.setText("Continua");
continuaButton.setFont(buttonsFont);
continuaButton.setVerticalTextPosition(JLabel.CENTER);
continuaButton.setHorizontalTextPosition(JLabel.CENTER);
continuaButton.setOpaque(true);
continuaButton.setFocusable(false);
continuaButton.setBorder(null);
continuaButton.setForeground(Color.WHITE);
continuaButton.setBackground(buttonsColor);
continuaButton.addActionListener(this);
continuaButton.addMouseListener(this);
this.add(continuaButton);
this.add(saveButton);
this.add(subLabel);
this.add(titleLabel);
this.add(mainLabel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==saveButton){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home") + "/Desktop"));
FileNameExtensionFilter filter = new FileNameExtensionFilter("File di testo (.txt)", "txt");
fileChooser.setFileFilter(filter);
int returnValue = fileChooser.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
String filePath = selectedFile.getAbsolutePath();
// Aggiungi l'estensione .txt se necessario
if (!filePath.toLowerCase().endsWith(".txt")) {
filePath += ".txt";
}
try {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
String dataOra = now.format(formatter);
String schedaVittoria = "********** Scheda di Gioco **********\n" +"Nome: "+this.name+ "\nData e Ora: " + dataOra + "\n" + "Importo Giocato: $" + this.importoGiocato + "\n" + "Importo Guadagnato: $" + this.importoGuadagnato + "\n" +"\n*****************************************";
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
writer.write(schedaVittoria);
writer.close();
dispose();
SwingUtilities.invokeLater(() -> {
HomePage results = new HomePage(this.name,(this.saldo+=importoGuadagnato));
results.setVisible(true);
});
try {
Thread.sleep(100); // Pausa di 1 secondo per caricare correttamente il frame
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
if(e.getSource()==continuaButton){
dispose();
SwingUtilities.invokeLater(() -> {
HomePage results = new HomePage(this.name,(this.saldo+=importoGuadagnato));
results.setVisible(true);
});
try {
Thread.sleep(100); // Pausa di 1 secondo per caricare correttamente il frame
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
if(e.getSource()==saveButton) saveButton.setBackground(buttonsColorHover);
if(e.getSource()==continuaButton) continuaButton.setBackground(buttonsColorHover);
}
@Override
public void mouseExited(MouseEvent e) {
if(e.getSource()==saveButton) saveButton.setBackground(buttonsColor);
if(e.getSource()==continuaButton) continuaButton.setBackground(buttonsColor);
}
}