当我使用 g.drawString 时 java 出现白屏
White screen appearing in java when i use g.drawString
所以我应该制作一款在其 HUD 中有分数的游戏,但每当我使用代码时
g.drawString("Score: " + score, 15, 64);
//score is a variable
出现这样的白屏
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 我有对象,windows,颜色等,但它们在其他 类 中。
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);
希望对您有所帮助。
我知道这已经有将近 4 年的历史了,但我在 youtube 上关注相同的教程并遇到了同样的问题。我也找到了一个解决方案,所以在这里发布以防其他人偶然发现这个线程:
我的情况是这样解决的:
在 Game class 中(你有游戏循环和 render / tick 方法),查看 render 方法。
就我而言,我必须将 createBufferStrategy() 中的数字从 2 更改为 3。
所以这段代码现在可以工作了:
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
我现在可以在 HUD class
中成功 g.drawString
所以我应该制作一款在其 HUD 中有分数的游戏,但每当我使用代码时
g.drawString("Score: " + score, 15, 64);
//score is a variable
出现这样的白屏
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 我有对象,windows,颜色等,但它们在其他 类 中。 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);
希望对您有所帮助。
我知道这已经有将近 4 年的历史了,但我在 youtube 上关注相同的教程并遇到了同样的问题。我也找到了一个解决方案,所以在这里发布以防其他人偶然发现这个线程:
我的情况是这样解决的: 在 Game class 中(你有游戏循环和 render / tick 方法),查看 render 方法。 就我而言,我必须将 createBufferStrategy() 中的数字从 2 更改为 3。 所以这段代码现在可以工作了:
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
我现在可以在 HUD class
中成功 g.drawString