Guest User

Untitled

a guest
Sep 26th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.65 KB | Source Code | 0 0
  1. The GameScene class extends JFrame
  2.  
  3. package SystemManager;
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.FlowLayout;
  9. import java.awt.Font;
  10. import java.awt.GridBagConstraints;
  11. import java.awt.GridBagLayout;
  12. import java.awt.Label;
  13. import java.awt.Panel;
  14. import java.awt.TextField;
  15. import java.util.Random;
  16.  
  17. import javax.swing.JFrame;
  18. import javax.swing.JPanel;
  19.  
  20. import Graph.Interactables.Enemies.Enemies;
  21.  
  22. import javax.swing.JLabel;
  23. import javax.swing.ImageIcon;
  24. import javax.swing.JButton;
  25.  
  26. import java.awt.event.KeyEvent;
  27. import java.awt.event.KeyListener;
  28.  
  29. public class GameScene extends JFrame implements KeyListener{
  30.     public ImageIcon character1;
  31.     public ImageIcon character2;
  32.  
  33.     public ImageIcon image1;
  34.     public ImageIcon background;
  35.  
  36.     public String[] arr  = {"W", "A", "S", "D"};
  37.    
  38.     public JFrame gameScene;
  39.     public JPanel panel;
  40.     public JPanel panel2;
  41.     public JPanel panel3;
  42.     public JPanel panel4;
  43.     public JPanel panelCOM;
  44.     public JPanel panelCOM2;
  45.     public JLabel label; // Use this instance variable
  46.     public JLabel label2;
  47.    
  48.     public boolean press = false;
  49.     public JLabel playerHealth;
  50.     public JLabel EnemyHealth;
  51.     public JLabel Player;
  52.     public JLabel Enemy;
  53.     public TextField text;
  54.    
  55.  
  56.     public GameScene(){
  57.  
  58.     addKeyListener(this);
  59.     setFocusable(true);         // Make sure the component can receive focus
  60.     requestFocusInWindow();
  61.    
  62.    
  63.  
  64.  
  65.     character1 = new ImageIcon("ImagesJPEG_PNG/png-transparent-cat-animal-lovely-cat.png");
  66.     character2 = new ImageIcon("ImagesJPEG_PNG/nigga.jpg");
  67.  
  68.     // good enough hahahahaha
  69.     // something wrong i character2
  70.     // i need to better position the labels but its good now
  71.  
  72.     // Initialize Player label
  73.     Player = new JLabel();
  74.     Player.setPreferredSize(new Dimension(100, 100));
  75.    
  76.     Player.setOpaque(true);
  77.     Player.setIcon(character1);
  78.  
  79.  
  80.     // Initialize Enemy label
  81.     Enemy = new JLabel(); // This must come before using Enemy
  82.     Enemy.setPreferredSize(new Dimension(100, 100));
  83.    
  84.     Enemy.setOpaque(true);
  85.     Enemy.setIcon(character1);
  86.    
  87.  
  88.  
  89.        
  90.        
  91.  
  92.        
  93.         label = new JLabel("JBHDBHDBHB"); // Use the instance variable
  94.         label.setPreferredSize(new Dimension(100, 100));
  95.         label.setText("");
  96.         label.setOpaque(true); // Set the label opaque so the background color is visible
  97.         GameBattles();
  98.  
  99.         playerHealth = new JLabel();
  100.         playerHealth.setPreferredSize(new Dimension(100, 100));
  101.         playerHealth.setText("");
  102.         playerHealth.setOpaque(true);
  103.  
  104.  
  105.         EnemyHealth = new JLabel();
  106.         EnemyHealth.setPreferredSize(new Dimension(100, 100));
  107.         EnemyHealth.setText("");
  108.         EnemyHealth.setOpaque(true);
  109.  
  110.  
  111.  
  112.  
  113.  
  114.        
  115.  
  116.         JLabel task = new JLabel("");
  117.         task.setPreferredSize(new Dimension(100, 100));
  118.         task.setForeground(Color.RED);
  119.         task.setHorizontalAlignment(JLabel.CENTER);
  120.         task.setVerticalAlignment(JLabel.CENTER);
  121.  
  122.         text = new TextField();
  123.         text.setBackground(Color.DARK_GRAY);
  124.         text.setForeground(Color.WHITE);
  125.         JPanel pan = new JPanel();
  126.         pan.setPreferredSize(new Dimension(200, 200));
  127.         pan.setBackground(Color.RED);
  128.         pan.setOpaque(false);
  129.         pan.setLayout(new BorderLayout());
  130.         pan.add(text, BorderLayout.CENTER);
  131.        
  132.         panelCOM = new JPanel();
  133.         panelCOM.setPreferredSize(new Dimension(400, 300));
  134.         panelCOM.setBackground(Color.LIGHT_GRAY);
  135.         panelCOM.setLayout(new BorderLayout());
  136.         panelCOM.add(playerHealth, BorderLayout.NORTH);
  137.         panelCOM.add(Player);
  138.        
  139.         playerHealth.setText("150");
  140.  
  141.  
  142.         panelCOM2 = new JPanel();
  143.         panelCOM2.setPreferredSize(new Dimension(400,300));
  144.         panelCOM2.setBackground(Color.LIGHT_GRAY);
  145.         panelCOM2.setLayout(new BorderLayout());
  146.         panelCOM2.add(EnemyHealth, BorderLayout.NORTH);
  147.         panelCOM2.add(Enemy);
  148.      
  149.  
  150.         EnemyHealth.setText("150");
  151.  
  152.         JLabel label2 = new JLabel(); // Avoid reusing `label` name here
  153.         label2.setPreferredSize(new Dimension(150, 50));
  154.         label2.setBackground(Color.green);
  155.         label2.setOpaque(true);
  156.  
  157.         panel3 = new JPanel();
  158.         panel3.setPreferredSize(new Dimension(400, 300));
  159.         panel3.setLayout(new BorderLayout());
  160.         panel3.setBackground(Color.GREEN);
  161.  
  162.         panel4 = new JPanel();
  163.         panel4.setLayout(new BorderLayout());
  164.         panel4.setPreferredSize(new Dimension(400, 300));
  165.         panel4.setBackground(Color.GREEN);
  166.  
  167.         panel2 = new JPanel();
  168.         panel2.setPreferredSize(new Dimension(50, 50));
  169.         panel2.setBackground(Color.BLACK);
  170.         panel2.add(new JButton("Button"));
  171.         panel2.add(new JButton("Inventory"));
  172.  
  173.         panel = new JPanel();
  174.         panel.setPreferredSize(new Dimension(200, 200));
  175.         panel.setBackground(Color.LIGHT_GRAY);
  176.         panel.setLayout(new BorderLayout());
  177.         panel.add(panel2, BorderLayout.NORTH);
  178.         panel.add(panel3, BorderLayout.EAST);
  179.         panel.add(panel4, BorderLayout.WEST);
  180.         panel.add(pan, BorderLayout.CENTER);
  181.  
  182.         setSize(1920, 1080);
  183.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  184.         setLayout(new BorderLayout());
  185.         setVisible(true);
  186.  
  187.         add(panel, BorderLayout.SOUTH);
  188.         add(panelCOM, BorderLayout.EAST);
  189.         add(panelCOM2, BorderLayout.WEST);
  190.         add(label, BorderLayout.CENTER); // Add the correct label to the center
  191.         requestFocusInWindow();     // Request focus so key events are captured
  192.  
  193.     }
  194.  
  195.    
  196.     private int expectedKey; // Track the expected key
  197.     public int GameBattles() {
  198.         Random rand = new Random();
  199.         int i = rand.nextInt(arr.length);
  200.         label.setFont(new Font("Arial", Font.PLAIN, 500)); // Update the correct label
  201.         label.setForeground(Color.BLACK);
  202.         label.setText(arr[i]); // Set the random letter in the label
  203.         String text = label.getText().toLowerCase();
  204.    
  205.         switch (text) {
  206.             case "w":
  207.             expectedKey = KeyEvent.VK_W; // Set expected key for W
  208.             return KeyEvent.VK_W;
  209.         case "a":
  210.             expectedKey = KeyEvent.VK_A; // Set expected key for A
  211.             return KeyEvent.VK_A;
  212.         case "s":
  213.             expectedKey = KeyEvent.VK_S; // Set expected key for S
  214.             return KeyEvent.VK_S;
  215.         case "d":
  216.             expectedKey = KeyEvent.VK_D; // Set expected key for D
  217.             return KeyEvent.VK_D;
  218.         default:
  219.             expectedKey = -1; // Reset if not a valid move
  220.             return -1;
  221.     }
  222. }
  223.    
  224.  
  225.  
  226.  
  227.  
  228.     public void setHealth(int health, String who){
  229.         String str = who.toLowerCase();
  230.         if (str.equals("player")){
  231.             playerHealth.setText(String.valueOf(health));
  232.  
  233.         }
  234.         else if (str.equals("enemy")){
  235.             EnemyHealth.setText(String.valueOf(health));
  236.         }
  237.     }
  238.  
  239.     public void setPressed(boolean bool){
  240.         this.press = bool;
  241.     }
  242.  
  243.     public boolean getPressed(){
  244.         return this.press;
  245.     }
  246.  
  247.  
  248.  
  249.     @Override
  250.     public void keyTyped(KeyEvent e) {
  251.         // Not used
  252.     }
  253.     public void keyPressed(KeyEvent e) {
  254.        
  255.         if (e.getKeyCode() == expectedKey) {
  256.             setPressed(true);
  257.            
  258.         } else {
  259.             setPressed(false);
  260.            
  261.         }
  262.     }
  263.  
  264.  
  265.     @Override
  266.     public void keyReleased(KeyEvent e) {
  267.         // Not used
  268.         setPressed(false);
  269.     }
  270. }
  271.  
Add Comment
Please, Sign In to add comment