Created
May 8, 2025 19:00
-
-
Save melvincabatuan/8284ba9081b44595c7a22ea74190687b to your computer and use it in GitHub Desktop.
JavaFX Grid Coordinates Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GridCoordinatesDemo.java (JavaFX) | |
import javafx.application.Application; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.control.Label; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
public class GridCoordinatesDemo extends Application { | |
private static final int WINDOW_WIDTH = 1280; | |
private static final int WINDOW_HEIGHT = 740; | |
private static final int GRID_SIZE = 50; // Size of each grid cell | |
private Label statusLabel; | |
@Override | |
public void start(Stage primaryStage) { | |
BorderPane root = new BorderPane(); | |
// Create a canvas for drawing the grid | |
Canvas canvas = new Canvas(WINDOW_WIDTH, WINDOW_HEIGHT); | |
GraphicsContext gc = canvas.getGraphicsContext2D(); | |
drawGrid(gc); | |
// Create a pane to hold the canvas | |
Pane canvasPane = new Pane(); | |
canvasPane.getChildren().add(canvas); | |
// Create status label to show coordinates | |
statusLabel = new Label("Mouse Position: "); | |
BorderPane.setMargin(statusLabel, new Insets(5)); | |
// Add mouse event handlers | |
canvas.setOnMouseMoved(this::handleMouseMove); | |
canvas.setOnMouseClicked(this::handleMouseClick); | |
// Set up the root layout | |
root.setCenter(canvasPane); | |
root.setBottom(statusLabel); | |
// Create the scene and show the stage | |
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT); | |
primaryStage.setTitle("JavaFX Grid Coordinates Demo"); | |
primaryStage.setScene(scene); | |
primaryStage.setResizable(false); | |
primaryStage.show(); | |
} | |
private void drawGrid(GraphicsContext gc) { | |
// Clear the canvas | |
gc.setFill(Color.WHITE); | |
gc.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); | |
// Draw the grid lines | |
gc.setStroke(Color.LIGHTGRAY); | |
gc.setLineWidth(1); | |
// Draw vertical grid lines | |
for (int x = 0; x <= WINDOW_WIDTH; x += GRID_SIZE) { | |
gc.strokeLine(x, 0, x, WINDOW_HEIGHT); | |
// Draw coordinates on x-axis (every 100 pixels) | |
if (x % 100 == 0) { | |
gc.setFill(Color.BLACK); | |
gc.fillText(Integer.toString(x), x + 5, 15); | |
} | |
} | |
// Draw horizontal grid lines | |
for (int y = 0; y <= WINDOW_HEIGHT; y += GRID_SIZE) { | |
gc.strokeLine(0, y, WINDOW_WIDTH, y); | |
// Draw coordinates on y-axis (every 100 pixels) | |
if (y % 100 == 0) { | |
gc.setFill(Color.BLACK); | |
gc.fillText(Integer.toString(y), 5, y + 15); | |
} | |
} | |
// Draw origin | |
gc.setFill(Color.RED); | |
gc.fillOval(-5, -5, 10, 10); | |
gc.setFill(Color.BLACK); | |
gc.fillText("Origin (0,0)", 20, 20); | |
// Draw x and y axis with arrows | |
gc.setStroke(Color.RED); | |
gc.setLineWidth(2); | |
// X-axis | |
gc.strokeLine(0, 0, 100, 0); | |
gc.strokeLine(100, 0, 95, -5); | |
gc.strokeLine(100, 0, 95, 5); | |
gc.setFill(Color.RED); | |
gc.fillText("X", 105, 15); | |
// Y-axis | |
gc.strokeLine(0, 0, 0, 100); | |
gc.strokeLine(0, 100, -5, 95); | |
gc.strokeLine(0, 100, 5, 95); | |
gc.fillText("Y", 15, 105); | |
} | |
private void handleMouseMove(MouseEvent event) { | |
// Update status label with current mouse coordinates | |
int x = (int) event.getX(); | |
int y = (int) event.getY(); | |
statusLabel.setText(String.format("Mouse Position: (%d, %d)", x, y)); | |
} | |
private void handleMouseClick(MouseEvent event) { | |
// Show cell coordinates when clicked | |
int x = (int) event.getX(); | |
int y = (int) event.getY(); | |
System.out.printf("Clicked at (%d, %d)%n", x, y); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment