diff options
author | cirakg <ciraboxkg@gmail.com> | 2022-12-31 16:36:04 +0100 |
---|---|---|
committer | cirakg <ciraboxkg@gmail.com> | 2022-12-31 16:36:04 +0100 |
commit | d0df0ed9a37ecd9288997ff0e1a68e7f37b52a47 (patch) | |
tree | c1d89f3d8501d6897c8587660581a7b2e5647cf4 /src/main/java | |
parent | f72f7c4216cefe4c6e2c30dc9919b5d8034bd59e (diff) |
Omogucen prikaz grafickih elemenata u toku igre.
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/ui/GamePanel.java | 314 | ||||
-rw-r--r-- | src/main/java/ui/MainFrame.java | 3 |
2 files changed, 289 insertions, 28 deletions
diff --git a/src/main/java/ui/GamePanel.java b/src/main/java/ui/GamePanel.java index 0071849..b29597c 100644 --- a/src/main/java/ui/GamePanel.java +++ b/src/main/java/ui/GamePanel.java @@ -1,16 +1,47 @@ package ui; import javax.swing.JPanel; + +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.util.Base64; + +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; +import javax.swing.SwingUtilities; + + +import models.Ability; +import models.AbilityType; +import models.MonsterViewModel; +import models.User; + import javax.swing.JTextArea; public class GamePanel extends JPanel { - private JTextField textFieldChat; + public JTextField textFieldChat; + public JButton btnAttack; + public JButton btnSpecial; + public JButton btnHeal; + public JButton btnShield; + public JLabel lblImageEnemy; + public JLabel lblImageMe; + public JLabel lblPokemonNameEnemy; + public JProgressBar progressBarHpEnemy; + public JLabel lblHpEnemy; + public JLabel lblPokemonNameMe; + public JProgressBar progressBarHpMe; + public JLabel lblHpMe; + public JButton btnSendText; + public JScrollPane scrollPane; + public JTextArea txtAreaChat; /** * Create the panel. @@ -18,52 +49,52 @@ public class GamePanel extends JPanel { public GamePanel() { setLayout(null); - JButton btnAttack = new JButton("Attack"); - btnAttack.setBounds(0, 239, 89, 30); + btnAttack = new JButton("Attack"); + btnAttack.setBounds(0, 239, 130, 30); add(btnAttack); - JButton btnSpecial = new JButton("Special"); - btnSpecial.setBounds(87, 239, 89, 30); + btnSpecial = new JButton("Special"); + btnSpecial.setBounds(130, 239, 130, 30); add(btnSpecial); - JButton btnHeal = new JButton("Heal"); - btnHeal.setBounds(0, 272, 89, 30); + btnHeal = new JButton("Heal"); + btnHeal.setBounds(0, 272, 130, 30); add(btnHeal); - JButton btnShield = new JButton("Shield"); - btnShield.setBounds(87, 272, 89, 30); + btnShield = new JButton("Shield"); + btnShield.setBounds(130, 272, 130, 30); add(btnShield); - JLabel lblImageEnemy = new JLabel("imageEnemy"); - lblImageEnemy.setBounds(333, 35, 50, 50); + lblImageEnemy = new JLabel("imageEnemy"); + lblImageEnemy.setBounds(326, 29, 110, 110); add(lblImageEnemy); - JLabel lblImageMe = new JLabel("imageMe"); - lblImageMe.setBounds(21, 177, 50, 50); + lblImageMe = new JLabel("imageMe"); + lblImageMe.setBounds(25, 118, 110, 110); add(lblImageMe); - JLabel lblPokemonNameEnemy = new JLabel("PokemonNameEnemy"); + lblPokemonNameEnemy = new JLabel("PokemonNameEnemy"); lblPokemonNameEnemy.setBounds(25, 11, 139, 14); add(lblPokemonNameEnemy); - JProgressBar progressBarHpEnemy = new JProgressBar(); + progressBarHpEnemy = new JProgressBar(); progressBarHpEnemy.setBounds(21, 33, 146, 14); add(progressBarHpEnemy); - JLabel lblHpEnemy = new JLabel("HpEnemy"); - lblHpEnemy.setBounds(104, 53, 72, 14); + lblHpEnemy = new JLabel("HpEnemy"); + lblHpEnemy.setBounds(104, 53, 200, 14); add(lblHpEnemy); - JLabel lblPokemonNameMe = new JLabel("PokemonNameMe"); + lblPokemonNameMe = new JLabel("PokemonNameMe"); lblPokemonNameMe.setBounds(232, 171, 139, 14); add(lblPokemonNameMe); - JProgressBar progressBarHpMe = new JProgressBar(); + progressBarHpMe = new JProgressBar(); progressBarHpMe.setBounds(228, 193, 146, 14); add(progressBarHpMe); - JLabel lblHpMe = new JLabel("HpMe"); - lblHpMe.setBounds(311, 213, 72, 14); + lblHpMe = new JLabel("HpMe"); + lblHpMe.setBounds(311, 213, 200, 14); add(lblHpMe); textFieldChat = new JTextField(); @@ -71,21 +102,248 @@ public class GamePanel extends JPanel { add(textFieldChat); textFieldChat.setColumns(10); - JButton btnSendText = new JButton("Send"); + btnSendText = new JButton("Send"); btnSendText.setBounds(585, 266, 89, 23); add(btnSendText); - JScrollPane scrollPane = new JScrollPane(); + scrollPane = new JScrollPane(); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBounds(486, 22, 188, 205); add(scrollPane); - JTextArea txtrLoremIpsumDolor = new JTextArea(); - txtrLoremIpsumDolor.setEditable(false); - txtrLoremIpsumDolor.setLineWrap(true); - txtrLoremIpsumDolor.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Est ultricies integer quis auctor elit sed. Urna nunc id cursus metus aliquam eleifend mi. Amet consectetur adipiscing elit pellentesque habitant morbi tristique senectus. Consequat interdum varius sit amet mattis vulputate enim nulla aliquet. Tristique magna sit amet purus gravida quis blandit. Eget gravida cum sociis natoque penatibus. Nec tincidunt praesent semper feugiat nibh. Maecenas sed enim ut sem. Pulvinar neque laoreet suspendisse interdum consectetur. Diam maecenas ultricies mi eget mauris pharetra. Sollicitudin aliquam ultrices sagittis orci. Nulla facilisi etiam dignissim diam quis."); - scrollPane.setViewportView(txtrLoremIpsumDolor); + txtAreaChat = new JTextArea(); + txtAreaChat.setEditable(false); + txtAreaChat.setLineWrap(true); + txtAreaChat.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Est ultricies integer quis auctor elit sed. Urna nunc id cursus metus aliquam eleifend mi. Amet consectetur adipiscing elit pellentesque habitant morbi tristique senectus. Consequat interdum varius sit amet mattis vulputate enim nulla aliquet. Tristique magna sit amet purus gravida quis blandit. Eget gravida cum sociis natoque penatibus. Nec tincidunt praesent semper feugiat nibh. Maecenas sed enim ut sem. Pulvinar neque laoreet suspendisse interdum consectetur. Diam maecenas ultricies mi eget mauris pharetra. Sollicitudin aliquam ultrices sagittis orci. Nulla facilisi etiam dignissim diam quis."); + scrollPane.setViewportView(txtAreaChat); + + } + public void loadElements() { + MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, GamePanel.this); + long you=-1; + long enemy=-1; + MonsterViewModel yourMonster=null; + MonsterViewModel enemyMonster=null; + int yourHp=0; + int enemyHp=0; + int yourShield=0; + int enemyShield=0; + boolean yourTurn=false; + + if(topFrame.userId==topFrame.game.player1Id) { + you=topFrame.game.player1Id; + enemy=topFrame.game.player2Id; + yourMonster=topFrame.game.monster1; + enemyMonster=topFrame.game.monster2; + yourHp=topFrame.game.currentHp1; + enemyHp=topFrame.game.currentHp2; + yourShield=topFrame.game.shield1; + enemyShield=topFrame.game.shield2; + if(topFrame.game.player1Turn) { + yourTurn=true; + + }else { + yourTurn=false; + + } + }else if(topFrame.userId==topFrame.game.player2Id) { + you=topFrame.game.player2Id; + enemy=topFrame.game.player1Id; + yourMonster=topFrame.game.monster2; + enemyMonster=topFrame.game.monster1; + yourHp=topFrame.game.currentHp2; + enemyHp=topFrame.game.currentHp1; + yourShield=topFrame.game.shield2; + enemyShield=topFrame.game.shield1; + if(topFrame.game.player1Turn) { + yourTurn=false; + + }else { + yourTurn=true; + + } + } + + for(Ability ability:yourMonster.abilities) { + if(ability.type==AbilityType.ATTACK) { + btnAttack.setText(ability.Name); + + }else if(ability.type==AbilityType.SPECIAL) { + btnSpecial.setText(ability.Name); + + }else if(ability.type==AbilityType.HEAL) { + btnHeal.setText(ability.Name); + + }else if(ability.type==AbilityType.SHIELD) { + btnShield.setText(ability.Name); + + } + } + + + lblPokemonNameMe.setText(yourMonster.name); + lblPokemonNameEnemy.setText(enemyMonster.name); + + lblHpMe.setText(yourHp+"("+yourShield+")"+"/"+yourMonster.hp); + lblHpEnemy.setText(enemyHp+"("+enemyShield+")"+"/"+enemyMonster.hp); + + progressBarHpMe.setValue((yourHp/yourMonster.hp)*100); + progressBarHpEnemy.setValue((enemyHp/enemyMonster.hp)*100); + + try { + Image img=base64toImage(yourMonster.base64Image).getScaledInstance(lblImageMe.getWidth(), lblImageMe.getHeight(), Image.SCALE_SMOOTH); + lblImageMe.setIcon(new ImageIcon(img)); + } catch (Exception e) { + } + try { + Image img=base64toImage(enemyMonster.base64Image).getScaledInstance(lblImageMe.getWidth(), lblImageMe.getHeight(), Image.SCALE_SMOOTH); + lblImageEnemy.setIcon(new ImageIcon(img)); + } catch (Exception e) { + + } + //txtAreaChat.setText(null); + + + + + } + + public BufferedImage base64toImage(String imgStr)throws Exception { + BufferedImage image=null; + byte[] imageByte; + + imageByte = Base64.getDecoder().decode(imgStr); + ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); + image = ImageIO.read(bis); + bis.close(); + return image; + + } + + public JTextArea getTxtAreaChat() { + return txtAreaChat; + } + public void setTxtAreaChat(JTextArea txtAreaChat) { + this.txtAreaChat = txtAreaChat; + } + public JTextField getTextFieldChat() { + return textFieldChat; + } + + public void setTextFieldChat(JTextField textFieldChat) { + this.textFieldChat = textFieldChat; + } + + public JButton getBtnAttack() { + return btnAttack; + } + + public void setBtnAttack(JButton btnAttack) { + this.btnAttack = btnAttack; + } + + public JButton getBtnSpecial() { + return btnSpecial; + } + + public void setBtnSpecial(JButton btnSpecial) { + this.btnSpecial = btnSpecial; + } + + public JButton getBtnHeal() { + return btnHeal; + } + + public void setBtnHeal(JButton btnHeal) { + this.btnHeal = btnHeal; + } + + public JButton getBtnShield() { + return btnShield; + } + + public void setBtnShield(JButton btnShield) { + this.btnShield = btnShield; + } + + public JLabel getLblImageEnemy() { + return lblImageEnemy; + } + + public void setLblImageEnemy(JLabel lblImageEnemy) { + this.lblImageEnemy = lblImageEnemy; + } + + public JLabel getLblImageMe() { + return lblImageMe; + } + + public void setLblImageMe(JLabel lblImageMe) { + this.lblImageMe = lblImageMe; + } + + public JLabel getLblPokemonNameEnemy() { + return lblPokemonNameEnemy; + } + + public void setLblPokemonNameEnemy(JLabel lblPokemonNameEnemy) { + this.lblPokemonNameEnemy = lblPokemonNameEnemy; + } + + public JProgressBar getProgressBarHpEnemy() { + return progressBarHpEnemy; + } + + public void setProgressBarHpEnemy(JProgressBar progressBarHpEnemy) { + this.progressBarHpEnemy = progressBarHpEnemy; + } + + public JLabel getLblHpEnemy() { + return lblHpEnemy; + } + + public void setLblHpEnemy(JLabel lblHpEnemy) { + this.lblHpEnemy = lblHpEnemy; + } + + public JLabel getLblPokemonNameMe() { + return lblPokemonNameMe; + } + + public void setLblPokemonNameMe(JLabel lblPokemonNameMe) { + this.lblPokemonNameMe = lblPokemonNameMe; + } + + public JProgressBar getProgressBarHpMe() { + return progressBarHpMe; + } + + public void setProgressBarHpMe(JProgressBar progressBarHpMe) { + this.progressBarHpMe = progressBarHpMe; + } + + public JLabel getLblHpMe() { + return lblHpMe; + } + + public void setLblHpMe(JLabel lblHpMe) { + this.lblHpMe = lblHpMe; + } + + public JButton getBtnSendText() { + return btnSendText; + } + + public void setBtnSendText(JButton btnSendText) { + this.btnSendText = btnSendText; + } + + public JScrollPane getScrollPane() { + return scrollPane; + } + public void setScrollPane(JScrollPane scrollPane) { + this.scrollPane = scrollPane; } } diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java index 16fc4ef..85b2175 100644 --- a/src/main/java/ui/MainFrame.java +++ b/src/main/java/ui/MainFrame.java @@ -152,6 +152,9 @@ public class MainFrame extends JFrame implements Runnable { this.getContentPane().removeAll(); this.getContentPane().add(this.gamePanel,BorderLayout.CENTER); SwingUtilities.updateComponentTreeUI(this); + this.game=game; + this.gamePanel.loadElements(); + this.gamePanel.txtAreaChat.setText(null);//clear chat between games } |