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 +++++++++++++++++++++++++--- src/main/java/ui/MainFrame.java | 92 +++++++++++++++++++++++++++++++++++----- 2 files changed, 139 insertions(+), 17 deletions(-) (limited to 'src/main/java/ui') 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 + } + } } diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java index 0eac906..cfef0ba 100644 --- a/src/main/java/ui/MainFrame.java +++ b/src/main/java/ui/MainFrame.java @@ -5,31 +5,39 @@ import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; + +import models.CONSTS; + import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.Color; import javax.swing.JButton; import javax.swing.JTextArea; import java.awt.Font; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; import java.awt.BorderLayout; public class MainFrame extends JFrame implements Runnable { - /** - * Launch the application. - */ + + + public static void main(String[] args) { MainFrame frame = new MainFrame(); frame.setVisible(true); } - - /** - * Create the frame. - */ + public ByteBuffer readBuffer=ByteBuffer.allocate(1024); + public SocketChannel client; public LoginPanel loginPanel=null; public ChooseOpponentPanel chooseOpponentPanel=null; GamePanel gamePanel=null; + public long userId=-1; public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 727, 429); @@ -39,20 +47,84 @@ public class MainFrame extends JFrame implements Runnable { getContentPane().setLayout(new BorderLayout(0, 0)); add(loginPanel,BorderLayout.CENTER); setVisible(true); + this.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) + { + System.out.println("EXITED"); + + try { + client.close(); + Thread.sleep(500); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + System.exit(EXIT_ON_CLOSE); + } + + }); + + try { + client=SocketChannel.open(new InetSocketAddress(CONSTS.socketPort)); + } catch (IOException e) { + e.printStackTrace(); + } new Thread(this).start(); } @Override public void run() { while(true) { try { - Thread.sleep(500); - System.out.println("testClient"); - } catch (InterruptedException e) { + + + + + + + + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } + public SocketChannel getClient() { + return client; + } + public void setClient(SocketChannel client) { + this.client = client; + } + public LoginPanel getLoginPanel() { + return loginPanel; + } + public void setLoginPanel(LoginPanel loginPanel) { + this.loginPanel = loginPanel; + } + public ChooseOpponentPanel getChooseOpponentPanel() { + return chooseOpponentPanel; + } + public void setChooseOpponentPanel(ChooseOpponentPanel chooseOpponentPanel) { + this.chooseOpponentPanel = chooseOpponentPanel; + } + public GamePanel getGamePanel() { + return gamePanel; + } + public void setGamePanel(GamePanel gamePanel) { + this.gamePanel = gamePanel; + } + public ByteBuffer getReadBuffer() { + return readBuffer; + } + public void setReadBuffer(ByteBuffer readBuffer) { + this.readBuffer = readBuffer; + } + public long getUserId() { + return userId; + } + public void setUserId(long userId) { + this.userId = userId; + } } -- cgit v1.2.3