Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package duel;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.JTextPane;
- import duel.util.Console;
- import duel.util.Security;
- /**
- *
- * @author Ron Bodnar
- *
- */
- public class Gui extends JFrame implements KeyListener {
- private JButton loginButton = new JButton("Login");
- private JLabel usernameLabel = new JLabel();
- private JLabel passwordLabel = new JLabel();
- private JTextField usernameField = new JTextField();
- private JPasswordField passwordField = new JPasswordField();
- public JScrollPane scroll;
- public JTextArea console;
- public static void main(String[] args) {
- new Gui().setVisible(true);
- }
- public Gui() {
- super("Login");
- JOptionPane.showMessageDialog(this, "Please login to the game");
- initialize(getContentPane());
- //initializeComponents(getContentPane());
- }
- public void initialize(Container pane) {
- setResizable(false);
- setSize(200, 150);
- usernameLabel.setBounds(5, 5, 80, 20);
- usernameLabel.setText("Username: ");
- passwordLabel.setBounds(5, 40, 80, 20);
- passwordLabel.setText("Password: ");
- usernameField.setBounds(80, 7, 90, 20);
- usernameField.addKeyListener(this);
- passwordField.setBounds(80, 42, 90, 20);
- passwordField.addKeyListener(this);
- loginButton.setBounds(55, 77, 70, 25);
- loginButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println(passwordField.getPassword().hashCode());
- if (checkLogin(usernameField.getText(), passwordField.getPassword())) {
- initializeComponents(getContentPane());
- } else {
- JOptionPane.showMessageDialog(new JFrame(), "Invalid password!");
- }
- }
- });
- pane.add(loginButton);
- pane.add(passwordField);
- pane.add(passwordLabel);
- pane.add(usernameLabel);
- pane.add(usernameField);
- pane.add(new JPanel());
- }
- public void initializeComponents(Container pane) {
- setSize(375, 300);
- setTitle("Duel - Logged in as " + Player.playerName);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- pane.setLayout(new BorderLayout());
- setResizable(false);
- addConsole(pane);
- addText(pane);
- pane.add(new JPanel());
- }
- public void addText(Container pane) {
- JTextPane enemyPane = new JTextPane();
- enemyPane.setEditable(false);
- enemyPane.setText("Enemy's health:\n999\n\nYour health:\n999");
- enemyPane.setBounds(5, 5, 100, 85);
- enemyPane.setBackground(new Color(238, 238, 238));
- pane.add(enemyPane);
- JTextPane playerPane = new JTextPane();
- playerPane.setEditable(false);
- playerPane.setText("Wins: 1\n\nLosses: 1\n\nRatio: 1.0");
- playerPane.setBounds(125, 5, 80, 100);
- playerPane.setBackground(new Color(238, 238, 238));
- pane.add(playerPane);
- JTextPane statPane = new JTextPane();
- statPane.setEditable(false);
- statPane.setText("Level: 1\n\nExperience: 14000000\n\nAchievements: 1");
- statPane.setBounds(230, 5, 200, 100);
- statPane.setBackground(new Color(238, 238, 238));
- pane.add(statPane);
- }
- public void addConsole(Container pane) {
- JTextArea consoleText = new JTextArea(10, 20);
- JScrollPane scroll = new JScrollPane(consoleText, 20, 31);
- consoleText.setForeground(Color.WHITE);
- consoleText.setBackground(Color.BLACK);
- System.setOut(new Console(consoleText, scroll));
- pane.add(scroll, BorderLayout.SOUTH);
- }
- public boolean checkLogin(String user, char[] pass) {
- return Security.passwordMatches(user, pass);
- }
- @Override
- public void keyPressed(KeyEvent keyEvent) {
- if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
- if (usernameField.isFocusOwner()) {
- passwordField.requestFocus();
- } else {
- if (usernameField.getText().length() == 0) {
- usernameField.requestFocus();
- } else {
- if (passwordField.getPassword().length == 0) {
- JOptionPane.showMessageDialog(this, "Please enter your password!");
- } else {
- if (checkLogin(usernameField.getText(), passwordField.getPassword())) {
- initializeComponents(getContentPane());
- } else {
- JOptionPane.showMessageDialog(new JFrame(), "Invalid password!");
- }
- }
- }
- }
- }
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement