gpt4 book ai didi

java - 当我使用 g.drawString 时,java 中出现白屏

转载 作者:行者123 更新时间:2023-11-30 02:36:41 25 4
gpt4 key购买 nike

所以我应该制作一个在 HUD 中有分数的游戏,但每当我使用代码时

g.drawString("Score: " + score, 15, 64);
//score is a variable

出现这样的白屏

enter image description here

HUD的完整代码是这样的

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 我有对象、窗口、颜色等,但它们在其他类中。P.P.S 当我删除上述行时,它工作正常。

最佳答案

我没有看到任何声明字体或字体颜色的内容...

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);

我希望这会有所帮助。

关于java - 当我使用 g.drawString 时,java 中出现白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42857358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com