Java Code Examples for javafx.scene.canvas.Canvas#addEventFilter()

The following examples show how to use javafx.scene.canvas.Canvas#addEventFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImagePanel.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private void createLeftPane() {
    canvas = new Canvas(image.getWidth(), image.getHeight());
    canvas.addEventFilter(MouseEvent.ANY, new ImagePanelMouseListener());
    graphics = canvas.getGraphicsContext2D();
    anchorPane.setMaxWidth(image.getWidth());
    anchorPane.setMaxHeight(image.getHeight());
    anchorPane.getChildren().add(canvas);
    initializeAnnotations();
    drawGraphics();
}
 
Example 2
Source File: HexagonMap.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
public void registerCanvasMouseLiner(final Canvas canvas) {

        canvas.addEventFilter(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
            private Hexagon oldHexagon = null;

            @Override
            public void handle(final MouseEvent me) {
                final GraphicsContext gc = canvas.getGraphicsContext2D();
                final int x = (int) me.getX();
                final int y = (int) me.getY();

                final Hexagon hexagon = getHexagonContainingPixel(x, y);
                if (hexagon != null) {

                    if (oldHexagon != null) {
                        gc.save();
                        gc.setFill(oldHexagon.getFill());
                        gc.setStroke(oldHexagon.getStroke());
                        oldHexagon.drawHexagon(gc);
                        gc.restore();
                    }
                    oldHexagon = hexagon;
                } else {
                    if (oldHexagon != null) {
                        // System.err.println("left hexagon " + oldHexagon);
                        gc.save();
                        gc.setFill(oldHexagon.getFill());
                        gc.setStroke(oldHexagon.getStroke());
                        oldHexagon.drawHexagon(gc);
                        gc.restore();
                    }

                    oldHexagon = null;
                    return;
                }

                // System.err.println("found hexagon = " + hexagon);
                gc.save();
                gc.setFill(Color.YELLOW.darker());
                gc.setStroke(hexagon.getStroke());
                hexagon.drawHexagon(gc);
                gc.restore();

                // GridPosition pos = ((Hexagon) me.getSource()).position;
                // for (HexagonCallback callBack : map.onHexExitCallback) {
                // if (callBack == null) {
                // continue;
                // }
                // try {
                // callBack.handle(map.getHexagon(pos));
                // } catch (NoHexagonFoundException e) {
                // }
                // }
            }

        });
    }