Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import javax.swing.border.Border;
- /**
- * Created by Vishva on 11/10/2016.
- */
- public class PsychView extends JFrame {
- private Controller controller;
- // Variables
- private String userToken;
- // Getter methods for instance variables
- public String getUser() {
- return usernameField.getText();
- }
- public String getUserToken()
- {
- return userToken;
- }
- public String getPassword() {
- String pass = "";
- char[] passFake = passwordField.getPassword();
- for(int i = 0; i < passFake.length; i++)
- {
- pass += passFake[i];
- }
- return pass;
- }
- public String getNewUser() {
- return newUsernameField.getText();
- }
- public char[] getNewPassword() {
- return newPasswordField.getPassword();
- }
- public void setErrorMessage(String message) {
- errorMessage.setText(message);
- }
- public void setErrorMessage2(String message) {
- errorMessage2.setText(message);
- }
- // Error panel (panel that displays error messages)
- JPanel errorPanel = new JPanel();
- JLabel errorMessage = new JLabel("");
- JButton goBack = new JButton("Return to previous page");
- JPanel errorPanel2 = new JPanel();
- JLabel errorMessage2 = new JLabel("");
- JButton goBack2 = new JButton("Return to previous page");
- // Main panel
- JPanel mainPanel = new JPanel();
- CardLayout layout = new CardLayout();
- // Login panel (AKA first panel to appear on game start)
- JPanel loginRegisterPanel = new JPanel();
- JButton login = new JButton("Login");
- JButton register = new JButton("Register");
- JLabel usernamePrompt = new JLabel("Username");
- JLabel passwordPrompt = new JLabel("Password");
- JTextField usernameField = new JTextField();
- JPasswordField passwordField = new JPasswordField();
- // Register new user panel (if user DNE and pushes the register button)
- JPanel registerPanel = new JPanel();
- JButton registerNew = new JButton("Register");
- JLabel newUsernamePrompt = new JLabel("Username");
- JLabel newPasswordPrompt = new JLabel("Password");
- JTextField newUsernameField = new JTextField();
- JPasswordField newPasswordField = new JPasswordField();
- // Start new game + join a game panel (for once a user logs in)
- JPanel startJoinPanel = new JPanel();
- JButton startNewGame = new JButton("Start new game");
- JButton joinGame = new JButton("Join a game");
- // Host lobby panel (for someone who decides to start a new game)
- JPanel hostLobby = new JPanel();
- JLabel keyMessage = new JLabel("Others should use the key below to join");
- JTextArea code = new JTextArea();
- JPanel subPanel1 = new JPanel(new GridLayout(1, 0));
- JTextArea members = new JTextArea(10, 10);
- JScrollPane scrollPane = new JScrollPane(members);
- JPanel subPanel2 = new JPanel();
- JButton start = new JButton("Start");
- // Join lobby panel (for someone who decides to join an existing game)
- JPanel joinLobby = new JPanel();
- JLabel joinMessage = new JLabel("Enter the key to join the game");
- JTextArea codeField = new JTextArea();
- JButton getMeIn = new JButton("Join game");
- JPanel joinLobbySub1 = new JPanel();
- JButton joinExistingGame = new JButton("Join game");
- // Waiting for leader to start game panel (for those waiting on leader to start game)
- JPanel waitingLobby = new JPanel();
- JLabel waitingMessage = new JLabel("Waiting for leader...");
- JPanel optionPanel = new JPanel();
- JLabel prompt = new JLabel("What is the word for: ");
- JPanel subPanel3 = new JPanel(new GridLayout(1,0));
- JTextArea area = new JTextArea(10,10);
- JScrollPane scrollPane2 = new JScrollPane(area);
- JPanel subPanel4 = new JPanel();
- JTextField suggestion = new JTextField();
- JButton submitSuggestion = new JButton("Submit Suggestion");
- JPanel resultPanel = new JPanel();
- JPanel roundResult = new JPanel(new GridLayout(1,0));
- JTextArea results = new JTextArea(10,10);
- JScrollPane scrollPane5 = new JScrollPane(results);
- JPanel overallResults = new JPanel(new GridLayout(1,0));
- JTextArea overall = new JTextArea(10,10);
- JScrollPane scrollPane6 = new JScrollPane(overall);
- JPanel pickPanel = new JPanel();
- JLabel pickMessage = new JLabel("Pick your option below");
- JButton submitOption = new JButton("Submit Option");
- JRadioButton rOne = new JRadioButton("");
- JRadioButton rTwo = new JRadioButton("");
- JRadioButton rThree = new JRadioButton("");
- JRadioButton rFour = new JRadioButton("");
- ButtonGroup group = new ButtonGroup();
- public PsychView(Controller controller) {
- this.controller = controller;
- pickPanel.setBorder(BorderFactory.createTitledBorder("User: Pick an option"));
- pickPanel.setLayout(null);
- pickMessage.setBounds(20, 20, 150, 20);
- rOne.setBounds(50, 150, 110, 20);
- rTwo.setBounds(50, 175, 110, 20);
- rThree.setBounds(50, 200, 110, 20);
- rFour.setBounds(50, 225, 110, 20);
- submitOption.setBounds(40, 400, 150, 20);
- group.add(rOne);
- group.add(rTwo);
- group.add(rThree);
- group.add(rFour);
- pickPanel.add(rOne);
- pickPanel.add(rTwo);
- pickPanel.add(rThree);
- pickPanel.add(rFour);
- pickPanel.add(submitOption);
- pickPanel.add(pickMessage);
- area.setEditable(false);
- roundResult.setBorder(BorderFactory.createTitledBorder("Round Results"));
- roundResult.setBounds(10,70,220,200);
- roundResult.add(scrollPane5);
- overallResults.setBorder(BorderFactory.createTitledBorder("Overall Results"));
- overallResults.setBounds(10,300,220,200);
- overallResults.add(scrollPane6);
- resultPanel.add(roundResult);
- resultPanel.add(overallResults);
- optionPanel.setBorder(BorderFactory.createTitledBorder("Options"));
- optionPanel.setLayout(null);
- prompt.setBounds(20,20,200,50);
- optionPanel.add(prompt);
- subPanel3.add(scrollPane2);
- subPanel3.setBounds(10,65,235,225);
- subPanel4.setBorder(BorderFactory.createTitledBorder("Your Suggestion"));
- subPanel4.setLayout(null);
- submitSuggestion.setBounds(20, 170, 200, 20);
- suggestion.setBounds(4,20,224,20);
- subPanel4.add(suggestion);
- subPanel4.add(submitSuggestion);
- subPanel4.setBounds(10,300, 230, 200);
- optionPanel.add(subPanel3);
- optionPanel.add(subPanel4);
- //Add components of error panel to panel itself
- errorPanel.add(errorMessage);
- errorPanel.add(goBack);
- errorPanel2.add(errorMessage2);
- errorPanel2.add(goBack2);
- // Configure components of error panel
- errorPanel.setBorder(BorderFactory.createTitledBorder("Error"));
- errorPanel.setLayout(null);
- errorMessage.setBounds(50, 150, 400, 50);
- goBack.setBounds(18, 450, 110, 20);
- errorPanel2.setBorder(BorderFactory.createTitledBorder("Error"));
- errorPanel2.setLayout(null);
- errorMessage2.setBounds(50, 150, 400, 50);
- goBack2.setBounds(18, 450, 110, 20);
- // Add components of login panel to panel itself
- loginRegisterPanel.add(login);
- loginRegisterPanel.add(register);
- loginRegisterPanel.add(usernamePrompt);
- loginRegisterPanel.add(passwordPrompt);
- loginRegisterPanel.add(usernameField);
- loginRegisterPanel.add(passwordField);
- // Configure components of login panel
- loginRegisterPanel.setBorder(BorderFactory.createTitledBorder("Welcome"));
- loginRegisterPanel.setLayout(null);
- login.setBounds(18, 450, 110, 20);
- register.setBounds(150, 450, 110, 20);
- usernamePrompt.setBounds(16, 203, 110, 10);
- passwordPrompt.setBounds(16, 223, 110, 10);
- usernameField.setBounds(100, 200, 110, 20);
- passwordField.setBounds(100, 220, 110, 20);
- // Add components of register panel to panel itself
- registerPanel.add(registerNew);
- registerPanel.add(newUsernamePrompt);
- registerPanel.add(newPasswordPrompt);
- registerPanel.add(newUsernameField);
- registerPanel.add(newPasswordField);
- // Configure components of new user registration panel
- registerPanel.setBorder(BorderFactory.createTitledBorder("Create new user"));
- registerPanel.setLayout(null);
- registerNew.setBounds(100, 450, 110, 20);
- newUsernamePrompt.setBounds(16, 203, 110, 10);
- newPasswordPrompt.setBounds(16, 223, 110, 10);
- newUsernameField.setBounds(100, 200, 110, 20);
- newPasswordField.setBounds(100, 220, 110, 20);
- // Add components of start new game + join game panel to panel itself
- startJoinPanel.add(startNewGame);
- startJoinPanel.add(joinGame);
- // Configure components of start new game + join game panel
- startJoinPanel.setBorder(BorderFactory.createTitledBorder("User: " /* username code here */));
- startJoinPanel.setLayout(null);
- startNewGame.setBounds(60, 150, 150, 20);
- joinGame.setBounds(60, 200, 150, 20);
- // Add components of host lobby panel to panel itself
- subPanel1.add(scrollPane);
- subPanel2.add(start);
- hostLobby.add(keyMessage);
- hostLobby.add(code);
- hostLobby.add(subPanel1);
- hostLobby.add(subPanel2);
- // Configure components of host lobby panel
- hostLobby.setBorder(BorderFactory.createTitledBorder("Wait for friends to join!"));
- hostLobby.setLayout(null);
- keyMessage.setBounds(10, 150, 300, 20);
- code.setBounds(75, 180, 110, 20);
- code.setEditable(false);
- subPanel1.setBorder(BorderFactory.createTitledBorder("Joined Members: "));
- subPanel1.setBounds(10, 225, 250, 225);
- members.setEditable(false);
- subPanel2.setBounds(105, 450, 50, 30);
- start.setBounds(350, 10, 110, 10);
- // Add components of join lobby panel to panel itself
- joinLobbySub1.add(joinExistingGame);
- joinLobby.add(joinMessage);
- joinLobby.add(getMeIn);
- joinLobby.add(codeField);
- joinLobby.add(joinLobbySub1);
- // Configure components of join lobby panel
- joinLobby.setBorder(BorderFactory.createTitledBorder("User: -- Join an existing game"));
- joinLobby.setLayout(null);
- joinExistingGame.setBounds(350, 10, 110, 20);
- joinLobbySub1.setBounds(95, 180, 70, 20);
- joinMessage.setBounds(50, 150, 300, 20);
- codeField.setBounds(95, 180, 70, 20);
- getMeIn.setBounds(75, 210, 120, 20);
- // Add components of waiting for leader panel to panel itself
- waitingLobby.add(waitingMessage);
- // Configure components of waiting for leader panel
- waitingLobby.setBorder(BorderFactory.createTitledBorder("User: "));
- waitingLobby.setLayout(null);
- waitingMessage.setBounds(50, 150, 300, 20);
- // Add login panel and new user registration panel to main panel
- mainPanel.setLayout(layout);
- mainPanel.add(loginRegisterPanel, "1");
- mainPanel.add(registerPanel, "2");
- mainPanel.add(startJoinPanel, "3");
- mainPanel.add(hostLobby, "4");
- mainPanel.add(joinLobby, "5");
- mainPanel.add(waitingLobby, "6");
- mainPanel.add(errorPanel, "Error");
- mainPanel.add(errorPanel2, "Error2");
- mainPanel.add(optionPanel, "7");
- mainPanel.add(pickPanel, "8");
- mainPanel.add(resultPanel, "9");
- // Add action listener to to register button on login panel
- register.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.show(mainPanel, "2");
- }
- });
- // Add action listener to register new user button on register panel
- registerNew.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String pass = "";
- String user = "";
- char[] passFake = getNewPassword();
- for (int i = 0; i < passFake.length; i++) {
- pass += passFake[i];
- }
- user = getNewUser();
- String serverMessage = controller.Register(user, pass);
- int index = serverMessage.indexOf("-", 25);
- String importantPart = serverMessage.substring(25, index);
- goBack.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.show(mainPanel, "2");
- }
- });
- if (importantPart.equals("INVALIDMESSAGEFORMAT")) {
- setErrorMessage("Invalid message format");
- layout.show(mainPanel, "Error");
- }
- else if (importantPart.equals("INVALIDUSERNAME")) {
- setErrorMessage("Invalid username");
- layout.show(mainPanel, "Error");
- }
- else if (importantPart.equals("INVALIDUSERPASSWORD")) {
- setErrorMessage("Invalid user password");
- layout.show(mainPanel, "Error");
- }
- else if (importantPart.equals("USERALREADYEXISTS")) {
- setErrorMessage("User already exists");
- layout.show(mainPanel, "Error");
- }
- else if (importantPart.equals("SUCCESS")) {
- layout.show(mainPanel, "1");
- }
- }
- });
- // Add action listener to login an existing user button on register panel
- login.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if(controller == null)
- {
- System.out.println("HI");
- }
- String msg = controller.Login(getUser(), getPassword());
- int index = msg.indexOf("-");
- String msg1 = msg.substring(0, index);
- userToken = msg.substring(index+2, msg.length());
- System.out.println(userToken);
- goBack.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.show(mainPanel, "1");
- }
- });
- if(msg1.equals("SUCCESS")) {
- startJoinPanel.setBorder(BorderFactory.createTitledBorder(getUser()));
- layout.show(mainPanel, "3");
- }
- else if (msg1.equals("UNKNOWNUSER"))
- {
- setErrorMessage("Invalid Username");
- layout.show(mainPanel, "Error");
- }
- else if(msg1.equals("INVALIDMESSAGEFORMAT"))
- {
- setErrorMessage("Request does not comply.");
- layout.show(mainPanel, "Error");
- }
- else if(msg1.equals("INVALIDUSERPASSWORD"))
- {
- setErrorMessage("Invalid Password");
- layout.show(mainPanel, "Error");
- }
- else if(msg1.equals("USERALREADYLOGGEDIN"))
- {
- setErrorMessage("User already logged in.");
- layout.show(mainPanel, "Error");
- }
- }
- });
- // Add action listener to start a new game on start new game + join game panel
- startNewGame.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Thread t = Thread.currentThread();
- String str1 = controller.startGame(getUserToken());
- int index = str1.indexOf("-");
- String str = str1.substring(0, index);
- String gameToken = str1.substring(index+2, str1.length());
- System.out.println(gameToken);
- if(str.equals("SUCCESS")) {
- code.setText(gameToken);
- hostLobby.setBorder(BorderFactory.createTitledBorder(getUser() /* username code here */));
- layout.show(mainPanel, "4");
- SwingWorker worker = new SwingWorker<String, Void>() {
- @Override
- protected String doInBackground() throws Exception {
- String str5 = controller.keepReading();
- int index = str5.indexOf("-", 16);
- str5 = str5.substring(16,index);
- members.append(str5 + "\n");
- return str5;
- }
- };
- worker.execute();
- SwingWorker worker2 = new SwingWorker<String, Void>() {
- @Override
- protected String doInBackground() throws Exception {
- String str5 = controller.keepReading();
- int index = str5.indexOf("-", 16);
- str5 = str5.substring(16,index);
- members.append(str5 + "\n");
- return str5;
- }
- };
- worker2.execute();
- }
- else
- {
- layout.show(mainPanel, "3");
- }
- }
- });
- // Add action listener to join a new game on start new game + join game panel
- joinGame.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.show(mainPanel, "5");
- }
- });
- // Add action listener to join existing game on join game panel
- getMeIn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String status = controller.joinGame(getUserToken(), codeField.getText());
- goBack2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.show(mainPanel, "5");
- }
- });
- if(status.equals("SUCCESS")) {
- layout.show(mainPanel, "6");
- }
- else if(status.equals("USERNOTLOGGEDIN"))startJoinPanel.setBorder(BorderFactory.createTitledBorder("User: " /* username code here */));
- {
- setErrorMessage2("Invalid user token");
- layout.show(mainPanel, "Error2");
- }
- else if(status.equals("GAMEKEYNOTFOUND"))
- {
- setErrorMessage2("Invalid game token");
- layout.show(mainPanel, "Error2");
- }
- else if(status.equals("FAILURE"))
- {
- setErrorMessage2("User already playing the game.");
- layout.show(mainPanel, "Error2");
- }
- SwingWorker worker3 = new SwingWorker<String, Void>() {
- @Override
- protected String doInBackground() throws Exception {
- String str6 = controller.keepReading();
- int index = str6.indexOf("-", 13);
- String str7 = str6.substring(13, index);
- String str8 = str6.substring(index+2, str6.length());
- System.out.println(str7);
- area.setText(str7);
- layout.show(mainPanel, "7");
- return str6;
- }
- };
- worker3.execute();
- }
- });
- start.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- goBack2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.show(mainPanel, "4");
- }
- });
- String serverMessage = controller.startGame(getUserToken(), code.getText());
- int index = serverMessage.indexOf("-", 13);
- String importantPart = serverMessage.substring(13,index);
- String correctAnswer = serverMessage.substring(index+2, serverMessage.length());
- if (importantPart.equals("USERNOTLOGGEDIN")) {
- setErrorMessage2("User not logged in");
- layout.show(mainPanel, "Error2");
- } else if (importantPart.equals("INVALIDGAMETOKEN")) {
- setErrorMessage2("Invalid game token");
- layout.show(mainPanel, "Error2");
- } else if (importantPart.equals("USERNOTGAMELEADER")) {
- setErrorMessage2("User already playing the game");
- layout.show(mainPanel, "Error2");
- }
- else {
- area.setText(importantPart);
- layout.show(mainPanel, "7");
- }
- }
- });
- // Set up frame and add main panel to GUI Frame
- setTitle("Psych!");
- add(mainPanel);
- setLocation(900, 200);
- setSize(275, 550);
- setResizable(false);
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- layout.show(mainPanel, "1");
- setVisible(true);
- }
- public static void main(String[] args) {
- Controller controller = new Controller();
- new PsychView(controller); // Show the GUI
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement