package package.here;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import com.Bc1151.Darkrealm.Game;
import com.Bc1151.Darkrealm.comptent.DBConnect;
import com.Bc1151.Darkrealm.input.GameKeyController;
public class PasswordField extends JFrame {
private JPanel passwordPanel;
private JPasswordField passwordField;
private JLabel passwordLabel;
public static GameKeyController gkc = new GameKeyController();
private JButton userConfirmationBtn;
private JButton submenu;
private Image icon;
private ImageIcon frameBackground;
DBConnect db = new DBConnect();
String sql = db.sql;
JButton closebutton = new JButton("Close");
String sqlusername = db.username;
JLabel usernamelabel = new JLabel("Username:");
JTextField username = new JTextField(10);
String sqlpassword = db.password;
String password = "password1";
Statement stmt = db.stmt;
public String loggedonS = "false";
public PasswordField() {
//loggedon = false;
icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
frameBackground = new ImageIcon("c:/pics/passwordbck2.jpg");
passwordField = new JPasswordField();
passwordLabel = new JLabel("Password: ");
userConfirmationBtn = new JButton("Enter");
submenu = new JButton("Submenu");
// setLayout(new GridLayout(3,2));
// add(usernamelabel);
//add(username);
// add(passwordLabel);
//add(passwordField);
//add(userConfirmationBtn);
//add(cancel);
initComponents();
}
private void initComponents() {
//loggedon = false;
Game game = new Game();
game.frame.setVisible(false);
// set the properties of this label
passwordLabel.setBounds(12, 63, 80, 20);
passwordLabel.setFont(new Font("Monospaced", Font.BOLD, 12));
passwordLabel.setForeground(Color.BLACK);
usernamelabel.setBounds(12, 13, 80, 20);
usernamelabel.setFont(new Font("Monospaced", Font.BOLD, 12));
usernamelabel.setForeground(Color.BLACK);
// set the properties and actions of this text field
username.setBounds(88, 13, 185, 20);
passwordField.setBounds(88, 63, 185, 20);
// set the properties and actions of this button
Action pass = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// retrive the password data from the password field
char[] passwordChars = passwordField.getPassword();
String usernameChars = username.getText();
String passwordStr = "";
String usernameStr = "";
usernameStr = usernameChars;
// convert the returned password characters as string
for (int x = 0; x <= passwordChars.length - 1; x++) {
passwordStr = passwordStr + passwordChars[x];
}
// check if the password entered is valid
// if (Password.isPasswordAuthentic(passwordStr)) {
// do something here
//}
//TODO change to mysql
if (passwordStr.equals(sqlpassword) && usernameStr.equalsIgnoreCase(sqlusername)){
disableWindow();
loggedonS = "true";
}
else if (passwordStr.equals("") && usernameStr.equals("")) {
System.out.println("you typed nothing :P");
// do something here
} else if (usernameStr.equals("")) {
System.out.println("please enter a username");
} else if (passwordStr.equals("")) {
System.out.println("please enter a password");
}
else {
System.out.println("incorrect username or password");
// do something here
}
}
};
closebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("you clicked close");
try {
close();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
userConfirmationBtn.addActionListener(pass);
userConfirmationBtn.setMnemonic(KeyEvent.VK_E);
userConfirmationBtn.setBounds(88, 105, 90, 20);
userConfirmationBtn.setFont(new Font("Monospaced", Font.BOLD, 11));
userConfirmationBtn.setBackground(Color.LIGHT_GRAY);
userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
JComponent.WHEN_IN_FOCUSED_WINDOW);
userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
JComponent.WHEN_IN_FOCUSED_WINDOW);
closebutton.setBounds(88, 135, 90, 20);
closebutton.setFont(new Font("Monospaced", Font.BOLD, 11));
// closebutton.setMnemonic(KeyEvent.VK_E);
closebutton.setBackground(Color.LIGHT_GRAY);
closebutton.addKeyListener(gkc.login);
passwordPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
// Scale image to size of component
Dimension d = getSize();
g.drawImage(frameBackground.getImage(), 0, 0, d.width, d.height, null);
super.paintComponent(g);
}
};
passwordPanel.setOpaque(false);
// set this panel's layout to null for absolute positioning of
// components
passwordPanel.setLayout(null);
// add the components to this panel
passwordPanel.add(username);
passwordPanel.add(usernamelabel);
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
passwordPanel.add(userConfirmationBtn);
passwordPanel.add(closebutton);
passwordPanel.addKeyListener(gkc.login);
// add the panel to the container
getContentPane().add(passwordPanel);
getContentPane().addKeyListener(gkc.login);
addKeyListener(gkc.login);
setTitle(" User Confirmation");
setIconImage(icon);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(288, 190); //added 200
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
// loggedon = false;
}
/**
* Invoke this method if this window doesnt have any more use for this
* current event.
*/
private void disableWindow() {
Game game = new Game();
System.out.println("disable window");
setVisible(false);
game.showwindow();
}
public static void close() throws InterruptedException{
System.out.println("closing...");
Thread.sleep(500);
System.exit(0);
}
}