I am having trouble drawing text and graphics in my game.
Basically, I am wanting to display a score in the top left corner and a filed rectangle to be a health bar.
How should I go about this?
thanks


1 2 3 4 5 6 7 8 9 10 11 | [code] public class HealthBar extends Actor { public HealthBar() { setImage( "health.jpg" ); // Draw a red image. Size doesn't matter. Name it health.jpg and put it into the image ordner } public void showHealth( int health) // health from 0 - 50 { getImage().scale(health, 30 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [code] public class TextField extends Actor { public GreenfootImage label = new GreenfootImage( 100 , 100 ); public Label() { setImage(label); } public void setText(String text) { label.drawString( text, 0 , 20 ); } } |
1 2 3 4 5 | public void setText(String text) { label.clear(); label.drawString( text, 0 , 20 ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import greenfoot.*; import java.awt.Color; import java.awt.color.*; public class HealthBar extends Actor { private final int maxValue = 200 ; // whatever max value you desire private int barValue = 200 ; private int lastValue = 0 ; public HealthBar() { } public void act() { if (barValue != lastValue) { lastValue = barValue; int pctHealth = ( int ) ( 200 * barValue / maxValue); // 200 is maxPixelHealth // Create thermometer -- sorta speak GreenfootImage imgOne = new GreenfootImage( 206 , 12 ); imgOne.setColor(Color.CYAN); imgOne.fill(); imgOne.setColor(Color.BLUE); imgOne.drawRect( 2 , 2 , 202 , 8 ); // Add mercury, if there is any temperature -- sorta speak if (pctHealth != 0 ) { GreenfootImage imgTwo = new GreenfootImage(pctHealth, 6 ); imgTwo.setColor(Color.RED); imgTwo.fill(); // Completes the second image imgOne.drawImage(imgTwo, 3 , 3 ); // Puts mercury into the thermometer } // imgOne.scale(myX, myY); // Dimensions myX and myY are whatever size you wish to make it imgOne.setTransparency( 128 ); // Adjust value as wanted or delete statement for no transparency setImage(imgOne); } } public void chgHealth( int chgValue) { barValue += chgValue; if (barValue > maxValue) barValue = maxValue; if (barValue < 0 ) barValue = 0 ; } public int getHealth() { return barValue; } } |
1 2 3 4 5 6 7 8 9 10 11 12 | public class HUD extends Actor { private GreenfootImage label = new GreenfootImage( 200 , 200 ); public void setText(String text) { setImage(label); label.clear(); label.drawString(text, 0 , 20 ); setImage(label); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class HUD extends Actor { private GreenfootImage imageScore = new GreenfootImage( 120 , 24 ); public HUD() { setImage(imageScore); Font font = imageScore.getFont(); imageScore.setFont(font.deriveFont( 24 .0F)); // use larger font } public void act() { CarWorld w = (CarWorld) getWorld(); Car aCar = w.getCar(); imageScore.clear(); imageScore.drawString( "Score: " + aCar.getScore(), 1 , 18 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int exp = 100 ; int nextlvlat = 150 ; ... ... ... ... public void act() { int value1 = ( int ) nextlvlat/exp; GreenfootImage img1 = new GreenfootImage( 206 , 6 ); img1.setColor(Color.RED); img1.fill(); if (value1 > 0 ) { GreenfootImage img2 = new GreenfootImage(value1, 6 ); img2.setColor(Color.green); img2.fill(); img1.drawImage(img2, 0 , 0 ); } setImage(img1); } |