Java – JavaFX – How do I create a health bar?

I've been trying to search online for a health bar with Java FX in a side view 2D Java fighting game, but I can only find those that match "awt" and "swing".

The health bar I imagine is like the two health bars in Street Fighter games. This is a game in which two tanks in side view compete against each other.

I have a ViewManager.java this works as follows:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javafx.event.ActionEvent;
Import javafx.event.Event;
import javafx.event.EventHandler;
Import javafx.scene. *;
Import javafx.scene.control.Button;
Import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
Import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
Import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
Import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
Import javafx.scene.shape.Rectangle;
Import javafx.stage.Stage;
Import javafx.scene.paint. *;
import javafx.scene.canvas. *;

public class ViewManager {

private static final int HEIGHT = 1000;
private static finale int WIDTH = 1920;
Private Anchor Pan main Panes;
private scene mainScene;
private stage mainStage;

private final static int GAME_BUTTON_START_X = 120;
private final static int GAME_BUTTON_START_Y = 400;

list game buttons;

public ViewManager ()
{
// Initialize the game phase
initializeStage ();

// create background
createBackground ();

// Draw the tank and the ball
drawTank ();
drawBullet ();

// Initialize the game buttons
gameButtons = new ArrayList <> ();
createButtons ();


} // quit ViewManager

private void initializeStage () {
mainPane = new AnchorPane ();
mainScene = new scene (mainPane, WIDTH, HEIGHT);
mainStage = new stage ();
mainStage.setScene (mainScene);
}

public Stage getMainStage ()
{
return mainStage;
}

private void addGameButton ("WarstrikeButton" button)
{
button.setLayoutX (GAME_BUTTON_START_X);
button.setLayoutY (GAME_BUTTON_START_Y + gameButtons.size () * 100);
gameButtons.add (button);
mainPane.getChildren (). add (button);
}

private void createButtons ()
{
createMoveButton ();
createShootButton ();

} // End the createButtons method

private void createMoveButton ()
{
WarstrikeButton moveButton = new WarstrikeButton ("Move");
addGameButton (moveButton);
}

private void createShootButton ()
{
WarstrikeButton shootButton = new WarstrikeButton ("Shoot");
addGameButton (shootButton);
}

private void createBackground ()
{
Image backgroundImage = new Image ("src / com / warstrike / graphics / resources / 2d-game-background-6.jpg", 1366, 1000, false, true);
BackgroundImage background = new BackgroundImage (backgroundImage, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, null);
mainPane.setBackground (new background);
} // Stop the createBackground function

// finish class

/ * public void drawImageOnScreen (double x, double y, double rotation, string imagePath) triggers FileNotFoundException
{
// create picture
Image image1 = new image (new FileInputStream (imagePath));

// set the picture view
ImageView ImageView = new ImageView (Image1);

// Set the position of the image
imageView.setX (x);
imageView.setY (y);
imageView.setRotate (rotation);

// Set the height and width of the image view
imageView.setFitHeight (x);
imageView.setFitWidth (y);

// Set the preservation ratio of the image view
imageView.setPreserveRatio (true);


} // end drawImageOnScreen function * /

public void drawTank ()
{
// Create the canvas
Canvas canvas = new canvas (1920, 1080);

// Get the canvas graphic context

GraphicsContext gc = canvas.getGraphicsContext2D ();

// load the picture (tank 1)
String imagePath = "src / com / warstrike / graphics / resources / tanks_tankNavy3.png";
Image Image = new image (ImagePath);

// Draw the image (150px is x-position, 850px is y-position, 134px is 1.4 times the original width of 92px, 96px is 1.4 times the original height of 69px
gc.drawImage (Illus. 150, 840, 110, 82); // void drawImage (image img, double x, double y, double w, double h)


// load the picture (tank 2)
String imagePath2 = "src / com / warstrike / graphics / resources / tanks_tankDesert3.png";
Image image2 = new image (imagePath2);

// Draw the image (the Width parameter gets a negative value to rotate the tank horizontally)
gc.drawImage (Image 2, 1725, 840, -110, 82); // void drawImage (image img, double x, double y, double w, double h)

// Add the canvas to the mainPane
mainPane.getChildren (). add (canvas);
}

public void drawBullet ()
{
// Create the canvas
Canvas canvas = new canvas (1920, 1080);

// Get the canvas graphic context
GraphicsContext gc = canvas.getGraphicsContext2D ();

// Load the bulleted image
String imagePath = "src / com / warstrike / graphics / resources / tank_bullet3.png";
Image Image = new image (ImagePath);

// draw picture
gc.drawImage (Picture 265,843,37,5,25); // void drawImage (image img, double x, double y, double w, double h)

mainPane.getChildren (). add (canvas);
}

}

To have a better idea, here's a picture:

Mockup image

Only if you want to see mine Main.java:

                Import javafx.application.Application; // Import JavaFX libraries
Import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
Import javafx.scene.shape.Rectangle;
Import javafx.scene.Group;
Import javafx.scene.Parent;
Import javafx.stage.Stage;


public class Main extends Application {
@Run over
public void start (Stage primaryStage)
{
To attempt
{
ViewManager Manager = new ViewManager ();
primaryStage = manager.getMainStage ();
//primaryStage.setScene(manager.drawImageOnScreen(50, 50, 0, "src / com / warstrike / graphics / resources / tanks_tankGreen3.png"));
primaryStage.show ();
}
catch (exception e)
{
e.printStackTrace ();
} // Stop catching
} // end start

public static void main (String[] args)
{
Start (arguments);
} // end main
} // end class

TL; DR:

1) How do I create an integrity bar with JavaFX? (Obviously: the health indicator is extended for work, eg the health indicator shrinks when a bullet hits the tank)

2) Can I know if my implementation is the right way? Or should I divide different draw components (eg drawTank (), drawBullet ()) by classes and how exactly do I do that?

I am sorry that I am completely new for the game dev. Thank you for your help.