Hi. I’ve tried to search for this topic so I don’t repeat a question, but I didn’t have any luck. I’m new here, so please don’t crucify me if this is an accidental repeat.
I’m working on a final project for my OOP/Java class and I’m stuck. I can create the grid (game board) and create my ‘ship’ (1x5 grid of red tiles) within the game board. While I can hard code the location change, I cannot seem to figure out how to get the ‘ship’ to place at the clicked tile, instead of (0,0). I’m sure this is an incredibly obvious fix, but I can’t manage to figure it out. Can anyone help? This code is messy/redundant I’m sure - I’ll work on streamlining later. Right now I’m just trying to figure out this click/placement issue.
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class practice extends Application
{
private Tiles[][] gameBoard = new Tiles[9][9]; //sets size for game board
private Ships[][] carrier = new Ships[1][5];
boolean clicked = false;
Pane root = new Pane();
@Override
public void start(Stage stage) throws Exception
{
stage.setScene(new Scene(createGame()));
stage.show();
}
private Parent createGame()
{
root.setPrefSize(450, 450);
for (int i = 0; i < gameBoard.length; i++)
{
for (int j = 0; j < gameBoard[i].length; j++)
{
Tiles tile = new Tiles();
tile.setTranslateX(i * 50);
tile.setTranslateY(j * 50);
root.getChildren().add(tile);
gameBoard[i][j] = tile;
tile.setOnMouseClicked(event ->
{
if (event.getButton() == MouseButton.PRIMARY)
{
clicked = true;
for (int row = 0; row < 5; row++)
{
for (int column = 0; column < 1; column++)
{
Ships ship = new Ships();
ship.setTranslateX(row * 50);
ship.setTranslateY(column * 50);
root.getChildren().add(ship);
}
}
}
else if (event.getButton() == MouseButton.SECONDARY)
{
if (clicked == false)
{
for (int row = 0; row < 1; row++)
{
for (int column = 0; column < 5; column++)
{
Ships ship = new Ships();
ship.setTranslateX(row * 50);
ship.setTranslateY(column * 50);
root.getChildren().add(ship);
}
}
}
}
});
}
}
return root;
}
private class Ships extends GridPane
{
public Ships()
{
Rectangle gameTile = new Rectangle(50,50);
gameTile.setFill(Color.RED);
gameTile.setStroke(Color.BLACK);
getChildren().addAll(gameTile);
}
}
private class Tiles extends GridPane
{
public Tiles()
{
Rectangle gameTile = new Rectangle(50,50);
gameTile.setFill(Color.LIGHTBLUE);
gameTile.setStroke(Color.BLACK);
getChildren().addAll(gameTile);
}
}
/*main method that executes launch*/
public static void main(String[] args)
{
launch(args);
}
}