miércoles, 25 de noviembre de 2015

Clase LoginCliente

package pacman;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.ResultSet;

import javax.swing.ImageIcon;
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.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

public class LoginCliente extends JFrame {

private JPanel contentPane;
private JTextField textID;
private JLabel lblNewLabel_1;
private JButton btnIniciarS;
private JLabel lblNewLabel_2;
private JButton btnSalir;
private JLabel lblNewLabel_3;
private HomeCliente ventanaHome;
private JPasswordField textPass;
private BaseDatos bd = null;
private ResultSet rs = null;
private JTextField Ip;
private Socket socket ;
private DataOutputStream salidadato ;
private DataInputStream entradaDatos;
private Thread hilo ;
private JLabel lVersion;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginCliente frame = new LoginCliente();
frame.setUndecorated(true);
frame.setLocationRelativeTo(frame);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public LoginCliente() {

/* Instancia la Base de Datos */

bd = new BaseDatos();

setIconImage(Toolkit.getDefaultToolkit().getImage(Login.class.getResource("/Imagenes/p6.png")));
setTitle("Juego");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 483, 312);
//contentPane = new JPanel();
contentPane = new Fondo("/Imagenes/p8.jpg");
contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(255, 255, 255), new Color(255, 255, 255), new Color(255, 255, 255), new Color(255, 255, 255)));
contentPane.setBackground(Color.BLACK);

contentPane.setForeground(new Color(255, 255, 255));
setContentPane(contentPane);
contentPane.setLayout(null);

textID = new JTextField();
textID.setFont(new Font("Tahoma", Font.PLAIN, 15));
textID.setHorizontalAlignment(SwingConstants.CENTER);
textID.setForeground(Color.WHITE);
textID.setBackground(Color.BLACK);
textID.setToolTipText("");
textID.setText("\r\n");
textID.setBounds(200, 117, 166, 26);
contentPane.add(textID);
textID.setColumns(10);

JLabel lblNewLabel = new JLabel("Usuario");
lblNewLabel.setBackground(Color.WHITE);
lblNewLabel.setForeground(Color.ORANGE);
lblNewLabel.setFont(new Font("Consolas", Font.BOLD, 19));
lblNewLabel.setBounds(63, 117, 97, 26);
contentPane.add(lblNewLabel);

lblNewLabel_1 = new JLabel("Contrase�a");
lblNewLabel_1.setBackground(Color.WHITE);
lblNewLabel_1.setForeground(Color.ORANGE);
lblNewLabel_1.setFont(new Font("Consolas", Font.BOLD, 19));
lblNewLabel_1.setBounds(38, 153, 109, 26);
contentPane.add(lblNewLabel_1);

btnIniciarS = new JButton("Iniciar Sesi�n");
btnIniciarS.setForeground(Color.WHITE);
btnIniciarS.setBackground(Color.DARK_GRAY);
btnIniciarS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

/*
String id = textID.getText().trim();
String passwd = new String(textPass.getPassword());
if (id.isEmpty() || passwd.isEmpty())
JOptionPane.showMessageDialog(null, "Usuario/Contrase�a son nulos","ERROR", JOptionPane.ERROR_MESSAGE);
else{
establecerConexion();
salidaDato(id+" "+passwd);
//System.out.println("Enviado Usuario y Contrase�a: " + id );

if(esperaRespuestaServer().equals("0")){
lblNewLabel_3.setText("Credenciales Incorrectas");
lblNewLabel_3.setVisible(true);
}
else{*/
crearVentana();
/* }

}*/
}
});
btnIniciarS.setBounds(225, 192, 121, 23);
contentPane.add(btnIniciarS);

lblNewLabel_2 = new JLabel("Bienvenido al Pac-Man");
lblNewLabel_2.setForeground(new Color(255, 215, 0));
lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_2.setVerticalAlignment(SwingConstants.TOP);
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblNewLabel_2.setBounds(118, 27, 322, 26);
contentPane.add(lblNewLabel_2);

btnSalir = new JButton("Salir");
btnSalir.setForeground(Color.RED);
btnSalir.setBackground(Color.BLACK);
btnSalir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
abreDialogoSalida();
}
});
btnSalir.setBounds(368, 260, 89, 23);
contentPane.add(btnSalir);

lblNewLabel_3 = new JLabel("Usuario/Contrase�a Incorrecto");
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblNewLabel_3.setForeground(new Color(255, 69, 0));
lblNewLabel_3.setBounds(23, 238, 202, 14);
lblNewLabel_3.setVisible(false);
contentPane.add(lblNewLabel_3);

textPass = new JPasswordField();
textPass.setHorizontalAlignment(SwingConstants.CENTER);
textPass.setForeground(Color.YELLOW);
textPass.setBackground(Color.BLACK);
textPass.setBounds(200, 154, 166, 27);
contentPane.add(textPass);

JLabel lblNewLabel_4 = new JLabel("New label");
lblNewLabel_4.setIcon(new ImageIcon(Login.class.getResource("/Imagenes/picon.png")));
lblNewLabel_4.setBounds(407, 32, 33, 19);
contentPane.add(lblNewLabel_4);

JLabel textIP = new JLabel("IP");
textIP.setForeground(Color.ORANGE);
textIP.setFont(new Font("Consolas", Font.BOLD, 19));
textIP.setBackground(Color.WHITE);
textIP.setBounds(81, 80, 33, 26);
contentPane.add(textIP);

Ip = new JTextField();
Ip.setToolTipText("");
Ip.setText("127.0.0.1");
Ip.setHorizontalAlignment(SwingConstants.CENTER);
Ip.setForeground(Color.WHITE);
Ip.setFont(new Font("Tahoma", Font.PLAIN, 15));
Ip.setColumns(10);
Ip.setBackground(Color.BLACK);
Ip.setBounds(200, 80, 166, 26);
contentPane.add(Ip);

lVersion = new JLabel("V 1.10");
lVersion.setFont(new Font("Tahoma", Font.PLAIN, 9));
lVersion.setForeground(SystemColor.windowBorder);
lVersion.setBounds(424, 239, 33, 14);
contentPane.add(lVersion);

}

void abreDialogoSalida(){
int res = JOptionPane.showConfirmDialog(this, "Desea Salir ? ","Salida",JOptionPane.YES_NO_OPTION);
if (res==JOptionPane.YES_NO_OPTION)
System.exit(0);
}

public void crearVentana(){
ventanaHome= new HomeCliente(this);
//ventanaHome.setUndecorated(true);
ventanaHome.setLocationRelativeTo(ventanaHome);
ventanaHome.setVisible(true);
this.setVisible(false);
}

public String getUser(){
return textID.getText();
}

    private void salidaDato(String output){

try {
salidadato.writeUTF(output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
   }
}
   
    private String esperaRespuestaServer(){

 String mensaje = null;
     boolean conectado = true;
     while (conectado) {
         try {
             mensaje = entradaDatos.readUTF();
             return mensaje;
         } catch (IOException ex) {
          System.out.println("Error al leer del stream de entrada de Login: " + ex.getMessage());
             conectado = false;
         } catch (NullPointerException ex) {
          System.out.println("El socket no se creo correctamente. ");
             conectado = false;
         }
     }
return mensaje;
}



public void establecerConexion(){

try {
         socket = new Socket(Ip.getText(),1234);
         salidadato = new DataOutputStream(socket.getOutputStream());
         entradaDatos = new DataInputStream(socket.getInputStream());
     } catch (UnknownHostException ex) {
      System.out.println("No se ha podido conectar con el servidor (" + ex.getMessage() + ").");
     } catch (IOException ex) {
      System.out.println("No se ha podido conectar con el servidor (" + ex.getMessage() + ").");
       lblNewLabel_3.setText("Network error: Connection refused");
       lblNewLabel_3.setVisible(true);
     }

}


public Socket getSocket() {
return socket;
}

public DataOutputStream getSalidadato() {
return salidadato;
}

public DataInputStream getEntradaDatos() {
return entradaDatos;
}
}

No hay comentarios:

Publicar un comentario