Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.notjacob.main;
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.imageio.ImageIO;
- import javax.swing.GroupLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- public class Main {
- public static windowstate currentstate;
- public static windowstate laststate;
- public static ArrayList<user> usermap = new ArrayList<user>();
- public static boolean getPasswordMatches(user uzer, String password) {
- return uzer.getPassword().equalsIgnoreCase(password) ? true : false;
- }
- public static boolean getUserExists(String user) {
- for (int i = 0; i < usermap.size(); i++) {
- if (usermap.get(i).getUsername().equalsIgnoreCase(user)) {
- return true;
- }
- }
- return false;
- }
- public static user userFromString(String user) {
- for (int i = 0; i < usermap.size(); i++) {
- if (usermap.get(i).getUsername().equalsIgnoreCase(user)) {
- return usermap.get(i);
- }
- }
- return null;
- }
- public static void main(String[] args) {
- new user("username", "password");
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- initFrame();
- optionsScreen();
- }
- });
- }
- static JFrame frame = new JFrame();
- static PanelBG ControlPanel = new PanelBG();
- static JButton clickButton = new JButton("Confirm");
- static JLabel isvalid = new JLabel("Button not clicked");
- static JTextArea username = new JTextArea();
- static JTextArea password = new JTextArea();
- static JLabel success = new JLabel("Login success!");
- static JButton logout = new JButton("logout");
- static JButton loginButton = new JButton("Login");
- static JButton registerButton = new JButton("Register");
- static JButton clickBu2ton = new JButton("Confirm");
- static JLabel isvalidr = new JLabel("Button not clicked");
- static JTextArea rusername = new JTextArea();
- static JTextArea rpassword = new JTextArea();
- static JButton back = new JButton("back");
- static JLabel configs = new JLabel("Success!");
- public static void initFrame() {
- laststate = windowstate.OPTIONS;
- frame.setTitle("login window");
- frame.setLayout(new BorderLayout());
- frame.add(ControlPanel, BorderLayout.CENTER);
- frame.setSize(new Dimension(800, 650));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- ControlPanel.setLayout(new GroupLayout(ControlPanel));
- ControlPanel.add(back);
- back.setBounds(750, 600, 200, 40);
- frame.setVisible(true);
- back.addActionListener((e) -> {
- executeFor(laststate);
- frame.revalidate();
- frame.repaint();
- });
- }
- public static void loingScreen() {
- isvalid.setText("Button not clicked");
- isvalidr.setText("Button not clicked");
- currentstate = windowstate.LOGIN;
- ControlPanel.removeAll();
- ControlPanel.add(back);
- ControlPanel.add(isvalid);
- ControlPanel.add(username);
- ControlPanel.add(password);
- ControlPanel.add(clickButton);
- username.setBounds(100, 100, 200, 40);
- password.setBounds(100, 200, 200, 40);
- isvalid.setBounds(100, 340, 400, 40);
- clickButton.setBounds(100, 200, 340, 40);
- frame.revalidate();
- frame.repaint();
- clickButton.addActionListener((e) -> {
- if (!username.getText().isEmpty()) {
- if (getUserExists(username.getText())) {
- if (!password.getText().isEmpty()) {
- if (getPasswordMatches(userFromString(username.getText()), password.getText())) {
- laststate = windowstate.LOGIN;
- login();
- } else {
- isvalid.setText("invalid password!");
- }
- } else {
- isvalid.setText("add a password!");
- }
- } else {
- isvalid.setText("user is not valid!");
- }
- } else {
- isvalid.setText("need a user!");
- }
- });
- }
- public static void optionsScreen() {
- isvalid.setText("Button not clicked");
- isvalidr.setText("Button not clicked");
- currentstate = windowstate.OPTIONS;
- ControlPanel.removeAll();
- ControlPanel.add(back);
- ControlPanel.add(loginButton);
- ControlPanel.add(registerButton);
- loginButton.setBounds(300, 300, 300, 40);
- registerButton.setBounds(300, 400, 300, 40);
- frame.revalidate();
- frame.repaint();
- loginButton.addActionListener((e) -> {
- ControlPanel.remove(loginButton);
- ControlPanel.remove(registerButton);
- ControlPanel.remove(isvalidr);
- ControlPanel.remove(isvalid);
- loingScreen();
- });
- registerButton.addActionListener((e) -> {
- ControlPanel.remove(loginButton);
- ControlPanel.remove(registerButton);
- ControlPanel.remove(isvalidr);
- ControlPanel.remove(isvalid);
- laststate = windowstate.OPTIONS;
- doRegisterScreen();
- });
- }
- public static void doRegisterScreen() {
- isvalid.setText("Button not clicked");
- isvalidr.setText("Button not clicked");
- currentstate = windowstate.REGISTER;
- ControlPanel.removeAll();
- ControlPanel.add(back);
- ControlPanel.add(rpassword);
- ControlPanel.add(rusername);
- ControlPanel.add(isvalidr);
- ControlPanel.add(clickBu2ton);
- rpassword.setBounds(100, 200, 200, 40);
- rusername.setBounds(100, 100, 200, 40);
- isvalidr.setBounds(100, 340, 400, 40);
- clickBu2ton.setBounds(100, 200, 340, 40);
- frame.revalidate();
- frame.repaint();
- clickBu2ton.addActionListener((e) -> {
- if (!rusername.getText().isEmpty()) {
- if (!getUserExists(rusername.getText())) {
- if (!rpassword.getText().isEmpty()) {
- new user(rusername.getText(), rpassword.getText());
- success.setText("Registered!");
- success.setBounds(40, 40, 100, 40);
- ControlPanel.add(success);
- ControlPanel.remove(rpassword);
- ControlPanel.remove(rusername);
- ControlPanel.remove(clickBu2ton);
- laststate = windowstate.REGISTER;
- optionsScreen();
- } else {
- isvalidr.setText("Need a password!");
- }
- } else {
- isvalidr.setText("That user already exists!");
- }
- } else {
- isvalidr.setText("Need a username!");
- }
- });
- }
- public static void login() {
- isvalid.setText("Button not clicked");
- isvalidr.setText("Button not clicked");
- currentstate = windowstate.LOGGEDIN;
- ControlPanel.removeAll();
- ControlPanel.add(back);
- username.setText("");
- password.setText("");
- ControlPanel.remove(username);
- ControlPanel.remove(clickButton);
- ControlPanel.remove(isvalid);
- ControlPanel.remove(password);
- success.setText("Logged in!");
- ControlPanel.add(success);
- ControlPanel.add(logout);
- success.setBounds(300, 300, 300, 40);
- logout.setBounds(300, 380, 300, 40);
- frame.revalidate();
- frame.repaint();
- logout.addActionListener((e) -> {
- laststate = windowstate.OPTIONS;
- logout();
- });
- }
- public static void logout() {
- ControlPanel.remove(success);
- ControlPanel.remove(logout);
- success.setText("Logged out!");
- success.setBounds(40, 40, 100, 40);
- ControlPanel.add(success);
- laststate = windowstate.OPTIONS;
- optionsScreen();
- }
- enum windowstate {
- OPTIONS,
- SELECT,
- LOGIN,
- REGISTER,
- LOGGEDIN;
- }
- public static void executeFor(windowstate state) {
- switch(state) {
- case OPTIONS:
- optionsScreen();
- break;
- case SELECT:
- optionsScreen();
- break;
- case LOGIN:
- loingScreen();
- break;
- case REGISTER:
- doRegisterScreen();
- break;
- case LOGGEDIN:
- optionsScreen();
- break;
- default:
- optionsScreen();
- }
- }
- }
- class user {
- private final String username;
- private final String password;
- public user(String username, String password) {
- this.username = username;
- this.password = password;
- Main.usermap.add(this);
- }
- public String getPassword() {
- return this.password;
- }
- public String getUsername() {
- return this.username;
- }
- }
- @SuppressWarnings("serial")
- class PanelBG extends JPanel {
- private Image backgroundImage;
- public PanelBG() {
- /* try {
- backgroundImage = ImageIO.read(new File("/Users/georgeblair/Pictures/heic0503a.jpg"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- */
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(backgroundImage, 0, 0, this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement