2012-07-25 94 views
1

我想呈現一個健康酒吧與油滑。我想要一個紅色的長方形,前面有一個綠色的矩形,以顯示剩餘的健康狀況。這是我到目前爲止有:油滑 - 填充矩形

 Rectangle healthBG = new Rectangle(healthX, healthY, healthWidth, healthHeight); 
     ShapeFill healthFill = new GradientFill(0, healthHeight/2, Color.red, healthWidth, healthHeight - 1, Color.orange, true); 

     float healthGAWidth = ((float) health/(float) maxHealth) * (float) healthWidth; 
     Rectangle healthGA = new Rectangle(healthX, healthY, healthGAWidth, healthHeight); 
     ShapeFill healthGAFill = new GradientFill(0, healthHeight/2, Color.green, healthGAWidth, healthHeight - 1, Color.green, true); 

     //g.setColor(Color.red); 
     g.draw(healthBG, healthFill); 
     //g.setColor(Color.green); 
     g.draw(healthGA, healthGAFill); 

這就是在屏幕上呈現:

Screenshot of health bar in action

回答

2

的問題是,你使用了錯誤的渲染方法。在Graphics中有兩種類型的方法draw()和fill()。您的渲染行應該是:

g.fill(healthBG, healthFill); 

或者你可以跳過製作矩形或ShapeFill,因爲圖形有填充矩形的方法:

float healthGAWidth = ((float) health/(float) maxHealth) * (float) healthWidth; 
g.fillRect(healthX, healthY, healthWidth, healthHeight); 
g.fillRect(healthX, healthY, healthGAWidth, healthHeight);