Reputation:
So I'm supposed to make a game that has a score in its HUD but whenever i use the code
g.drawString("Score: " + score, 15, 64);
//score is a variable
it appears a white screen like this
the full code of the HUD is like this
import java.awt.Color;
import java.awt.Graphics;
public class HUD {
public static int HEALTH = 800;
private static int gvalue = 255;
public int score = 0;
public int level = 1;
public void tick(){
HEALTH = MainGame.clamp(HEALTH, 0, 800);
gvalue = MainGame.clamp(gvalue, 0, 255);
gvalue = HEALTH/4;
score++;
}
public void render(Graphics g){
g.setColor(Color.gray);
g.fillRect(15, 15, 200, 30);
g.setColor(new Color(70, gvalue, 0));
g.fillRect(15, 15, (HEALTH / 4) , 30);
g.setColor(Color.white);
g.drawRect(15, 15, 100 * 2, 30);
g.drawString("Score: " + score, 15, 64);
//the line above though, when i remove it, it completely works
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
P.S I have objects, windows, colors, etc. but they are in other classes. P.P.S When I remove, the said line, it works fine.
Upvotes: 1
Views: 317
Reputation: 11
I know this is almost 4 years old, but I'm following the same tutorial on youtube and got the same problem. I also found a solution, so posting here in case other people stumble across this thread:
Here is what solved it in my case: In the Game class (where you have the game loop and render / tick method), look in the render method. In my case, I had to change the number in createBufferStrategy() from 2 to 3. So this code now works:
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
And I can now successfully g.drawString in the HUD class
Upvotes: 1
Reputation:
I don't see anything declaring font or font color...
g.setColor(Color.black);
Font font = new Font("Times New Roman", Font.BOLD, (int) Math.round(20 * scaleX));
g.setFont(font);
g.drawString("Score: " + score, 15, 64);
I hope this helps.
Upvotes: 2