1

I am making a JavaFX based game that shoots drones using a spaceship to defend your base. I want to wrap a shape around an image to create a hitbox for it.

This will allow the laser to cause damage to the drone whenever it touches the hitbox. It will make the game look better than the laser hitting an invisible wall (the shape's dimensions), and not the actual image.

My question is, is there any built-in library that JavaFX provides to solve this problem, or do you have to use a calculus formula to solve this?

I have tried to look over the API's of JavaFX, but I cannot seem to find anything useful.

// Getting the images for the shapes
Image spaceCity = new Image("com/images/spaceCity.jpg");
Image spaceShip = new Image("com/images/spaceShip.png");

// Making the graphics
Rectangle space_Ship = new Rectangle(0, positionOfShipY, 191, 300);
Rectangle background = new Rectangle(0, 0, STAGE_WIDTH, STAGE_HEIGHT);

I have my example here (I do not know why Stack overflow does not allow me to upload images):

https://docs.google.com/document/d/1A8HJ61jhthBhd7wr6xrZNLlsTcQJNLUhTzrucvNY3BY/view?usp=sharing

c0der
  • 18,467
  • 6
  • 33
  • 65
Compiler v2
  • 3,509
  • 10
  • 31
  • 55
  • What happens if you use a `png` with an invisible background? – SedJ601 Mar 24 '19 at 14:44
  • Just to clarify, the ship is on top of the background, and the red box around it (in the Google Doc screenshot) is not the actual shape dimensions. The background of the ship is already invisible. It's not the ship I need to do this for, it's for the drones that I have not yet added images for. That the ship will shot at. – Compiler v2 Mar 24 '19 at 15:09
  • The ship will use a PathTransition object to move the laser (Rectangle) towards the target. – Compiler v2 Mar 24 '19 at 15:11

2 Answers2

4

Set the shape's dimensions based on image, and place both in a StackPane:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class FxTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        String imagePath = "https://png.pngtree.com/png-clipart/20190118/ourmid/"
                          + "pngtree-hand-drawn-spaceship-grey-spaceship-alien-spaceship-blue"
                          + "-spaceship-border-png-image_450067.jpg";
        Image image =  new Image(imagePath);
        Rectangle rec = new Rectangle(0, 0, 50+image.getWidth(), 50+image.getHeight());
        rec.setFill(Color.AQUA);

        StackPane root = new StackPane();
        root.getChildren().add(rec);
        root.getChildren().add(new ImageView(image));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(null);
    }
} 

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65
1

You can use a DropShadow to draw a border around your space ship image:

Image image = new Image("/path/to/image.png");
ImageView imageView = new ImageView(image);
DropShadow ds = new DropShadow();
ds.setColor(Color.RED);
ds.setSpread(10);
imageView.setEffect(ds);
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
  • 1
    The thing is, that it cannot be used in a shape, as it uses setFill() method. Different folders in the JavaFX API. – Compiler v2 Mar 24 '19 at 19:07