-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCountLayout.java
160 lines (139 loc) · 5.44 KB
/
TestCountLayout.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
//GUI page
//Generates a randomly generated number of shapes for counting or operations
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;
import javax.swing.border.LineBorder;
import java.util.*;
import java.util.Random;
import java.util.ArrayList;
public class TestCountLayout extends GUI_Page
{
public static ArrayList<AnswerKey> currentTest;
private int inputContainerHeight = 50;
private int inputContainerWidth;// = 400;
private int shapeContainerWidth = 345; //5x64 + 5
private int shapeContainerHeight = 138;//2x64 + 5
private int numPips;
private int maxNumPips;
private int numQuestions;
private int curQuestion;
private JTextField answerTextField;
private JPanel shapeContainer;
private int defaultBorderSize = 2;
private int expandedBorderSize = 3;
public TestCountLayout(String pageKey, String pageTitle, int numQuestions, int maxNumPips){
this.pageTitle = pageTitle;
this.pageKey = pageKey;
this.numQuestions = numQuestions;
this.maxNumPips = maxNumPips;
}
public void activate(){
/*JPanel mainContainer = new JPanel();
mainContainer.setLocation( GUI.BUFFER_SIZE_LG );
mainContainer.setSize( GUI.SCREEN_WIDTH - GUI.BUFFER_SIZE_LG*3, GUI.SCREEN_HEIGHT - (inputContainerHeight + GUI.BUFFER_SIZE_LG*3) );
mainContainer.setLayout( new BoxLayout(mainContainer, BoxLayout.X_AXIS));
mainContainer.setOpaque(true);
GUI.addComponent( mainContainer );*/
GUI.addComponent(new HomeButton());
curQuestion = 0;
currentTest = new ArrayList<AnswerKey>();
shapeContainer = new JPanel();
shapeContainer.setBackground(Color.black);
shapeContainer.setLocation( GUI.SCREEN_WIDTH/2 - shapeContainerWidth/2, GUI.SCREEN_HEIGHT/2 - shapeContainerHeight );
shapeContainer.setSize( shapeContainerWidth, shapeContainerHeight );
GridLayout gridLayout = new GridLayout(2,5);
shapeContainer.setLayout( gridLayout );
GUI.addComponent( shapeContainer );
int inputContainerWidth = GUI.NAVBUTTON_WIDTH*2 + GUI.BUFFER_SIZE_LG*3;
JPanel inputContainer = new JPanel();
inputContainer.setLocation( GUI.SCREEN_WIDTH/2 - inputContainerWidth/2, GUI.SCREEN_HEIGHT - (GUI.BUFFER_SIZE_LG*4 + inputContainerHeight/2) );
inputContainer.setSize( inputContainerWidth, inputContainerHeight );
GridLayout inputLayout = new GridLayout(1,3);
inputContainer.setLayout( inputLayout );
inputContainer.setBackground( Color.black );
GUI.addComponent( inputContainer );
answerTextField = new JTextField();
answerTextField.setFont( new Font("Calibri", Font.BOLD, 24));
inputContainer.add( answerTextField );
JLabel submitAnswerButton = new JLabel("Check Ans", SwingConstants.CENTER);
submitAnswerButton.setForeground( Color.white );
submitAnswerButton.setFont( new Font("Calibri", Font.BOLD, 24));
submitAnswerButton.setBorder( new LineBorder(Color.LIGHT_GRAY, defaultBorderSize));
submitAnswerButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
mousePressAction();
}
public void mouseEntered(MouseEvent evt) {
submitAnswerButton.setBorder(new LineBorder(Color.white, expandedBorderSize));
}
public void mouseExited(MouseEvent evt) {
submitAnswerButton.setBorder(new LineBorder(Color.LIGHT_GRAY, defaultBorderSize));
}
});
inputContainer.add( submitAnswerButton );
JLabel nextQuestionButton = new JLabel(">>>", SwingConstants.CENTER);
nextQuestionButton.setForeground( Color.white );
nextQuestionButton.setFont( new Font("Calibri", Font.BOLD, 24));
nextQuestionButton.setBorder(new LineBorder(Color.LIGHT_GRAY, defaultBorderSize));
nextQuestionButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
mousePressAction();
}
public void mouseEntered(MouseEvent evt) {
nextQuestionButton.setBorder(new LineBorder(Color.white, expandedBorderSize));
}
public void mouseExited(MouseEvent evt) {
nextQuestionButton.setBorder(new LineBorder(Color.LIGHT_GRAY, defaultBorderSize));
}
});
inputContainer.add( nextQuestionButton );
nextQuestion();
}
private void mousePressAction(){
int userAns = -1;
try {
userAns = Integer.parseInt( answerTextField.getText() );
} catch(NumberFormatException numForEx){
//userAns = -1;
}
currentTest.add( new AnswerKey( userAns, numPips ));
nextQuestion();
}
private void nextQuestion(){
if(curQuestion > numQuestions){
TestResultLayout.testType = TestType.RNG_SHAPE;
GUI_Manager.loadPage( GUI.RESULTS_ID );
}
if(shapeContainer.getComponentCount() > 0){
shapeContainer.removeAll();
}
curQuestion++;
numPips = getRndomInt();
for(int i = 0; i < numPips; i++){
JLabel img = new JLabel(Resource_Manager.loadImageLocal("pip_image.png"));
shapeContainer.add(img);
}
answerTextField.setText("");
shapeContainer.revalidate();
shapeContainer.repaint();
}
private int getRndomInt(){
Random rng = new Random();
return rng.nextInt(maxNumPips) + 1;
}
public static int getScore(){
int numCorrect = 0;
for(AnswerKey ak : currentTest)
{
if(ak.userAnswer == ak.correctAnswer)
{
numCorrect++;
}
}
double finalScore = 100*((double)numCorrect/currentTest.size());
//System.out.printf("Final Grade: %.2f\nNum Correct: %d of %d\n", finalScore, totalCorrect, currentTestAnswers.size());
return (int)finalScore;
}
}