package layout;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SpringLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class testing2 extends JPanel implements ActionListener {
static String usernamefile;
static String passwordfile;
static String emailfile;
//static String Path = System.getenv("USERPROFILE");
static File file = new File("temp");
static String dir = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4 );//remove the temp - this gives you the directory
static File rfile = new File(dir + "username.txt" );
File rfile1 = new File(dir + "password.txt" );
File rfile2 = new File(dir + "email.txt" );
File rfile3 = new File(dir + "linenum.txt");
static int linenum = 0;
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static int y = 0;
static JTextField textField = new JTextField("", 15);
static JPasswordField textField2 = new JPasswordField("", 15);
static JPasswordField textField3 = new JPasswordField("", 15);
static JTextField textField4 = new JTextField("", 15);
static JLabel label3 = new JLabel("Re-enter Password: ");
static JLabel label4 = new JLabel("Email Address: ");
protected static JButton b1;
protected static JButton b4;
protected JButton b2, b3;
public static String username;
public static String password;
public static String password2;
public static String email;
static int x = 0;
static JFrame frame = new JFrame("Account Creation");
private static void createAndShowGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
Container contentPane = frame.getContentPane();
SpringLayout layout = new SpringLayout();
contentPane.setLayout(layout);
//Create and add the components.
JLabel label = new JLabel("Username: ");
// JTextField textField = new JTextField("", 15);
JLabel label2 = new JLabel("Password: ");
// JPasswordField textField2 = new JPasswordField("", 15);
// JLabel label3 = new JLabel("Re-enter Password: ");
// JPasswordField textField3 = new JPasswordField("", 15);
//JLabel label4 = new JLabel("Email Address: ");
// JTextField textField4 = new JTextField("", 15);
b1 = new JButton("Finish");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_F);
b1.addActionListener(new testing2());
b1.setActionCommand("Finish");
b4 = new JButton("Login With Current Account");
b4.setVerticalTextPosition(AbstractButton.CENTER);
b4.setHorizontalTextPosition(AbstractButton.LEADING);
b4.setMnemonic(KeyEvent.VK_L);
b4.addActionListener(new testing2());
b4.setActionCommand("Finish");
contentPane.add(label);
contentPane.add(textField);
contentPane.add(label2);
contentPane.add(textField2);
contentPane.add(label3);
contentPane.add(textField3);
contentPane.add(label4);
contentPane.add(textField4);
contentPane.add(b1);
contentPane.add(b4);
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, label,
5,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, label,
5,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, textField,
56,
SpringLayout.EAST, label);
layout.putConstraint(SpringLayout.NORTH, textField,
5,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, label2,
5,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, label2,
33,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, textField2,
57,
SpringLayout.EAST, label2);
layout.putConstraint(SpringLayout.NORTH, textField2,
33,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, label3,
5,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, label3,
61,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, textField3,
5,
SpringLayout.EAST, label3);
layout.putConstraint(SpringLayout.NORTH, textField3,
61,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, label4,
5,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, label4,
89,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, textField4,
32,
SpringLayout.EAST, label4);
layout.putConstraint(SpringLayout.NORTH, textField4,
89,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b1,
25,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b1,
120,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b4,
100,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b4,
120,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//Display the window.
frame.pack();
frame.setSize(325, 200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1) {
username = textField.getText();
////System.out.println(username);
password = textField2.getText();
//System.out.println(password);
password2 = textField3.getText();
//System.out.println(password2);
email = textField4.getText();
//System.out.println(email);
if(password.equals(password2)&&email.contains("@"))
{
JOptionPane.showMessageDialog(null, "Thank you.","Complete",JOptionPane.INFORMATION_MESSAGE);
// System.out.println(x+"\\Downloads");
try {
FileWriter writer = new FileWriter(dir+"username.txt",true);
FileWriter writer1 = new FileWriter(dir+"password.txt",true);
FileWriter writer2 = new FileWriter(dir+"email.txt",true);
FileWriter writer3 = new FileWriter(dir+"writing.txt",true);
BufferedWriter out = new BufferedWriter(writer);
out.newLine();
out.write(username);
out.close();
BufferedWriter out1 = new BufferedWriter(writer1);
out1.newLine();
out1.write(password);
out1.close();
BufferedWriter out2 = new BufferedWriter(writer2);
out2.newLine();
out2.write(email);
out2.close();
BufferedWriter out3 = new BufferedWriter(writer3);
out3.write("");
out3.close();
FileInputStream fis3 = null;
BufferedInputStream bis3 = null;
DataInputStream dis3 = null;
fis3 = new FileInputStream(rfile3);
bis3 = new BufferedInputStream(fis3);
dis3 = new DataInputStream(bis3);
String linenumRead = dis3.readLine();
if(linenumRead==(null))
{
linenumRead=("0");
}
int linenumInt = Integer.parseInt(linenumRead);
linenumInt = linenumInt + 1;
String rlinenumS = Integer.toString(linenumInt);
System.out.println(rlinenumS);
BufferedWriter out4 = new BufferedWriter(new FileWriter(dir + "linenum.txt"));
out4.write(rlinenumS);
out4.close();
} catch (IOException e1) { }
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null, "Error.","Error",JOptionPane.ERROR_MESSAGE);
textField.setText("");
textField2.setText("");
textField3.setText("");
textField4.setText("");
}
}
if(e.getSource ()==b4)
{
y=y+1;
//System.out.println(y);
if(y==1)
{
frame.setTitle("Login");
b1.setVisible(false);
b4.setText("Login");
textField3.setVisible(false);
label3.setVisible(false);
textField4.setVisible(false);
label4.setVisible(false);
textField.setText("");
textField2.setText("");
y=y+1;
}
if(y == 3)
{
username = textField.getText();
//System.out.println(username);
password = textField2.getText();
//System.out.println(password);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
FileInputStream fis1 = null;
BufferedInputStream bis1 = null;
DataInputStream dis1 = null;
FileInputStream fis2 = null;
BufferedInputStream bis2 = null;
DataInputStream dis2 = null;
try {
fis = new FileInputStream(rfile);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
fis1 = new FileInputStream(rfile1);
bis1 = new BufferedInputStream(fis1);
dis1 = new DataInputStream(bis1);
fis2 = new FileInputStream(rfile2);
bis2 = new BufferedInputStream(fis2);
dis2 = new DataInputStream(bis2);
// dis.available() returns 0 if the file does not have more lines.
FileInputStream fis4 = null;
BufferedInputStream bis4 = null;
DataInputStream dis4 = null;
fis4 = new FileInputStream(rfile3);
bis4 = new BufferedInputStream(fis4);
dis4 = new DataInputStream(bis4);
String rlinenumRead = dis4.readLine();
System.out.println("After read:"+rlinenumRead);
if(rlinenumRead == null)
{
rlinenumRead=("0");
}
System.out.println("After checked for null:"+rlinenumRead);
int rlinenumInt = Integer.parseInt(rlinenumRead);
int LINENUM = rlinenumInt;
System.out.println("After converted to int:"+rlinenumInt);
while(LINENUM > 0)
{
usernamefile = dis.readLine();
System.out.println("Username:"+username);
System.out.println("Check:"+usernamefile);
if(username.equals(usernamefile))
{
LINENUM=0;
}
else
{
if(LINENUM==0)
{
JOptionPane.showMessageDialog(null, "Incorrect Username.","Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
LINENUM--;
}
int LINENUM2 = rlinenumInt;
while(LINENUM2 > 0)
{
passwordfile = dis.readLine();
System.out.println("Password:"+password);
System.out.println("Check:"+passwordfile);
if(password.equals(passwordfile))
{
LINENUM2=0;
}
else
{
if(LINENUM2==0)
{
JOptionPane.showMessageDialog(null, "Incorrect Password.","Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
LINENUM2--;
}
emailfile = dis2.readLine();
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
fis1.close();
bis1.close();
dis1.close();
fis2.close();
bis2.close();
dis2.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
//System.out.println(usernamefile);
//System.out.println(passwordfile);
JOptionPane.showMessageDialog(null, "Welcome back, "+username+"!","Welcome",JOptionPane.INFORMATION_MESSAGE);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
game1.createAndShowGame();
}
});
frame.setVisible(false);
}
}
}
}