Making a Simple RPG- Creating a new JFrame

883 views Asked by At

I am making a 4-class RPG. It has Player class which allows us to move the player. It has Board class which is for the map in the game. There is a Battle class which is for the battle phase. RPG is the main class responsible for running the game. What I want is the when the player comes to coordinates(500x500) I want the program to open a new window and it will have the JPanel of battle on it.

EDIT : Updated display() and actionPerformed() methods

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Player {

    private int dx,dy,x,y;
    private Image image;
    private ImageIcon front = new ImageIcon("player_front.png");
    private ImageIcon left = new ImageIcon("player_left.png");
    private ImageIcon right = new ImageIcon("player_right.png");
    private ImageIcon back = new ImageIcon("player_back.png");

    public Player() {

        initplayer();
    }

    private void initplayer() {

        image = front.getImage();
        x = 0;
        y = 0;        
    }


    public void move() {
        x += dx;
        y += dy;

    }

    public int getX() {
        return x;
    }

    public void setX(int x){
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Image getImage() {
        return image;
    }


    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        dx = 0;
        dy = 0;

        if (key == KeyEvent.VK_LEFT && x >= 0) {
            image = left.getImage();
            dx = -1;
        }


        if (key == KeyEvent.VK_RIGHT && x <= 850) {
            image = right.getImage();
            dx = 1;
        }

        if (key == KeyEvent.VK_UP && y >= 0) {
            image = back.getImage();                
            dy = -1;

        }
            if (key == KeyEvent.VK_DOWN && y <= 850) {
                image = front.getImage();
                dy = 1;

        }
    }

    public void keyReleased(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
}


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener {

    private Battle battle;
    private Timer timer;
    private Player player;
    private final int DELAY = 10;
    private Image image;
    private JLabel label;

    public Board() {

        initBoard();

    }

    private void initBoard() {

        image = Toolkit.getDefaultToolkit().createImage("map.png");

        addKeyListener(new TAdapter());
        setFocusable(true);

        player = new Player();

        timer = new Timer(DELAY, this);
        timer.start();           
    }

    public Image getImage(){
        return image;
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(image,0,0,null);

        doDrawing(g);

    }

    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;   
        g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);

    }

    public boolean checkBattle(){
        if(player.getX() == 500 && player.getY() == 500){
            return true;
        }
        return false;
    }

    public void actionPerformed(ActionEvent e) {//UPDATED

        player.move();

        if(checkBattle()){
              battle.display();
        }            

        repaint();       
    }


    private class TAdapter extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
        }
    }
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
import java.util.*;


public class Battle extends JPanel{

    private JFrame frame;
    private JPanel panel1, panel2;
    private JLabel label1, label2, label3, label4;
    private JButton button1, button2, button3, button4;
    private int playerHealth = 100;
    private int opponentHealth = 50;
    private int maxOpponentHealth = 50;
    private int level = 0;
    private int playerDamage, opponentDamage, chance, levelDamage;
    private ImageIcon image;

    public Battle(){
        setPreferredSize(new Dimension(500,500));
        setLayout(new BorderLayout());
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());

        label1 = new JLabel("You : " + Integer.toString(playerHealth) + " / 100");
        label2 = new JLabel("Opponent : " + Integer.toString(opponentHealth) + " / 50");
        button1 = new JButton("Power Attack!(%50 hit chance)");
        button2 = new JButton("Normal Attack!(%70 hit chance)");
        button3 = new JButton("Quick Attack!(%90 hit chance)");
        button4 = new JButton("NEXT LEVEL!");   
        panel2 = new JPanel();

        ImageIcon image = new ImageIcon("player_vs_rival.png");
        label4 = new JLabel(image);

        ButtonListener listener = new ButtonListener();

        button1.addActionListener(listener);
        button2.addActionListener(listener);
        button3.addActionListener(listener);
        button4.addActionListener(listener);
        panel1.add(button1);
        panel1.add(button2);
        panel1.add(button3);
        panel1.add(button4);
        panel2.add(label4);
        label3 = new JLabel("");

        add(label1, BorderLayout.WEST);
        add(label2, BorderLayout.EAST);
        add(label3, BorderLayout.SOUTH);
        add(panel1, BorderLayout.CENTER);
        add(panel2, BorderLayout.NORTH);

    }

    public void powerAttack(){
        Random random = new Random();
        playerDamage = random.nextInt(5) + 10;
    }

    public void normalAttack(){
        Random random = new Random();
        playerDamage = random.nextInt(10) + 5;
    }

    public void quickAttack(){
        Random random = new Random();
        playerDamage = random.nextInt(5) + 5;
    }

    public void opponentAttack(){
        Random random = new Random();
        opponentDamage = random.nextInt(5) + 5;
    }

    public boolean checkPowerAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 50){
            return true;
        }
        return false;   
    }

    public boolean checkNormalAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 30){
            return true;
        }
        return false;
    }

    public boolean checkQuickAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 10){
            return true;
        }
        return false;
    }

    public boolean checkOpponentAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 25){
            return true;
        }
        return false;
    }

    public void levelDamage(){
        Random random = new Random();
        levelDamage = random.nextInt(5);
        levelDamage = levelDamage * level;
    }

    public void display(){//UPDATED

        JFrame newFrame = new JFrame("New Window"); 
        Battle battle = new Battle();
        newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        newFrame.setVisible(true);
        newFrame.pack();
        newFrame.getContentPane().add(battle);

    }
}

    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){

            Object source = e.getSource();

            if(source == button1){
                if(checkPowerAttack() == true){
                    powerAttack();
                }
                else
                playerDamage = 0;           

                opponentHealth = opponentHealth - playerDamage; 
            }

            if(source == button2){
                if(checkNormalAttack() == true){

                    normalAttack();
                }
                else
                playerDamage = 0;           

                opponentHealth = opponentHealth - playerDamage; 
            }

            if(source == button3){
                if(checkQuickAttack() == true){
                    quickAttack();
                }
                else
                playerDamage = 0;           

                opponentHealth = opponentHealth - playerDamage; 
            }

            if((source == button1 || source == button2) || source == button3){
                if(checkOpponentAttack() == true){
                    opponentAttack();
                }
                else
                opponentDamage = 0;

                playerHealth = playerHealth - opponentDamage - levelDamage; 
            }

            if(source == button4){
                level++;
                opponentHealth = opponentHealth + 25;
                maxOpponentHealth = opponentHealth;
            }

            if(playerHealth <= 0 && opponentHealth > 0){
                JOptionPane.showMessageDialog(null,"YOU LOSE");
            }

            if(opponentHealth <= 0 && playerHealth > 0){
                JOptionPane.showMessageDialog(null,"YOU WIN");
            }

            label1.setText("You : " + Integer.toString(playerHealth) + " / 100");
            label2.setText("Opponent : " + Integer.toString(opponentHealth) + " / " + Integer.toString(maxOpponentHealth));
            if(playerDamage != 0 && opponentDamage != 0){
                label3.setText("You made " + Integer.toString(playerDamage) + " damage to your opponent. " + http://stackoverflow.com/editing-help
                        "Your opponent give " + Integer.toString(opponentDamage + levelDamage) + " damage to you.");
            }
            else if(playerDamage == 0 && opponentDamage != 0){
                label3.setText("YOU MISSED YOUR ATTACK! " + 
                        "Your opponent give " + Integer.toString(opponentDamage + levelDamage) + " damage to you.");
            }
            else if(playerDamage != 0 && opponentDamage == 0){
                label3.setText("You made " + Integer.toString(playerDamage) + " damage to your opponent. " + 
                        "YOUR OPPONENT MISSED HIS ATTACK!");
            }
            else if(playerDamage == 0 && opponentDamage == 0){
                label3.setText("YOU MISSED YOUR ATTACK! " + 
                        "YOUR OPPONENT ALSO MISSED HIS ATTACK!");
            }
        }
    }
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RPG extends JFrame {

    private Board board = new Board();
    private Battle battle = new Battle();

    public RPG() {

        initUI();
    }

    private void initUI() {

        add(board);

        setSize(900, 900);
        setResizable(false);

        setTitle("RPG");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {

                RPG ex = new RPG();
                ex.setVisible(false);
            }
        });
    }
}
1

There are 1 answers

8
Curmudgeon On BEST ANSWER

Opening a new window is as simple as creating a new frame like you've already done in your display() function. For instance, this:

    JFrame newFrame = new JFrame("New Window"); 
    newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    newFrame.setVisible(true);

Should be enough to open a new window with nothing in it (you'll probably want to add something to it you did in your display() method). Since you want to do this when the player reaches a certain position, just check the player coordinates on movement and call similar code to open a new frame.

EDIT: Upon looking again, it looks like you can just call display() in your checkBattle() function? It looks like you already have most of the logic here. If you're wondering where to call checkBattle() from, I'd put it in the Player.move() function.