-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestLayout.java
173 lines (150 loc) · 6.52 KB
/
TestLayout.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
//GUI page
//Test type displays hard coded, multiple choice questions
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class TestLayout extends GUI_Page
{
private static int NUM_ANS_BTNS = 4;
private static int PRESSED_BTN_DEFAULT = -1;
private static int quesTextSize = 25;
private static String font = "Serif";
public String nextPage;
public String previousPage;
public AnswerKey currentAnswerKey;
public static int pressedButton;
private int answerButtonHeight = 50;
public TestLayout(String pageKey, String pageTitle){//, String previousPage, String nextPage){
this.pageTitle = pageTitle;
this.nextPage = nextPage;
this.previousPage = previousPage;
this.pageKey = pageKey;
}
public void activate(){
pressedButton = PRESSED_BTN_DEFAULT; //set selected button to 'none pressed'
// Page Title
JPanel titlePanel = new JPanel();
titlePanel.setBackground( Color.black );
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
titlePanel.setSize( GUI.TITLE_LABEL_WIDTH, GUI.TITLE_LABEL_HEIGHT*2 );
titlePanel.setLocation( (GUI.SCREEN_WIDTH/2) - (GUI.TITLE_LABEL_WIDTH/2), GUI.BUFFER_SIZE );
GUI.addComponent(titlePanel);
JLabel title = new JLabel(Module.getActiveModule().topic, SwingConstants.CENTER);
title.setForeground( Color.white );
titlePanel.add(title);
JLabel subTitle = new JLabel(Module.getActiveModule().subTopic, SwingConstants.CENTER);
//subTitle.setSize( GUI.TITLE_LABEL_WIDTH, GUI.TITLE_LABEL_HEIGHT );
//title.setLocation( (GUI.SCREEN_WIDTH/2) - (GUI.TITLE_LABEL_WIDTH/2), GUI.BUFFER_SIZE );
subTitle.setForeground( Color.white );
titlePanel.add(subTitle);
//Home button
GUI.addComponent( new HomeButton() );
currentAnswerKey = Resource_Manager.getAnswerKeys(pageKey);
if(currentAnswerKey == null){
System.out.printf("Add answer key for: %s", pageKey);
return;
}
//Create place holder for text
JLabel pageText = new JLabel(currentAnswerKey.question, SwingConstants.CENTER);
pageText.setFont(new Font(font,Font.PLAIN, quesTextSize));
pageText.setSize(GUI.TEXT_FRAME_WIDTH, GUI.TEXT_FRAME_HEIGHT);
int textPanelStartX = GUI.BUFFER_SIZE_LG*3;
pageText.setLocation(textPanelStartX, GUI.TEST_BUTTON_HEIGHT + GUI.BUFFER_SIZE_LG);
pageText.setForeground( Color.WHITE );
GUI.addComponent( pageText );
//Create place holder for image
JLabel pageImage = new JLabel(Resource_Manager.loadResource(pageKey), SwingConstants.CENTER);
pageImage.setSize(GUI.IMAGE_FRAME_WIDTH, GUI.IMAGE_FRAME_HEIGHT);
pageImage.setLocation(textPanelStartX + GUI.TEXT_FRAME_WIDTH + GUI.BUFFER_SIZE_LG, GUI.TEST_BUTTON_HEIGHT + GUI.BUFFER_SIZE_LG);
GUI.addComponent( pageImage );
//Answer Button Container
int buttonContainerYStartOffset = GUI.IMAGE_FRAME_HEIGHT + GUI.BUFFER_SIZE_LG*2;
int buttonContainerHeight = answerButtonHeight*2 + GUI.BUFFER_SIZE_LG;
JPanel buttonContainer = new JPanel();
buttonContainer.setLayout(new java.awt.GridLayout(2,2,10,10));
buttonContainer.setSize(GUI.SCREEN_WIDTH - (GUI.BUFFER_SIZE_LG*3), buttonContainerHeight);
buttonContainer.setLocation(GUI.BUFFER_SIZE_LG, buttonContainerYStartOffset);
buttonContainer.setBackground(Color.black);
for(int i = 0; i < 4; i++){
buttonContainer.add( new TestButton(currentAnswerKey.answers[i], i) );
}
GUI.addComponent(buttonContainer);
int navCOntainerYStartOffset = buttonContainerYStartOffset + buttonContainerHeight + GUI.BUFFER_SIZE_LG;
int navContainerWidth = GUI.NAVBUTTON_WIDTH*2 + GUI.BUFFER_SIZE_LG*3;
JPanel navContainerPanel = new JPanel();
navContainerPanel.setBackground(Color.black);
GridLayout gridLayout = new GridLayout(1,2);
gridLayout.setHgap(GUI.BUFFER_SIZE_LG);
navContainerPanel.setLayout(gridLayout);
navContainerPanel.setSize(navContainerWidth, GUI.NAVBUTTON_HEIGHT);
System.out.printf("Y Offset: %d", buttonContainerYStartOffset);
navContainerPanel.setLocation(GUI.SCREEN_WIDTH - (navContainerWidth + GUI.BUFFER_SIZE_LG*2), navCOntainerYStartOffset + GUI.BUFFER_SIZE_LG);
GUI.addComponent(navContainerPanel);
//Previous Page Button
ActionButton prev_button = new ActionButton( "Previous Page" );
prev_button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
GUI_Manager.loadPage( Module.getPreviousPageKey() );
}
} );
if(Module.currentPage == 1){
prev_button.setVisible(false);
}
navContainerPanel.add(prev_button);
//Next Page Button
boolean isLastPage = Module.currentPage >= Module.getActiveModule().numPages;
ActionButton next_button = new ActionButton((isLastPage) ? "Finish" : "Next" );
next_button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(GUI.DEBUG)System.out.printf("adding answer for: %s : %d\n", pageKey, pressedButton);
TestHelper.addAnswer(pageKey, pressedButton);
if(isLastPage){
TestHelper.getScore();
//UserProfile.addScore( Module.getActiveModule().pageKey, TestHelper.currentScore );
TestResultLayout.testType = TestType.MULTI;
GUI_Manager.loadPage( GUI.RESULTS_ID );
}
else{
GUI_Manager.loadPage( Module.getNextPageKey() );
}
}
} );
navContainerPanel.add( next_button );
}
}
class TestButton extends JLabel
{
private static JLabel selectedButton;
//private static Color defaultBgColor;
//private static Color defaultFgColor;
private int buttonNumber;
public TestButton(String buttonName, int buttonNumber) {
super(buttonName, SwingConstants.CENTER);
this.buttonNumber = buttonNumber;
setBackground(Color.black);
setForeground(Color.white);
setOpaque(true);
//setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
setBorder(new LineBorder(Color.LIGHT_GRAY, 2, true));
//defaultBgColor = getBackground();
//defaultFgColor = getForeground();
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
onPress();
}
});
}
void onPress() {
if(selectedButton != null) {
selectedButton.setBackground(Color.black);
selectedButton.setForeground(Color.white);
}
selectedButton = this;
TestLayout.pressedButton = buttonNumber;
selectedButton.setForeground(Color.black);
selectedButton.setBackground(Color.white);
//selectedButton.revalidate();
}
}