2017-03-17 55 views
1

所以我應該做一個遊戲,在其HUD一個分數,但每當我使用的代碼出現在Java中,當我使用白色屏幕g.drawString

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當我刪除,說的行,它工作正常。

+0

您需要使用.setBackground(Color.black)添加背景顏色; – Abi

+0

我有一個背景顏色,對象和東西,但他們在其他類。 – Stickzz107

回答

2

我沒有看到任何聲明的字體或字體顏色...

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

我希望這有助於。