-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiply.java
More file actions
284 lines (237 loc) · 8.22 KB
/
Multiply.java
File metadata and controls
284 lines (237 loc) · 8.22 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
package mathPractice;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Color;
import javax.swing.JProgressBar;
import java.awt.SystemColor;
public class Multiply extends JFrame {
private JPanel contentPane;
private int counter;
int a = 0;
Methods m = new Methods();
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Multiply frame = new Multiply();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private JTextField textNum1;
private JTextField textNum2;
private JTextField answer ;
private JTextField scoreBoard;
private JLabel levelDisplayer;
private JProgressBar levelProgress;
int a1;
int num1,num2;
String name;
private JTextField showAnswer;
private JLabel lblNewLabel_3;
private JTextField highScoreDisplay;
private JLabel lblAuthor;
public void starter(int a) {
Random number = new Random();
if (a < 5) {
levelDisplayer.setText("Level 1:");
do {
num1 = number.nextInt(3);
num2 = number.nextInt(8);
}while(num1 == 0 || num2 == 0 );
}
else if( a < 10) {
if (a == 5) {
JOptionPane.showMessageDialog(null, "Congrats you have completed level 1!");
a1 = 0;
levelProgress.setValue(a1);
}
levelDisplayer.setText("Level 2:");
do {
num1 = number.nextInt(4);
num2 = number.nextInt(10);
}while(num1 == 0 || num2 == 0 );
}
else if ( a < 15)
if (a == 10) {
JOptionPane.showMessageDialog(null, "Congrats you have completed level 2!"); a1 = 0;
levelProgress.setValue(a1);
}else {
levelDisplayer.setText("Level 3:"); a1 = 0;
do {
num1 = number.nextInt(6);
num2 = number.nextInt(10);
}while(num1 == 0 || num2 == 0 );
}
else {
if (a == 15) {
JOptionPane.showMessageDialog(null, "Congrats you have completed the final level \n now you can keep practicing to get a higher Score!"); a1 = 0;
levelProgress.setValue(a1);
}else {
levelDisplayer.setText("Unlimited:"); a1 = 0;
do {
levelProgress.setMaximum(150);
num1 = number.nextInt(6);
num2 = number.nextInt(10);
}while(num1 == 0 || num2 == 0 );
}
}
textNum1.setText(Integer.toString(num1));
textNum2.setText(Integer.toString(num2));
scoreBoard.setText(Integer.toString(getCounter()));
answer.setText("");
}
public Multiply() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 449);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 250, 205));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel display = new JLabel("");
display.setHorizontalAlignment(SwingConstants.CENTER);
display.setFont(new Font("Serif", Font.BOLD, 24));
display.setBounds(156, 174, 378, 39);
contentPane.add(display);
textNum1 = new JTextField();
textNum1.setHorizontalAlignment(SwingConstants.CENTER);
textNum1.setFont(new Font("Tahoma", Font.PLAIN, 55));
textNum1.setBounds(55, 88, 200, 75);
contentPane.add(textNum1);
textNum1.setColumns(10);
textNum2 = new JTextField();
textNum2.setHorizontalAlignment(SwingConstants.CENTER);
textNum2.setFont(new Font("Tahoma", Font.PLAIN, 55));
textNum2.setColumns(10);
textNum2.setBounds(438, 88, 200, 75);
contentPane.add(textNum2);
JLabel lblNewLabel = new JLabel("X");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 24));
lblNewLabel.setBounds(334, 123, 54, 29);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Answer:");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblNewLabel_1.setBounds(175, 213, 161, 64);
contentPane.add(lblNewLabel_1);
answer = new JTextField();
answer.setHorizontalAlignment(SwingConstants.CENTER);
answer.setFont(new Font("Tahoma", Font.PLAIN, 18));
answer.setBounds(283, 224, 150, 48);
contentPane.add(answer);
answer.setColumns(10);
levelProgress = new JProgressBar();
levelProgress.setString("Progress");
levelProgress.setValue(0);
levelProgress.setMaximum(5);
levelProgress.setBounds(398, 370, 249, 29);
contentPane.add(levelProgress);
scoreBoard = new JTextField();
scoreBoard.setHorizontalAlignment(SwingConstants.CENTER);
scoreBoard.setFont(new Font("Tahoma", Font.PLAIN, 31));
scoreBoard.setBounds(26, 339, 106, 60);
contentPane.add(scoreBoard);
scoreBoard.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Correct Answers");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_2.setBounds(26, 316, 106, 14);
contentPane.add(lblNewLabel_2);
levelDisplayer = new JLabel("");
levelDisplayer.setFont(new Font("Tahoma", Font.PLAIN, 25));
levelDisplayer.setBounds(288, 370, 131, 29);
contentPane.add(levelDisplayer);
JLabel lblMultiply = new JLabel("Multiplication");
lblMultiply.setForeground(new Color(0, 0, 139));
lblMultiply.setHorizontalAlignment(SwingConstants.CENTER);
lblMultiply.setFont(new Font("Serif", Font.BOLD, 52));
lblMultiply.setBounds(156, 11, 399, 64);
contentPane.add(lblMultiply);
JButton btnNewButton = new JButton("View Answer");
JButton btnCheck = new JButton("Check");
btnCheck.setFont(new Font("Tahoma", Font.PLAIN, 20));
starter(1);
btnCheck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (Integer.parseInt(answer.getText()) == (num1*num2)) {
contentPane.setBackground(new Color(50, 205, 50));
a1++;
setCounter(getCounter()+1);
levelProgress.setValue(a1);
showAnswer.hide();
display.setText(m.correctResponse());
starter(getCounter());
highScoreDisplay.setText(Integer.toString(m.highestScore(getCounter())));
}
else {
contentPane.setBackground(new Color(255, 99, 71));
a1=0;
display.setText(m.incorrectResponse());
levelProgress.setValue(a1);
setCounter(0);
answer.setText("");
btnNewButton.show();
//starter(getCounter());
btnNewButton.setBounds(488, 220, 150, 50);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
btnNewButton.hide();
showAnswer.show();
showAnswer.setBounds(488, 220, 150, 50);
String answer = num1 + " x "+num2+ " = "+num1*num2;
showAnswer.setText(answer);
}
});
contentPane.add(btnNewButton);
}
}catch(Exception ab) {
JOptionPane.showMessageDialog(null, ab);
}
}
});
btnCheck.setBounds(269, 288, 150, 50);
contentPane.add(btnCheck);
showAnswer = new JTextField();
showAnswer.setFont(new Font("Tahoma", Font.PLAIN, 30));
showAnswer.setBounds(488, 259, 0, 0);
contentPane.add(showAnswer);
showAnswer.setColumns(10);
lblNewLabel_3 = new JLabel("Highest Score");
lblNewLabel_3.setHorizontalAlignment(SwingConstants.TRAILING);
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 23));
lblNewLabel_3.setBounds(468, 298, 157, 28);
contentPane.add(lblNewLabel_3);
highScoreDisplay = new JTextField();
highScoreDisplay.setFont(new Font("Tahoma", Font.PLAIN, 25));
highScoreDisplay.setHorizontalAlignment(SwingConstants.CENTER);
highScoreDisplay.setText("0");
highScoreDisplay.setBounds(512, 339, 76, 29);
contentPane.add(highScoreDisplay);
highScoreDisplay.setColumns(10);
lblAuthor = new JLabel("Author : Anish K.");
lblAuthor.setBounds(10, 11, 185, 14);
contentPane.add(lblAuthor);
}
}