Problem: The problem I have run into is that when I click on the "Play" button or the "Feed" button, the pet will change its statistics, but the JProgressBar color will not change accordingly, but the progress bar percentage will. Here is the application class:
public class App extends JFrame implements ActionListener {
static Pet mainPet = new Pet("Default Pet", 0, 100, 40, 65); //name, age, health, hunger, happy
static JProgressBar health = new JProgressBar(0,100);
static JProgressBar hunger = new JProgressBar(0,100);
static JProgressBar happiness = new JProgressBar(0,100);
static JButton feed = new JButton("Feed");
static JButton play = new JButton("Play");
public App() {
super("Virtual Pet Simulator");
setLayout(new FlowLayout());
health.setStringPainted(true);
health.setValue(mainPet.getHealth());
health.setString("Health");
hunger.setStringPainted(true);
hunger.setString("Hunger");
hunger.setValue(mainPet.getHunger());
happiness.setStringPainted(true);
happiness.setString("Happiness");
happiness.setValue(mainPet.getHappiness());
add(health);
add(hunger);
add(happiness);
add(feed);
add(play);
feed.addActionListener(this);
play.addActionListener(this);
}
public static void main(String[] args) {
App a = new App();
a.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
a.pack();
a.setVisible(true);
a.setLocationRelativeTo(null);
Thread updateThread = new Thread() {
@Override
public void run() {
while(true) {
mainPet.setHappiness(mainPet.getHappiness()-1);
happiness.setValue(mainPet.happiness);
if(mainPet.happiness > 75) {
happiness.setForeground(Color.green);
happiness.setString("Great");
}
if(mainPet.happiness <= 75) {
happiness.setForeground(Color.cyan);
happiness.setString("Good");
}
if(mainPet.happiness <= 45) {
happiness.setForeground(Color.orange);
happiness.setString("Worse");
}
if(mainPet.happiness <= 20) {
happiness.setForeground(Color.red);
happiness.setString("Sad");
}
mainPet.setHunger(mainPet.getHunger()-1);
hunger.setValue(mainPet.hunger);
if(mainPet.hunger > 75) {
hunger.setForeground(Color.green);
hunger.setString("Full");
}
if(mainPet.hunger >= 75) {
hunger.setForeground(Color.cyan);
hunger.setString("Good");
}
if(mainPet.hunger >= 45) {
hunger.setForeground(Color.orange);
hunger.setString("Hungry");
}
if(mainPet.hunger >= 20) {
hunger.setForeground(Color.red);
hunger.setString("Starving");
}
if(mainPet.hunger <= 0) {
hunger.setForeground(Color.magenta);
hunger.setString("Extreme hunger!");
mainPet.setHealth(mainPet.getHealth()-1);
}
health.setValue(mainPet.health);
if(mainPet.health > 75) {
health.setForeground(Color.green);
health.setString("Great");
}
if(mainPet.health <= 75) {
health.setForeground(Color.cyan);
health.setString("Good");
}
if(mainPet.health <= 45) {
health.setForeground(Color.orange);
health.setString("Requires attention");
}
if(mainPet.health <= 20) {
health.setForeground(Color.red);
health.setString("Near death");
}
if(mainPet.health <= 0) {
health.setForeground(Color.magenta);
health.setString("Pet death.");
}
//a.repaint();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
updateThread.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == play) {
mainPet.setHappiness(mainPet.getHappiness()+1);
System.out.println(mainPet.getHappiness());
}
else if(e.getSource() == feed) {
mainPet.setHunger(mainPet.getHunger()+1);
System.out.println(mainPet.getHunger());
}
}
}
I will not include the Pet class, as it is very complex. To describe it, it is a class with the variables, happiness, hunger, health, age, name
. And it also has a constructor for these variables in the order of: String name, int age, int health, int hunger, int happiness
as commented on line three.
Thanks for your help.