Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package quiz;
- import java.awt.CardLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.event.WindowEvent;
- import java.io.IOException;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JSplitPane;
- import javax.swing.JTextField;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import helper.PaneStyler;
- public class QuizView {
- private static final int FRAME_HEIGHT = 350;
- private static final int FRAME_WIDTH = 600;
- private static Point compCoords;
- private JFrame frame;
- private GroupLayout frameLayout;
- private GroupLayout titleBarLayout;
- private JButton exitButton;
- private JPanel titleBar;
- private JPanel cardPanel;
- private CardLayout cardPanelLayout;
- private JPanel loginCardPanel;
- private JPanel unnamedLargePanel;
- private JSplitPane loginFormSplit;
- private JSplitPane userNameSplit;
- private JTextField usernameTextField;
- private JPasswordField passwordField;
- private JSplitPane passwordSplit;
- private JPanel loginButtonPanel;
- private JPanel quizCardPanel;
- private JTextField questionField;
- private JLabel questionLabel;
- private JLabel[] answers;
- private JTextField[] answerFields;
- private QuizModel model;
- public QuizView(QuizModel model) {
- this.model = model;
- initializeExitButton();
- titleBar = createTitleBarPanel();
- unnamedLargePanel = createUnnamedLargePanel();
- loginFormSplit = createLoginFormSplit();
- userNameSplit = createUsernameSplit();
- passwordSplit = createPasswordSplit();
- loginFormSplit.setLeftComponent(userNameSplit);
- loginFormSplit.setRightComponent(passwordSplit);
- loginButtonPanel = createLoginButtonPanel();
- loginCardPanel = createLoginCardPanel();
- quizCardPanel = createQuizCardPanel();
- cardPanel = createCardPanel();
- setUpJFrame();
- frame.setVisible(true);
- }
- public void initializeExitButton() {
- exitButton = new JButton("X");
- exitButton.setFocusPainted(false);
- exitButton.setBackground(new Color(150, 150, 150));
- exitButton.setForeground(new Color(255,255,255));
- exitButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
- }
- });
- }
- private JPanel createUnnamedLargePanel() {
- JPanel panel = new JPanel();
- return panel;
- }
- private JSplitPane createLoginFormSplit() {
- JSplitPane splitPane = new JSplitPane();
- splitPane.setEnabled(false);
- splitPane.setBorder(null);
- splitPane.setContinuousLayout(true);
- splitPane.setAlignmentY(Component.BOTTOM_ALIGNMENT);
- splitPane.setPreferredSize(new Dimension(250, 50));
- splitPane.setMinimumSize(new Dimension(250, 25));
- PaneStyler.flattenJSplitPane(splitPane);
- return splitPane;
- }
- private JSplitPane createUsernameSplit() {
- JSplitPane splitPane = new JSplitPane();
- splitPane.setEnabled(false);
- splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
- PaneStyler.flattenJSplitPane(splitPane);
- JLabel usernameLabel = new JLabel("Username");
- splitPane.setLeftComponent(usernameLabel);
- usernameTextField = new JTextField();
- splitPane.setRightComponent(usernameTextField);
- usernameTextField.setColumns(10);
- return splitPane;
- }
- private JSplitPane createPasswordSplit() {
- JSplitPane splitPane = new JSplitPane();
- splitPane.setEnabled(false);
- splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
- PaneStyler.flattenJSplitPane(splitPane);
- JLabel passwordLabel = new JLabel("Password");
- splitPane.setLeftComponent(passwordLabel);
- passwordField = new JPasswordField();
- splitPane.setRightComponent(passwordField);
- return splitPane;
- }
- private JPanel createLoginButtonPanel() {
- JPanel panel = new JPanel();
- JButton btnLogin = new JButton("Login");
- btnLogin.addActionListener(new loginFieldListener());
- GroupLayout loginButtonPanelLayout = new GroupLayout(panel);
- loginButtonPanelLayout.setHorizontalGroup(
- loginButtonPanelLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(loginButtonPanelLayout.createSequentialGroup()
- .addContainerGap()
- .addComponent(btnLogin)
- .addContainerGap(362, Short.MAX_VALUE))
- );
- loginButtonPanelLayout.setVerticalGroup(
- loginButtonPanelLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, loginButtonPanelLayout.createSequentialGroup()
- .addContainerGap(26, Short.MAX_VALUE)
- .addComponent(btnLogin)
- .addContainerGap())
- );
- panel.setLayout(loginButtonPanelLayout);
- return panel;
- }
- private JPanel createTitleBarPanel() {
- JPanel panel = new JPanel();
- compCoords = null;
- panel.setBackground(Color.LIGHT_GRAY);
- titleBarLayout = new GroupLayout(panel);
- titleBarLayout.setHorizontalGroup(
- titleBarLayout.createParallelGroup(Alignment.TRAILING)
- .addGroup(Alignment.LEADING, titleBarLayout.createSequentialGroup()
- .addContainerGap(678, Short.MAX_VALUE)
- .addComponent(exitButton))
- );
- titleBarLayout.setVerticalGroup(
- titleBarLayout.createParallelGroup(Alignment.LEADING)
- .addComponent(exitButton, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
- );
- panel.setLayout(titleBarLayout);
- panel.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- compCoords = e.getPoint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- compCoords = null;
- }
- });
- panel.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseDragged(MouseEvent e) {
- Point currCoords = e.getLocationOnScreen();
- frame.setLocation(currCoords.x - compCoords.x, currCoords.y - compCoords.y);
- }
- });
- return panel;
- }
- private JPanel createCardPanel() {
- JPanel panel = new JPanel();
- cardPanelLayout = new CardLayout();
- panel.setLayout(cardPanelLayout);
- panel.add(loginCardPanel, "loginCard");
- panel.add(quizCardPanel, "quizCard");
- return panel;
- }
- private JPanel createLoginCardPanel() {
- JPanel panel = new JPanel();
- GroupLayout loginCardLayout = new GroupLayout(panel);
- loginCardLayout.setHorizontalGroup(
- loginCardLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, loginCardLayout.createSequentialGroup()
- .addContainerGap()
- .addGroup(loginCardLayout.createParallelGroup(Alignment.TRAILING)
- .addComponent(unnamedLargePanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
- .addGroup(loginCardLayout.createSequentialGroup()
- .addComponent(loginFormSplit, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(loginButtonPanel, GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)))
- .addContainerGap())
- );
- loginCardLayout.setVerticalGroup(
- loginCardLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, loginCardLayout.createSequentialGroup()
- .addComponent(unnamedLargePanel, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addGroup(loginCardLayout.createParallelGroup(Alignment.LEADING, false)
- .addComponent(loginFormSplit, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(loginButtonPanel, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
- .addContainerGap())
- );
- panel.setLayout(loginCardLayout);
- return panel;
- }
- private JPanel createQuizCardPanel() {
- JPanel panel = new JPanel();
- answers = new JLabel[4];
- for(int i = 1; i <=4; i++) {
- answers[i-1] = new JLabel("Answer " + i + ":");
- }
- answerFields = new JTextField[4];
- for(int i = 0; i < 4; i++) {
- answerFields[i] = new JTextField();
- answerFields[i].setColumns(10);
- }
- questionLabel = new JLabel("Question:");
- questionField = new JTextField();
- questionField.setColumns(10);
- JButton submitQuestionbtn = new JButton("Send Question!");
- submitQuestionbtn.addActionListener(new questionFieldsListener());
- GroupLayout gl_panel = new GroupLayout(panel);
- gl_panel.setHorizontalGroup(
- gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
- .addContainerGap(595, Short.MAX_VALUE)
- .addComponent(submitQuestionbtn)
- .addContainerGap())
- .addGroup(gl_panel.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(questionLabel)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(questionField, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
- .addContainerGap(308, Short.MAX_VALUE))
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(answers[3], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(answerFields[3])
- .addGap(308))
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(answers[2], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(answerFields[2])
- .addGap(308))
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(answers[1], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(answerFields[1])
- .addGap(308))
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(answers[0])
- .addGap(11)
- .addComponent(answerFields[0])
- .addGap(308))))
- );
- gl_panel.setVerticalGroup(
- gl_panel.createParallelGroup(Alignment.TRAILING)
- .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
- .addContainerGap(263, Short.MAX_VALUE)
- .addComponent(submitQuestionbtn)
- .addContainerGap())
- .addGroup(gl_panel.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(questionLabel)
- .addComponent(questionField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(30)
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(answers[0])
- .addComponent(answerFields[0], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(30)
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(answers[1])
- .addComponent(answerFields[1], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(30)
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(answers[2])
- .addComponent(answerFields[2], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(30)
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(answerFields[3], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(answers[3]))
- .addGap(30))
- );
- panel.setLayout(gl_panel);
- return panel;
- }
- private void setUpJFrame() {
- frame = new JFrame("Park Quiz!");
- frameLayout = new GroupLayout(frame.getContentPane());
- frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
- frame.setResizable(false);
- frame.setUndecorated(true);
- frame.setBounds(200, 200, 721, 352);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frameLayout.setHorizontalGroup(
- frameLayout.createParallelGroup(Alignment.LEADING)
- .addComponent(titleBar, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
- .addComponent(cardPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
- );
- frameLayout.setVerticalGroup(
- frameLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(frameLayout.createSequentialGroup()
- .addComponent(titleBar, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(cardPanel, GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE))
- );
- frame.setLayout(frameLayout);
- frame.add(titleBar);
- }
- private class questionFieldsListener implements ActionListener {
- public questionFieldsListener() {
- super();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String question = questionField.getText();
- String[] answers = new String[4];
- for(int i = 0; i < 4; i++) {
- answers[i] = answerFields[i].getText();
- }
- try {
- model.addQuestionPost(question, answers);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- private class loginFieldListener implements ActionListener {
- public loginFieldListener() {
- super();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String username = usernameTextField.getText();
- char[] password = passwordField.getPassword();
- model.tryLoginInsecure(username, password);
- if(model.verifyToken())
- cardPanelLayout.next(cardPanel);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement