Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. package duel;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Container;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextArea;
  19. import javax.swing.JTextField;
  20. import javax.swing.JTextPane;
  21.  
  22. import duel.util.Console;
  23. import duel.util.Security;
  24.  
  25. /**
  26.  *
  27.  * @author Ron Bodnar
  28.  *
  29.  */
  30.  
  31. public class Gui extends JFrame implements KeyListener {
  32.    
  33.     private JButton loginButton = new JButton("Login");
  34.     private JLabel usernameLabel = new JLabel();
  35.     private JLabel passwordLabel = new JLabel();
  36.     private JTextField usernameField = new JTextField();
  37.     private JPasswordField passwordField = new JPasswordField();
  38.    
  39.     public JScrollPane scroll;
  40.     public JTextArea console;
  41.            
  42.     public static void main(String[] args) {
  43.         new Gui().setVisible(true);
  44.     }
  45.            
  46.     public Gui() {
  47.         super("Login");
  48.         JOptionPane.showMessageDialog(this, "Please login to the game");
  49.         initialize(getContentPane());
  50.         //initializeComponents(getContentPane());
  51.     }
  52.    
  53.     public void initialize(Container pane) {
  54.         setResizable(false);
  55.         setSize(200, 150);
  56.  
  57.         usernameLabel.setBounds(5, 5, 80, 20);
  58.         usernameLabel.setText("Username: ");
  59.        
  60.         passwordLabel.setBounds(5, 40, 80, 20);
  61.         passwordLabel.setText("Password: ");
  62.        
  63.         usernameField.setBounds(80, 7, 90, 20);
  64.         usernameField.addKeyListener(this);
  65.        
  66.         passwordField.setBounds(80, 42, 90, 20);
  67.         passwordField.addKeyListener(this);
  68.        
  69.         loginButton.setBounds(55, 77, 70, 25);
  70.         loginButton.addActionListener(new ActionListener() {
  71.  
  72.             @Override
  73.             public void actionPerformed(ActionEvent e) {
  74.                 System.out.println(passwordField.getPassword().hashCode());
  75.                 if (checkLogin(usernameField.getText(), passwordField.getPassword())) {
  76.                     initializeComponents(getContentPane());
  77.                 } else {
  78.                     JOptionPane.showMessageDialog(new JFrame(), "Invalid password!");
  79.                 }
  80.             }
  81.         });
  82.        
  83.         pane.add(loginButton);
  84.         pane.add(passwordField);
  85.         pane.add(passwordLabel);
  86.         pane.add(usernameLabel);
  87.         pane.add(usernameField);
  88.         pane.add(new JPanel());
  89.     }
  90.            
  91.     public void initializeComponents(Container pane) {
  92.         setSize(375, 300);
  93.         setTitle("Duel - Logged in as " + Player.playerName);
  94.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  95.         pane.setLayout(new BorderLayout());
  96.         setResizable(false);
  97.         addConsole(pane);
  98.         addText(pane);
  99.         pane.add(new JPanel());
  100.     }
  101.    
  102.     public void addText(Container pane) {
  103.         JTextPane enemyPane = new JTextPane();
  104.         enemyPane.setEditable(false);
  105.         enemyPane.setText("Enemy's health:\n999\n\nYour health:\n999");
  106.         enemyPane.setBounds(5, 5, 100, 85);
  107.         enemyPane.setBackground(new Color(238, 238, 238));
  108.         pane.add(enemyPane);
  109.        
  110.         JTextPane playerPane = new JTextPane();
  111.         playerPane.setEditable(false);
  112.         playerPane.setText("Wins: 1\n\nLosses: 1\n\nRatio: 1.0");
  113.         playerPane.setBounds(125, 5, 80, 100);
  114.         playerPane.setBackground(new Color(238, 238, 238));
  115.         pane.add(playerPane);
  116.        
  117.         JTextPane statPane = new JTextPane();
  118.         statPane.setEditable(false);
  119.         statPane.setText("Level: 1\n\nExperience: 14000000\n\nAchievements: 1");
  120.         statPane.setBounds(230, 5, 200, 100);
  121.         statPane.setBackground(new Color(238, 238, 238));
  122.         pane.add(statPane);
  123.     }
  124.    
  125.     public void addConsole(Container pane) {
  126.         JTextArea consoleText = new JTextArea(10, 20);
  127.         JScrollPane scroll = new JScrollPane(consoleText, 20, 31);
  128.         consoleText.setForeground(Color.WHITE);
  129.         consoleText.setBackground(Color.BLACK);
  130.         System.setOut(new Console(consoleText, scroll));
  131.         pane.add(scroll, BorderLayout.SOUTH);
  132.     }
  133.    
  134.     public boolean checkLogin(String user, char[] pass) {
  135.         return Security.passwordMatches(user, pass);
  136.     }
  137.  
  138.     @Override
  139.     public void keyPressed(KeyEvent keyEvent) {
  140.         if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
  141.             if (usernameField.isFocusOwner()) {
  142.                 passwordField.requestFocus();
  143.             } else {
  144.                 if (usernameField.getText().length() == 0) {
  145.                     usernameField.requestFocus();
  146.                 } else {
  147.                     if (passwordField.getPassword().length == 0) {
  148.                         JOptionPane.showMessageDialog(this, "Please enter your password!");
  149.                     } else {
  150.                         if (checkLogin(usernameField.getText(), passwordField.getPassword())) {
  151.                             initializeComponents(getContentPane());
  152.                         } else {
  153.                             JOptionPane.showMessageDialog(new JFrame(), "Invalid password!");
  154.                         }
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.     @Override
  162.     public void keyReleased(KeyEvent arg0) {
  163.     }
  164.  
  165.     @Override
  166.     public void keyTyped(KeyEvent arg0) {
  167.     }
  168.    
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement