如何使用java中的新值更新绘制的矩形

时间:2013-08-23 04:27:11

标签: java paint

我制作了一个非常简单的神奇宝贝战斗模拟器,我开始升级它。通过升级它,我的意思是以一种非常简约的方式(至少在我的脑海中)。此升级包括创建两个HP栏以配合我的控制台输出。我的问题是我不知道如何用神奇宝贝的HP更新它们......

以下是我认为与之直接相关的代码:

我的BattleHandler:     包com.mrmewniverse.pokemon.battle;

import java.util.Random;

import com.mrmewniverse.pokemon.pokemon.Pokemon;

public class BattleHandler {

    private static Pokemon fasterPokemon;
    private static Pokemon slowerPokemon;

    public BattleHandler() {}

    public void initiateBattle(Pokemon par1Poke, Pokemon par2Poke) {
        System.out.println("Battle initiated!\n\n");
        BattleHandler.calculateFasterPokemon(par1Poke, par2Poke);

        try {
            while (fasterPokemon.getCurrentHealth() > 0 && slowerPokemon.getCurrentHealth() > 0) {
                Thread.sleep(400);

                fasterPokemon.attack(slowerPokemon);
                System.out.println(fasterPokemon.getPokeName() + " attacked " + slowerPokemon.getPokeName() + " with " + fasterPokemon.getMoveUsed().getMoveName() + " dealing "
                + fasterPokemon.getLastDamageDealt() + " damage\n");

                Thread.sleep(50);

                if (fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 2F || fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 4F)
                    System.out.println("It's super effective!\n");
                else if (fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.5F || fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.25F)
                    System.out.println("It's not very effective...\n");

                Thread.sleep(400);

                if (fasterPokemon.getCurrentHealth() <= 0 || slowerPokemon.getCurrentHealth() <= 0) {
                    this.endBattle(fasterPokemon, slowerPokemon);
                    break;
                }

                Thread.sleep(400);

                slowerPokemon.attack(fasterPokemon);
                System.out.println(slowerPokemon.getPokeName() + " attacked " + fasterPokemon.getPokeName() + " with " + slowerPokemon.getMoveUsed().getMoveName() + " dealing "
                + slowerPokemon.getLastDamageDealt() + " damage\n");

                Thread.sleep(50);

                if (slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 2F || slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 4F)
                    System.out.println("It's super effective!\n");
                else if (slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.5F || slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.25F)
                    System.out.println("It's not very effective...\n");

                Thread.sleep(400);

                if (fasterPokemon.getCurrentHealth() <= 0 || slowerPokemon.getCurrentHealth() <= 0) {
                    this.endBattle(fasterPokemon, slowerPokemon);
                    break;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void calculateFasterPokemon(Pokemon par1Poke, Pokemon par2Poke) {
        if (par1Poke.getPokeStats().getPokeSpeed() == par2Poke.getPokeStats().getPokeSpeed()) {
            Random random = new Random();

            int randNum = random.nextInt(3 - 1) + 1;

            if (randNum == 1) {
                fasterPokemon = par1Poke;
                slowerPokemon = par2Poke;
            }
            if (randNum == 2) {
                fasterPokemon = par2Poke;
                slowerPokemon = par1Poke;
            }
        } else {
            if (par1Poke.getPokeStats().getPokeSpeed() > par2Poke.getPokeStats().getPokeSpeed()) {
                fasterPokemon = par1Poke;
                slowerPokemon = par2Poke;
            } else if (par2Poke.getPokeStats().getPokeSpeed() > par1Poke.getPokeStats().getPokeSpeed()) {
                fasterPokemon = par2Poke;
                slowerPokemon = par1Poke;
            }
        }
    }

    private void endBattle(Pokemon par1Poke, Pokemon par2Poke) {
        try {
            System.out.println("\nBattle finished!");

            Thread.sleep(400);

            if (par2Poke.getCurrentHealth() <= 0)
                System.out.println("\n" + par1Poke.getPokeName() + " won the battle!");
            else if (par1Poke.getCurrentHealth() <= 0)
                System.out.println("\n" + par2Poke.getPokeName() + " won the battle!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我的执行课程:     包com.mrmewniverse;

import javax.swing.JFrame;

import com.mrmewniverse.pokemon.battle.BattleHandler;
import com.mrmewniverse.pokemon.dex.PokeDex;
import com.mrmewniverse.pokemon.pokemon.Pokemon;

@SuppressWarnings("serial")
public class Start extends JFrame {

    static Pokemon poke1 = PokeDex.BULBASAUR;
    static Pokemon poke2 = PokeDex.CHARMANDER;
    static Pokemon poke3 = PokeDex.SQUIRTLE;

    public static Board board = new Board(poke1);

    public Start() {
        this.setSize(500, 290);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        this.add(board);

        this.setVisible(true);
    }

    public static void main ( String[] args ) {
        new Start();

        BattleHandler battle = new BattleHandler();
        battle.initiateBattle(poke1, poke2);
    }
}

我的董事会课程:     包com.mrmewniverse;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import com.mrmewniverse.pokemon.pokemon.Pokemon;

@SuppressWarnings("serial")
public class Board extends JPanel {

    private int health;
    private int maxHealth;

    public Board(Pokemon par1Poke) {
        this.health = par1Poke.getCurrentHealth();
        this.maxHealth = par1Poke.getPokeStats().getPokeMaxHealth();
    }

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

        g.setColor(Color.GRAY);
        g.fillRect(2, 2, 200, 20);

        int healthScale = health / maxHealth;
        g.setColor(Color.GREEN);
        g.fillRect(2, 2, 200 * healthScale, 20);
    }
}

随意批评我的编码,我需要帮助XD

1 个答案:

答案 0 :(得分:0)

这是我对您的代码的修订:

首先使用paintComponent()方法

而不是使用paint()方法 好吧,这解决了我的问题(“它一直显示灰色条,即使它更新了”) 注意*电路板已更新,但您必须通过调用手动更新它  更新(Pokemon CurrentPokemon)方法;

    public class Board extends JPanel {

private int health;
private int maxHealth;

public Board(Pokemon par1Poke) {
    this.health = par1Poke.getCurrentHealth();
    this.maxHealth = par1Poke.getPokeStats().getPokeMaxHealth();
}

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

    g.setColor(Color.GRAY);
    g.fillRect(2, 2, 200, 20);

    g.setColor(Color.GREEN);
    g.fillRect(2, 2, 200 *(health/maxHealth) , 20);
}

    public void update(Pokemon Poke1) {
    health = Poke1.getCurrentHealth();
    maxHealth = Poke1.getPokeStats().getPokeMaxHealth();
}

}

然后在你的主要

    public class Start extends JFrame {

static Pokemon poke1 = PokeDex.BULBASAUR;
static Pokemon poke2 = PokeDex.CHARMANDER;
static Pokemon poke3 = PokeDex.SQUIRTLE;

public static Board board = new Board( poke1 );

public Start() {
    this.setSize( 500, 290 );
    this.setDefaultCloseOperation( EXIT_ON_CLOSE );
    this.setLocationRelativeTo( null );

    this.add( board );

    this.setVisible( true );
}

public static void main ( String[] args ) {
    new Start();

     BattleHandler battle = new BattleHandler();
     battle.initiateBattle( poke1, poke2 ) ;
     board.update(poke1);
}

}

确定这不是最佳解决方案,我能想到的全部

我收到了你的代码并实施了我的代码,Download this and run.这可能是你想要的