2012-07-25 120 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);