I am making a game which has a store where in game coins can be spent. I want to display the amount of coins you have to spend. This value changes constantly as the game progresses. I have it capable of displaying the initial value and changing with purchases but it will not update from the other class.
Here is my code:
public StoreFrame(Run game,You y) {
super("Store");
theGame=game;
setLayout(null);
you=y;
try {
storeBack = ImageIO.read(new File("backgrounds/store.png"));
}
catch (IOException e) {
}
//background
setContentPane(new JLabel(new ImageIcon(storeBack)));
coins = new JLabel("Coins: "+you.gettotCoins());//here is the problem
coins.setFont(new Font("SanSerif", Font.BOLD, 18));
coins.setForeground(Color.black);
coins.setBounds(25, 25, 100, 100);
add(coins);
setSize (800,600);
setResizable(false);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
The class the text comes from:
public class You {
private int totCoins;
public You(int p) {
totCoins=110;//starting money
}
public int gettotCoins() {
return totCoins;
}
public void settotCoins(int subtract) {
totCoins-=subtract;
}
}
Please help
I found a way to do this. In the classes that access this frame, just say
This will pass in the new text into the class and will cause it to update
Thanks to all above who helped put me on the right track