Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.75 KB | None | 0 0
  1. package quiz;
  2.  
  3. import java.awt.CardLayout;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Dimension;
  7. import java.awt.Point;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseMotionAdapter;
  13. import java.awt.event.WindowEvent;
  14. import java.io.IOException;
  15.  
  16. import javax.swing.GroupLayout;
  17. import javax.swing.GroupLayout.Alignment;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JPanel;
  22. import javax.swing.JPasswordField;
  23. import javax.swing.JSplitPane;
  24. import javax.swing.JTextField;
  25. import javax.swing.LayoutStyle.ComponentPlacement;
  26.  
  27. import helper.PaneStyler;
  28.  
  29. public class QuizView {
  30.     private static final int FRAME_HEIGHT = 350;
  31.     private static final int FRAME_WIDTH = 600;
  32.  
  33.     private static Point compCoords;
  34.  
  35.     private JFrame frame;
  36.     private GroupLayout frameLayout;
  37.     private GroupLayout titleBarLayout;
  38.  
  39.     private JButton exitButton;
  40.  
  41.     private JPanel titleBar;
  42.     private JPanel cardPanel;
  43.    
  44.     private CardLayout cardPanelLayout;
  45.     private JPanel loginCardPanel;
  46.         private JPanel unnamedLargePanel;
  47.         private JSplitPane loginFormSplit;
  48.             private JSplitPane userNameSplit;
  49.                 private JTextField usernameTextField;
  50.                 private JPasswordField passwordField;
  51.             private JSplitPane passwordSplit;
  52.         private JPanel loginButtonPanel;
  53.        
  54.     private JPanel quizCardPanel;
  55.         private JTextField questionField;
  56.         private JLabel questionLabel;
  57.         private JLabel[] answers;
  58.         private JTextField[] answerFields;
  59.  
  60.     private QuizModel model;
  61.  
  62.  
  63.     public QuizView(QuizModel model) {
  64.         this.model = model;
  65.         initializeExitButton();
  66.         titleBar = createTitleBarPanel();
  67.        
  68.         unnamedLargePanel = createUnnamedLargePanel();
  69.        
  70.         loginFormSplit = createLoginFormSplit();
  71.             userNameSplit = createUsernameSplit();
  72.             passwordSplit = createPasswordSplit();
  73.             loginFormSplit.setLeftComponent(userNameSplit);
  74.             loginFormSplit.setRightComponent(passwordSplit);
  75.        
  76.         loginButtonPanel = createLoginButtonPanel();
  77.        
  78.         loginCardPanel = createLoginCardPanel();
  79.        
  80.         quizCardPanel = createQuizCardPanel();
  81.         cardPanel = createCardPanel();
  82.         setUpJFrame();
  83.         frame.setVisible(true);
  84.     }
  85.  
  86.  
  87.    
  88.  
  89.  
  90.  
  91.     public void initializeExitButton() {
  92.         exitButton  = new JButton("X");
  93.         exitButton.setFocusPainted(false);
  94.         exitButton.setBackground(new Color(150, 150, 150));
  95.         exitButton.setForeground(new Color(255,255,255));
  96.         exitButton.addActionListener(new ActionListener() {
  97.             public void actionPerformed(ActionEvent e) {
  98.                 frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
  99.             }
  100.         });
  101.     }
  102.  
  103.     private JPanel createUnnamedLargePanel() {
  104.         JPanel panel = new JPanel();
  105.        
  106.         return panel;
  107.     }
  108.    
  109.     private JSplitPane createLoginFormSplit() {
  110.         JSplitPane splitPane = new JSplitPane();
  111.         splitPane.setEnabled(false);
  112.         splitPane.setBorder(null);
  113.         splitPane.setContinuousLayout(true);
  114.         splitPane.setAlignmentY(Component.BOTTOM_ALIGNMENT);
  115.         splitPane.setPreferredSize(new Dimension(250, 50));
  116.         splitPane.setMinimumSize(new Dimension(250, 25));
  117.         PaneStyler.flattenJSplitPane(splitPane);
  118.         return splitPane;
  119.     }
  120.    
  121.     private JSplitPane createUsernameSplit() {
  122.         JSplitPane splitPane = new JSplitPane();
  123.         splitPane.setEnabled(false);
  124.         splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  125.         PaneStyler.flattenJSplitPane(splitPane);
  126.        
  127.         JLabel usernameLabel = new JLabel("Username");
  128.         splitPane.setLeftComponent(usernameLabel);
  129.        
  130.         usernameTextField = new JTextField();
  131.         splitPane.setRightComponent(usernameTextField);
  132.         usernameTextField.setColumns(10);
  133.        
  134.         return splitPane;
  135.     }
  136.  
  137.     private JSplitPane createPasswordSplit() {
  138.         JSplitPane splitPane = new JSplitPane();
  139.         splitPane.setEnabled(false);
  140.         splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  141.         PaneStyler.flattenJSplitPane(splitPane);
  142.        
  143.         JLabel passwordLabel = new JLabel("Password");
  144.         splitPane.setLeftComponent(passwordLabel);
  145.        
  146.         passwordField = new JPasswordField();
  147.         splitPane.setRightComponent(passwordField);
  148.        
  149.         return splitPane;
  150.     }
  151.  
  152.     private JPanel createLoginButtonPanel() {
  153.         JPanel panel = new JPanel();
  154.         JButton btnLogin = new JButton("Login");
  155.         btnLogin.addActionListener(new loginFieldListener());
  156.         GroupLayout loginButtonPanelLayout = new GroupLayout(panel);
  157.         loginButtonPanelLayout.setHorizontalGroup(
  158.             loginButtonPanelLayout.createParallelGroup(Alignment.LEADING)
  159.                 .addGroup(loginButtonPanelLayout.createSequentialGroup()
  160.                     .addContainerGap()
  161.                     .addComponent(btnLogin)
  162.                     .addContainerGap(362, Short.MAX_VALUE))
  163.         );
  164.         loginButtonPanelLayout.setVerticalGroup(
  165.             loginButtonPanelLayout.createParallelGroup(Alignment.LEADING)
  166.                 .addGroup(Alignment.TRAILING, loginButtonPanelLayout.createSequentialGroup()
  167.                     .addContainerGap(26, Short.MAX_VALUE)
  168.                     .addComponent(btnLogin)
  169.                     .addContainerGap())
  170.         );
  171.         panel.setLayout(loginButtonPanelLayout);
  172.         return panel;
  173.     }
  174.    
  175.     private JPanel createTitleBarPanel() {
  176.         JPanel panel = new JPanel();
  177.         compCoords = null;
  178.         panel.setBackground(Color.LIGHT_GRAY);
  179.  
  180.         titleBarLayout = new GroupLayout(panel);
  181.         titleBarLayout.setHorizontalGroup(
  182.                 titleBarLayout.createParallelGroup(Alignment.TRAILING)
  183.                 .addGroup(Alignment.LEADING, titleBarLayout.createSequentialGroup()
  184.                         .addContainerGap(678, Short.MAX_VALUE)
  185.                         .addComponent(exitButton))
  186.                 );
  187.         titleBarLayout.setVerticalGroup(
  188.                 titleBarLayout.createParallelGroup(Alignment.LEADING)
  189.                 .addComponent(exitButton, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
  190.                 );
  191.  
  192.         panel.setLayout(titleBarLayout);
  193.  
  194.         panel.addMouseListener(new MouseAdapter() {
  195.             @Override
  196.             public void mousePressed(MouseEvent e) {
  197.                 compCoords = e.getPoint();
  198.             }
  199.             @Override
  200.             public void mouseReleased(MouseEvent e) {
  201.                 compCoords = null;
  202.             }
  203.         });
  204.         panel.addMouseMotionListener(new MouseMotionAdapter() {
  205.             @Override
  206.             public void mouseDragged(MouseEvent e) {
  207.                 Point currCoords = e.getLocationOnScreen();
  208.                 frame.setLocation(currCoords.x - compCoords.x, currCoords.y - compCoords.y);
  209.             }
  210.         });
  211.  
  212.         return panel;
  213.     }
  214.  
  215.    
  216.  
  217.     private JPanel createCardPanel() {
  218.         JPanel panel = new JPanel();
  219.         cardPanelLayout = new CardLayout();
  220.         panel.setLayout(cardPanelLayout);
  221.        
  222.         panel.add(loginCardPanel, "loginCard");
  223.         panel.add(quizCardPanel, "quizCard");
  224.        
  225.         return panel;
  226.     }
  227.    
  228.     private JPanel createLoginCardPanel() {
  229.         JPanel panel = new JPanel();
  230.         GroupLayout loginCardLayout = new GroupLayout(panel);
  231.         loginCardLayout.setHorizontalGroup(
  232.                 loginCardLayout.createParallelGroup(Alignment.LEADING)
  233.                     .addGroup(Alignment.TRAILING, loginCardLayout.createSequentialGroup()
  234.                         .addContainerGap()
  235.                         .addGroup(loginCardLayout.createParallelGroup(Alignment.TRAILING)
  236.                             .addComponent(unnamedLargePanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
  237.                             .addGroup(loginCardLayout.createSequentialGroup()
  238.                                 .addComponent(loginFormSplit, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
  239.                                 .addPreferredGap(ComponentPlacement.RELATED)
  240.                                 .addComponent(loginButtonPanel, GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)))
  241.                         .addContainerGap())
  242.             );
  243.             loginCardLayout.setVerticalGroup(
  244.                 loginCardLayout.createParallelGroup(Alignment.LEADING)
  245.                     .addGroup(Alignment.TRAILING, loginCardLayout.createSequentialGroup()
  246.                         .addComponent(unnamedLargePanel, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
  247.                         .addPreferredGap(ComponentPlacement.RELATED)
  248.                         .addGroup(loginCardLayout.createParallelGroup(Alignment.LEADING, false)
  249.                             .addComponent(loginFormSplit, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  250.                             .addComponent(loginButtonPanel, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
  251.                         .addContainerGap())
  252.             );
  253.         panel.setLayout(loginCardLayout);
  254.         return panel;
  255.     }
  256.    
  257.     private JPanel createQuizCardPanel() {
  258.         JPanel panel = new JPanel();
  259.         answers = new JLabel[4];
  260.         for(int i = 1; i <=4; i++) {
  261.             answers[i-1] = new JLabel("Answer " + i + ":");
  262.         }
  263.        
  264.         answerFields = new JTextField[4];
  265.         for(int i = 0; i < 4; i++) {
  266.             answerFields[i] = new JTextField();
  267.             answerFields[i].setColumns(10);
  268.         }
  269.         questionLabel = new JLabel("Question:");       
  270.         questionField = new JTextField();
  271.         questionField.setColumns(10);
  272.        
  273.         JButton submitQuestionbtn = new JButton("Send Question!");
  274.         submitQuestionbtn.addActionListener(new questionFieldsListener());
  275.  
  276.         GroupLayout gl_panel = new GroupLayout(panel);
  277.         gl_panel.setHorizontalGroup(
  278.             gl_panel.createParallelGroup(Alignment.LEADING)
  279.             .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
  280.                     .addContainerGap(595, Short.MAX_VALUE)
  281.                     .addComponent(submitQuestionbtn)
  282.                     .addContainerGap())
  283.                 .addGroup(gl_panel.createSequentialGroup()
  284.                     .addContainerGap()
  285.                     .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  286.                         .addGroup(gl_panel.createSequentialGroup()
  287.                             .addComponent(questionLabel)
  288.                             .addPreferredGap(ComponentPlacement.RELATED)
  289.                             .addComponent(questionField, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
  290.                             .addContainerGap(308, Short.MAX_VALUE))
  291.                         .addGroup(gl_panel.createSequentialGroup()
  292.                             .addComponent(answers[3], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
  293.                             .addPreferredGap(ComponentPlacement.RELATED)
  294.                             .addComponent(answerFields[3])
  295.                             .addGap(308))
  296.                         .addGroup(gl_panel.createSequentialGroup()
  297.                             .addComponent(answers[2], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
  298.                             .addPreferredGap(ComponentPlacement.RELATED)
  299.                             .addComponent(answerFields[2])
  300.                             .addGap(308))
  301.                         .addGroup(gl_panel.createSequentialGroup()
  302.                             .addComponent(answers[1], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
  303.                             .addPreferredGap(ComponentPlacement.RELATED)
  304.                             .addComponent(answerFields[1])
  305.                             .addGap(308))
  306.                         .addGroup(gl_panel.createSequentialGroup()
  307.                             .addComponent(answers[0])
  308.                             .addGap(11)
  309.                             .addComponent(answerFields[0])
  310.                             .addGap(308))))
  311.         );
  312.         gl_panel.setVerticalGroup(
  313.                 gl_panel.createParallelGroup(Alignment.TRAILING)
  314.                 .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
  315.                         .addContainerGap(263, Short.MAX_VALUE)
  316.                         .addComponent(submitQuestionbtn)
  317.                         .addContainerGap())
  318.                 .addGroup(gl_panel.createSequentialGroup()
  319.                         .addContainerGap()
  320.                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  321.                                 .addComponent(questionLabel)
  322.                                 .addComponent(questionField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  323.                         .addGap(30)
  324.                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  325.                                 .addComponent(answers[0])
  326.                                 .addComponent(answerFields[0], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  327.                         .addGap(30)
  328.                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  329.                                 .addComponent(answers[1])
  330.                                 .addComponent(answerFields[1], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  331.                         .addGap(30)
  332.                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  333.                                 .addComponent(answers[2])
  334.                                 .addComponent(answerFields[2], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  335.                         .addGap(30)
  336.                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  337.                                 .addComponent(answerFields[3], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  338.                                 .addComponent(answers[3]))
  339.                         .addGap(30))
  340.                 );
  341.         panel.setLayout(gl_panel);
  342.  
  343.        
  344.        
  345.         return panel;
  346.     }
  347.    
  348.     private void setUpJFrame() {
  349.         frame = new JFrame("Park Quiz!");
  350.         frameLayout = new GroupLayout(frame.getContentPane());
  351.         frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  352.         frame.setResizable(false);
  353.         frame.setUndecorated(true);
  354.         frame.setBounds(200, 200, 721, 352);
  355.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  356.  
  357.         frameLayout.setHorizontalGroup(
  358.                 frameLayout.createParallelGroup(Alignment.LEADING)
  359.                 .addComponent(titleBar, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
  360.                 .addComponent(cardPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
  361.                 );
  362.         frameLayout.setVerticalGroup(
  363.                 frameLayout.createParallelGroup(Alignment.LEADING)
  364.                 .addGroup(frameLayout.createSequentialGroup()
  365.                         .addComponent(titleBar, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
  366.                         .addPreferredGap(ComponentPlacement.RELATED)
  367.                         .addComponent(cardPanel, GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE))
  368.                 );
  369.  
  370.  
  371.         frame.setLayout(frameLayout);
  372.         frame.add(titleBar);
  373.     }
  374.    
  375.     private class questionFieldsListener implements ActionListener {
  376.  
  377.         public questionFieldsListener() {
  378.             super();
  379.         }
  380.        
  381.         @Override
  382.         public void actionPerformed(ActionEvent e) {
  383.             String question = questionField.getText();
  384.             String[] answers = new String[4];
  385.            
  386.             for(int i = 0; i < 4; i++) {
  387.                 answers[i] = answerFields[i].getText();
  388.             }
  389.             try {
  390.                 model.addQuestionPost(question, answers);
  391.             } catch (IOException e1) {
  392.                 // TODO Auto-generated catch block
  393.                 e1.printStackTrace();
  394.             }
  395.         }
  396.        
  397.     }
  398.    
  399.     private class loginFieldListener implements ActionListener {
  400.        
  401.         public loginFieldListener() {
  402.             super();
  403.         }
  404.        
  405.         @Override
  406.         public void actionPerformed(ActionEvent e) {
  407.             String username = usernameTextField.getText();
  408.             char[] password = passwordField.getPassword();
  409.             model.tryLoginInsecure(username, password);
  410.             if(model.verifyToken())
  411.                 cardPanelLayout.next(cardPanel);
  412.         }
  413.        
  414.     }
  415.  
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement