-2

I've made a simple game with GUI interface in Java. It's an RPG game and somewhere in the interface it has a progress bar, which is the life bar, and inside of it, it has other GUI item, a text, which is the value of the character's life.

At first it worked fine, but I added an ActionerListener to one attack button, and what it does is reduce the life bar and the value of the life (that's on the text), as an real attack on games does. But when I do that I occurs a bug.

Sometimes the life bar stays on front of the life value, and sometimes it doesn't, it's like completely random. So my question is: Is there a way to set an Gui item always on front? In that case the value of the life.

Obs: I have a abstract class (CharactersInterface) where I declare all the stuff and the Orc enemy.

public class wizardInterface extends CharactersInterface{

//interface
public wizardInterface() {

//your name
you = new JLabel(wizard.name);
you.setBounds(85, 20, 160, 30);
panel.add(you);

//hit points
yhitpoints = new JLabel(wizard.hp + "/25");
yhitpoints.setBounds(95, 51, 50, 15);
panel.add(yhitpoints);

//life bar
ylife.setValue(wizard.hp*4);
ylife.setBounds(50, 50, 125, 18);
ylife.setForeground(Color.GREEN);
panel.add(ylife);

//your image
image1 = new ImageIcon(getClass().getResource("wizard.png"));
img1 = new JLabel(image1);
img1.setBounds(50, 80, 128, 128);
panel.add(img1);

//text
text = new JTextArea("Choose your spell!");
text.setFont(new Font("Serif", Font.PLAIN, 16));
text.setBackground(null);
text.setBounds(185, 300, 160, 45);
panel.add(text);

//thunder button
Icon thunder = new ImageIcon(getClass().getResource("thunder.png"));
attack1 = new JButton("Thunder", thunder);
attack1.setBounds(20, 350, 230, 140);
panel.add(attack1);

//fire button
Icon fire = new ImageIcon(getClass().getResource("fire.png"));
attack2 = new JButton("Fire", fire);
attack2.setBounds(280, 350, 230, 140);
panel.add(attack2);

//background image
backgroundImage = new ImageIcon(getClass().getResource("background1.png"));
background = new JLabel(backgroundImage);
background.setBounds(0, -20, 535, 300);
panel.add(background);

//do something when press the button
thehandler handler = new thehandler();
attack1.addActionListener(handler);
attack2.addActionListener(handler);

}

private class thehandler implements ActionListener{

     public void actionPerformed(ActionEvent event) {
        if(event.getSource()==attack1){
            orc.hp -=  1 + dice.nextInt(10) +  wizard.intelligence; // 1d10 +2;

        }else if(event.getSource()==attack2){
            orc.hp -= 3 + dice.nextInt(8) + wizard.intelligence; // 1d8 +4
        }

        //here is where I reduce the life of the enemy
        elife.setValue(orc.hp*2);
        ehitpoints.setText(orc.hp + "/50");

        if(orc.hp <=0){
            System.out.println("you won");
            System.exit(0);
        }

        //orc attacks
        wizard.hp -= 1 + dice.nextInt(4) + orc.strong; //1d4 + 2 

        ylife.setValue(wizard.hp*4);
        yhitpoints.setText(wizard.hp + "/25");

        if(wizard.hp <=0){
            System.out.println("you lose");
            System.exit(0);
        }
    }
} 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Jun 03 '18 at 11:20
  • .. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 4) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Jun 03 '18 at 11:20

1 Answers1

0

At first it worked fine, but I added an ActionerListener to one attack button, and what it does is reduce the life bar and the value of the life (that's on the text), as an real attack on games does.

Each time you reduce the character's health or increase it, try transferring focus to the object you want to be at the front...ex:-

bar.requestFocus();
Miles Morales
  • 307
  • 1
  • 15