Posted in JavaFX

Location Crosshair


Location Crosshair is used in charts to show the current location of mouse in chart with moving lines. Here is screenshot of javafx program
crosshair
Source code of this program

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class LocationCrosshair extends Application {

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

 @Override
 public void start(Stage stage) {
 AnchorPane root = new AnchorPane();

 Pane crosshairArea = new Pane();
 crosshairArea.setPrefWidth(300);
 crosshairArea.setPrefHeight(300);
 crosshairArea.setTranslateX(20);
 crosshairArea.setTranslateY(20);
 crosshairArea.setStyle("-fx-border-color:black");
 crosshairArea.setCursor(Cursor.NONE);

 final Label label = new Label("");
 crosshairArea.getChildren().add(label);

 final Line horizontalLine = new Line(0,0,crosshairArea.getPrefWidth(),0);
 final Line verticalLine = new Line(0,0,0,crosshairArea.getPrefHeight());

 crosshairArea.getChildren().add(horizontalLine);
 crosshairArea.getChildren().add(verticalLine);

 crosshairArea.setOnMouseMoved(new EventHandler() {
 @Override
 public void handle(MouseEvent event) {
 label.setVisible(true);
 label.setText(" Location: "+event.getX()+", "+event.getY());

 horizontalLine.setStartY(event.getY());
 horizontalLine.setEndY(event.getY());

 verticalLine.setStartX(event.getX());
 verticalLine.setEndX(event.getX());

 }
 });
 crosshairArea.setOnMouseExited(new EventHandler() {
 @Override
 public void handle(MouseEvent event) {
 label.setVisible(false);
 }
 });
 root.getChildren().add(crosshairArea);

 Scene scene = new Scene(root);
 stage.setScene(scene);
 stage.setTitle("Location Crosshair");
 stage.setWidth(500);
 stage.setHeight(470);
 stage.show();
 }
}
Advertisements

3 thoughts on “Location Crosshair

  1. Thanks for this example. But why do the crosshair lines appear thick and somewhat fuzzy? I have tried setting the stroke width to 1 and turning off smoothing but they still don’t appear as crisp single-pixel wide lines. They seem to be antialiased at the expense of clarity. Maybe this is a JavaFX thing?

    1. to get crispe line here is the code please update this mouseMoved Handler

      crosshairArea.setOnMouseMoved(new EventHandler() {
      @Override
      public void handle(MouseEvent event) {
      label.setVisible(true);
      label.setText(” Location: “+event.getX()+”, “+event.getY());

      horizontalLine.setStartY(event.getY() + 0.5);
      horizontalLine.setEndY(event.getY() + 0.5);

      verticalLine.setStartX(event.getX() + 0.5);
      verticalLine.setEndX(event.getX() + 0.5);
      }
      });

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s