Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The GameScene class extends JFrame
- package SystemManager;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Label;
- import java.awt.Panel;
- import java.awt.TextField;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import Graph.Interactables.Enemies.Enemies;
- import javax.swing.JLabel;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class GameScene extends JFrame implements KeyListener{
- public ImageIcon character1;
- public ImageIcon character2;
- public ImageIcon image1;
- public ImageIcon background;
- public String[] arr = {"W", "A", "S", "D"};
- public JFrame gameScene;
- public JPanel panel;
- public JPanel panel2;
- public JPanel panel3;
- public JPanel panel4;
- public JPanel panelCOM;
- public JPanel panelCOM2;
- public JLabel label; // Use this instance variable
- public JLabel label2;
- public boolean press = false;
- public JLabel playerHealth;
- public JLabel EnemyHealth;
- public JLabel Player;
- public JLabel Enemy;
- public TextField text;
- public GameScene(){
- addKeyListener(this);
- setFocusable(true); // Make sure the component can receive focus
- requestFocusInWindow();
- character1 = new ImageIcon("ImagesJPEG_PNG/png-transparent-cat-animal-lovely-cat.png");
- character2 = new ImageIcon("ImagesJPEG_PNG/nigga.jpg");
- // good enough hahahahaha
- // something wrong i character2
- // i need to better position the labels but its good now
- // Initialize Player label
- Player = new JLabel();
- Player.setPreferredSize(new Dimension(100, 100));
- Player.setOpaque(true);
- Player.setIcon(character1);
- // Initialize Enemy label
- Enemy = new JLabel(); // This must come before using Enemy
- Enemy.setPreferredSize(new Dimension(100, 100));
- Enemy.setOpaque(true);
- Enemy.setIcon(character1);
- label = new JLabel("JBHDBHDBHB"); // Use the instance variable
- label.setPreferredSize(new Dimension(100, 100));
- label.setText("");
- label.setOpaque(true); // Set the label opaque so the background color is visible
- GameBattles();
- playerHealth = new JLabel();
- playerHealth.setPreferredSize(new Dimension(100, 100));
- playerHealth.setText("");
- playerHealth.setOpaque(true);
- EnemyHealth = new JLabel();
- EnemyHealth.setPreferredSize(new Dimension(100, 100));
- EnemyHealth.setText("");
- EnemyHealth.setOpaque(true);
- JLabel task = new JLabel("");
- task.setPreferredSize(new Dimension(100, 100));
- task.setForeground(Color.RED);
- task.setHorizontalAlignment(JLabel.CENTER);
- task.setVerticalAlignment(JLabel.CENTER);
- text = new TextField();
- text.setBackground(Color.DARK_GRAY);
- text.setForeground(Color.WHITE);
- JPanel pan = new JPanel();
- pan.setPreferredSize(new Dimension(200, 200));
- pan.setBackground(Color.RED);
- pan.setOpaque(false);
- pan.setLayout(new BorderLayout());
- pan.add(text, BorderLayout.CENTER);
- panelCOM = new JPanel();
- panelCOM.setPreferredSize(new Dimension(400, 300));
- panelCOM.setBackground(Color.LIGHT_GRAY);
- panelCOM.setLayout(new BorderLayout());
- panelCOM.add(playerHealth, BorderLayout.NORTH);
- panelCOM.add(Player);
- playerHealth.setText("150");
- panelCOM2 = new JPanel();
- panelCOM2.setPreferredSize(new Dimension(400,300));
- panelCOM2.setBackground(Color.LIGHT_GRAY);
- panelCOM2.setLayout(new BorderLayout());
- panelCOM2.add(EnemyHealth, BorderLayout.NORTH);
- panelCOM2.add(Enemy);
- EnemyHealth.setText("150");
- JLabel label2 = new JLabel(); // Avoid reusing `label` name here
- label2.setPreferredSize(new Dimension(150, 50));
- label2.setBackground(Color.green);
- label2.setOpaque(true);
- panel3 = new JPanel();
- panel3.setPreferredSize(new Dimension(400, 300));
- panel3.setLayout(new BorderLayout());
- panel3.setBackground(Color.GREEN);
- panel4 = new JPanel();
- panel4.setLayout(new BorderLayout());
- panel4.setPreferredSize(new Dimension(400, 300));
- panel4.setBackground(Color.GREEN);
- panel2 = new JPanel();
- panel2.setPreferredSize(new Dimension(50, 50));
- panel2.setBackground(Color.BLACK);
- panel2.add(new JButton("Button"));
- panel2.add(new JButton("Inventory"));
- panel = new JPanel();
- panel.setPreferredSize(new Dimension(200, 200));
- panel.setBackground(Color.LIGHT_GRAY);
- panel.setLayout(new BorderLayout());
- panel.add(panel2, BorderLayout.NORTH);
- panel.add(panel3, BorderLayout.EAST);
- panel.add(panel4, BorderLayout.WEST);
- panel.add(pan, BorderLayout.CENTER);
- setSize(1920, 1080);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- setVisible(true);
- add(panel, BorderLayout.SOUTH);
- add(panelCOM, BorderLayout.EAST);
- add(panelCOM2, BorderLayout.WEST);
- add(label, BorderLayout.CENTER); // Add the correct label to the center
- requestFocusInWindow(); // Request focus so key events are captured
- }
- private int expectedKey; // Track the expected key
- public int GameBattles() {
- Random rand = new Random();
- int i = rand.nextInt(arr.length);
- label.setFont(new Font("Arial", Font.PLAIN, 500)); // Update the correct label
- label.setForeground(Color.BLACK);
- label.setText(arr[i]); // Set the random letter in the label
- String text = label.getText().toLowerCase();
- switch (text) {
- case "w":
- expectedKey = KeyEvent.VK_W; // Set expected key for W
- return KeyEvent.VK_W;
- case "a":
- expectedKey = KeyEvent.VK_A; // Set expected key for A
- return KeyEvent.VK_A;
- case "s":
- expectedKey = KeyEvent.VK_S; // Set expected key for S
- return KeyEvent.VK_S;
- case "d":
- expectedKey = KeyEvent.VK_D; // Set expected key for D
- return KeyEvent.VK_D;
- default:
- expectedKey = -1; // Reset if not a valid move
- return -1;
- }
- }
- public void setHealth(int health, String who){
- String str = who.toLowerCase();
- if (str.equals("player")){
- playerHealth.setText(String.valueOf(health));
- }
- else if (str.equals("enemy")){
- EnemyHealth.setText(String.valueOf(health));
- }
- }
- public void setPressed(boolean bool){
- this.press = bool;
- }
- public boolean getPressed(){
- return this.press;
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // Not used
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == expectedKey) {
- setPressed(true);
- } else {
- setPressed(false);
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // Not used
- setPressed(false);
- }
- }
Add Comment
Please, Sign In to add comment