diff options
author | cirakg <ciraboxkg@gmail.com> | 2022-12-30 07:27:32 +0100 |
---|---|---|
committer | cirakg <ciraboxkg@gmail.com> | 2022-12-30 07:27:32 +0100 |
commit | e5476a4f36551f11db0dc33e972a906adf0e0b5b (patch) | |
tree | cdc05bf1a3098f926188be0a3ae58a0399074dfa /src/main/java/ui | |
parent | 8d493c16f4bcfcb5c9f9754c999915ad00e650dd (diff) |
Omoguceno slanje zahteva za igru. Omoguceno accept i refuse zahteva(TODO ISPRAVITI).
Diffstat (limited to 'src/main/java/ui')
-rw-r--r-- | src/main/java/ui/ChooseOpponentPanel.java | 26 | ||||
-rw-r--r-- | src/main/java/ui/MainFrame.java | 54 | ||||
-rw-r--r-- | src/main/java/ui/OpponentConfirmation.java | 93 |
3 files changed, 173 insertions, 0 deletions
diff --git a/src/main/java/ui/ChooseOpponentPanel.java b/src/main/java/ui/ChooseOpponentPanel.java index 196477f..c318aaf 100644 --- a/src/main/java/ui/ChooseOpponentPanel.java +++ b/src/main/java/ui/ChooseOpponentPanel.java @@ -35,12 +35,38 @@ public class ChooseOpponentPanel extends JPanel { setLayout(null); btnSendGameRequest = new JButton("Send Game Request"); + btnSendGameRequest.setBounds(134, 153, 166, 23); add(btnSendGameRequest); comboBox = new JComboBox(); comboBox.setBounds(134, 101, 166, 22); add(comboBox); + + btnSendGameRequest.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if(comboBox.getItemCount()==0) + return; + User opponent=((ComboBoxUser)comboBox.getSelectedItem()).getUser(); + String msg="SELECTOPPONENT:"+opponent.id; + ByteBuffer bb=ByteBuffer.wrap(msg.getBytes()); + MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, ChooseOpponentPanel.this); + try { + SocketChannel client=topFrame.getClient(); + client.write(bb); + topFrame.getContentPane().removeAll(); + topFrame.opponentConfirmation.getBtnAccept().setVisible(false); + topFrame.opponentConfirmation.getBtnRefuse().setVisible(false); + topFrame.opponentConfirmation.setOpponentId(opponent.id); + topFrame.opponentConfirmation.lblNewLabel.setText("Wait for opponent"); + topFrame.getContentPane().add(topFrame.opponentConfirmation,BorderLayout.CENTER); + SwingUtilities.updateComponentTreeUI(topFrame); + } catch (Exception e1) { + e1.printStackTrace(); + } + + } + }); } diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java index 2aac225..8b3e949 100644 --- a/src/main/java/ui/MainFrame.java +++ b/src/main/java/ui/MainFrame.java @@ -8,8 +8,10 @@ import javax.swing.border.EmptyBorder; import models.CONSTS; import models.ComboBoxUser; +import models.GameStatus; import models.User; import models.UserListWrapper; +import pokemon.Game; import javax.swing.JTextField; import javax.swing.SwingUtilities; @@ -42,14 +44,18 @@ public class MainFrame extends JFrame implements Runnable { public SocketChannel client; public LoginPanel loginPanel=null; public ChooseOpponentPanel chooseOpponentPanel=null; + public OpponentConfirmation opponentConfirmation=null; GamePanel gamePanel=null; public long userId=-1; public boolean run=true; + public GameStatus status; + public Game game; public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 727, 429); loginPanel=new LoginPanel(); chooseOpponentPanel=new ChooseOpponentPanel(); + opponentConfirmation=new OpponentConfirmation(); gamePanel=new GamePanel(); getContentPane().setLayout(new BorderLayout(0, 0)); add(loginPanel,BorderLayout.CENTER); @@ -110,6 +116,20 @@ public class MainFrame extends JFrame implements Runnable { }else if(response[0].trim().equals("BADLOGINADMIN")){ System.out.println("Bad Login"); this.loginPanel.warningTextArea.setText("Admin nema pristup aplikaciji"); + }else if(response[0].trim().equals("GAMEREQUEST")){ + System.out.println("Game Request"); + this.getContentPane().removeAll(); + this.opponentConfirmation.getBtnAccept().setVisible(true); + this.opponentConfirmation.getBtnRefuse().setVisible(true); + this.opponentConfirmation.lblNewLabel.setText("Game Request"+"id:"+response[1].trim()); + this.opponentConfirmation.setOpponentId(Long.parseLong(response[1].trim())); + this.getContentPane().add(this.opponentConfirmation,BorderLayout.CENTER); + SwingUtilities.updateComponentTreeUI(this); + }else if(response[0].trim().equals("REFUSEGAME")){ + System.out.println("REFUSEGAME"); + this.getContentPane().removeAll(); + this.getContentPane().add(this.chooseOpponentPanel,BorderLayout.CENTER); + SwingUtilities.updateComponentTreeUI(this); }else { //XML OBJECTS XMLDecoder decoder = null; @@ -128,6 +148,16 @@ public class MainFrame extends JFrame implements Runnable { } catch (Exception e) { System.out.println("Nije refresh"); } + try { + game=(Game) decoder.readObject(); + decoder.close(); + this.getContentPane().removeAll(); + this.getContentPane().add(this.gamePanel,BorderLayout.CENTER); + SwingUtilities.updateComponentTreeUI(this); + + } catch (Exception e) { + System.out.println("Nije GAME"); + } } @@ -182,5 +212,29 @@ public class MainFrame extends JFrame implements Runnable { public void setUserId(long userId) { this.userId = userId; } + public OpponentConfirmation getOpponentConfirmation() { + return opponentConfirmation; + } + public void setOpponentConfirmation(OpponentConfirmation opponentConfirmation) { + this.opponentConfirmation = opponentConfirmation; + } + public boolean isRun() { + return run; + } + public void setRun(boolean run) { + this.run = run; + } + public GameStatus getStatus() { + return status; + } + public void setStatus(GameStatus status) { + this.status = status; + } + public Game getGame() { + return game; + } + public void setGame(Game game) { + this.game = game; + } } diff --git a/src/main/java/ui/OpponentConfirmation.java b/src/main/java/ui/OpponentConfirmation.java new file mode 100644 index 0000000..d0c2a95 --- /dev/null +++ b/src/main/java/ui/OpponentConfirmation.java @@ -0,0 +1,93 @@ +package ui; + +import javax.swing.JPanel; +import javax.swing.SwingUtilities; + +import models.ComboBoxUser; +import models.User; + +import javax.swing.JLabel; +import javax.swing.JButton; +import java.awt.event.ActionListener; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; + +public class OpponentConfirmation extends JPanel { + public JLabel lblNewLabel; + public JButton btnAccept; + public JButton btnRefuse; + public long opponentId; + + public OpponentConfirmation() { + setLayout(null); + + lblNewLabel = new JLabel("New label"); + lblNewLabel.setBounds(95, 66, 208, 98); + add(lblNewLabel); + + btnAccept = new JButton("Accept"); + btnAccept.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String msg="ACCEPTOPPONENT:"+opponentId; + ByteBuffer bb=ByteBuffer.wrap(msg.getBytes()); + MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, OpponentConfirmation.this); + try { + SocketChannel client=topFrame.getClient(); + client.write(bb); + SwingUtilities.updateComponentTreeUI(topFrame); + } catch (Exception e1) { + e1.printStackTrace(); + } + + + } + }); + btnAccept.setBounds(92, 194, 89, 23); + add(btnAccept); + + btnRefuse = new JButton("Refuse"); + btnRefuse.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String msg="REFUSEOPPONENT:"+opponentId; + ByteBuffer bb=ByteBuffer.wrap(msg.getBytes()); + MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, OpponentConfirmation.this); + try { + SocketChannel client=topFrame.getClient(); + client.write(bb); + SwingUtilities.updateComponentTreeUI(topFrame); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + }); + btnRefuse.setBounds(214, 194, 89, 23); + add(btnRefuse); + + } + public long getOpponentId() { + return opponentId; + } + public void setOpponentId(long opponentId) { + this.opponentId = opponentId; + } + public JLabel getLblNewLabel() { + return lblNewLabel; + } + public void setLblNewLabel(JLabel lblNewLabel) { + this.lblNewLabel = lblNewLabel; + } + public JButton getBtnAccept() { + return btnAccept; + } + public void setBtnAccept(JButton btnAccept) { + this.btnAccept = btnAccept; + } + public JButton getBtnRefuse() { + return btnRefuse; + } + public void setBtnRefuse(JButton btnRefuse) { + this.btnRefuse = btnRefuse; + } +} |