2012-07-25 6 views
1

J'essaye de rendre une barre de santé avec Slick. Je veux un rectangle rouge avec un vert devant lui pour montrer la santé restante. Voilà ce que j'ai jusqu'à présent:Rectangles remplis de slick

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

C'est ce qui est rendu à l'écran:

Screenshot of health bar in action

Répondre

2

Le problème est que vous utilisez la mauvaise méthode de rendu. Dans Graphics, il existe deux types de méthodes draw() et fill(). Votre ligne de rendu devrait être:

g.fill(healthBG, healthFill); 

Sinon, vous pouvez passer directement faire un rectangle ou ShapeFill, car Graphics a une méthode pour les rectangles de remplissage:

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