From 336af3217840237bba99935d63631d199d5c0e3e Mon Sep 17 00:00:00 2001 From: cirakg Date: Thu, 29 Dec 2022 02:27:32 +0100 Subject: Omogucena konekcija pomocu socketa. Omogucen login i primanje poruka. Handlovanje gasenje clienta(Logout). Napravljen kostur klase Game. Popravljen text wrap na warningu pri login-u. --- src/main/java/ui/LoginPanel.java | 64 +++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 7 deletions(-) (limited to 'src/main/java/ui/LoginPanel.java') diff --git a/src/main/java/ui/LoginPanel.java b/src/main/java/ui/LoginPanel.java index 336d538..5ab4c0d 100644 --- a/src/main/java/ui/LoginPanel.java +++ b/src/main/java/ui/LoginPanel.java @@ -13,11 +13,15 @@ import javax.swing.JButton; import javax.swing.JFrame; import java.awt.event.ActionListener; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; import java.awt.event.ActionEvent; public class LoginPanel extends JPanel { public JTextField textFieldUsername; public JTextField textFieldPassword; + public JButton btnLogIn; + JTextArea warningTextArea; /** * Create the panel. @@ -43,27 +47,73 @@ public class LoginPanel extends JPanel { textFieldPassword.setBounds(205, 106, 100, 20); add(textFieldPassword); - JTextArea warningTextArea = new JTextArea(); + warningTextArea = new JTextArea(); + warningTextArea.setWrapStyleWord(true); + warningTextArea.setLineWrap(true); warningTextArea.setToolTipText(""); - warningTextArea.setText("fsdfsdfd fdsfsdfsd"); warningTextArea.setForeground(Color.RED); warningTextArea.setFont(new Font("Monospaced", Font.PLAIN, 10)); warningTextArea.setEditable(false); warningTextArea.setBackground(SystemColor.menu); warningTextArea.setBounds(132, 134, 158, 36); add(warningTextArea); - JButton btnLogIn = new JButton("Log In"); + btnLogIn = new JButton("Log In"); btnLogIn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, LoginPanel.this); - topFrame.getContentPane().removeAll(); - topFrame.add(new GamePanel(),BorderLayout.CENTER); - SwingUtilities.updateComponentTreeUI(topFrame); +// MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, LoginPanel.this); +// topFrame.getContentPane().removeAll(); +// topFrame.add(new GamePanel(),BorderLayout.CENTER); +// SwingUtilities.updateComponentTreeUI(topFrame); + Login(); } }); btnLogIn.setBounds(174, 181, 81, 23); add(btnLogIn); } + + public void Login() { + String username=textFieldUsername.getText(); + String password=textFieldPassword.getText(); + if(!(username.trim().length()>0 && password.trim().length()>0)) + { + warningTextArea.setText("Morate uneti login informacije"); + return; + } + String loginMsg="LOGIN:"+username+":"+password; + ByteBuffer bbLogin=ByteBuffer.wrap(loginMsg.getBytes()); + MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, LoginPanel.this); + try { + SocketChannel client=topFrame.getClient(); + ByteBuffer readBuffer=topFrame.getReadBuffer(); + client.write(bbLogin); + client.configureBlocking(true); + readBuffer.clear(); + StringBuilder sb=new StringBuilder(); + while(client.read(readBuffer)>0) { + readBuffer.flip(); + byte[] bytes = new byte[readBuffer.limit()]; + readBuffer.get(bytes); + sb.append(new String(bytes)); + readBuffer.clear(); + client.configureBlocking(false); + } + String response[]=sb.toString().split(":"); + if(response[0].trim().equals("ACCEPTED")) { + topFrame.setUserId(Long.parseLong(response[1].trim())); + topFrame.getContentPane().removeAll(); + topFrame.getContentPane().add(new ChooseOpponentPanel(),BorderLayout.CENTER); + SwingUtilities.updateComponentTreeUI(topFrame); + + + }else { + System.out.println("Bad Login"); + warningTextArea.setText("Pogresan Login"); + } + + } catch (Exception e) { + // TODO: handle exception + } + } } -- cgit v1.2.3