All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gsi.chart.renderer.spi.hexagon.GridDrawer Maven / Gradle / Ivy

Go to download

This charting library ${project.artifactId}- is an extension in the spirit of Oracle's XYChart and performance/time-proven JDataViewer charting functionalities. Emphasis was put on plotting performance for both large number of data points and real-time displays, as well as scientific accuracies leading to error bar/surface plots, and other scientific plotting features (parameter measurements, fitting, multiple axes, zoom, ...).

There is a newer version: 11.2.7
Show newest version
package de.gsi.chart.renderer.spi.hexagon;

import java.util.Collection;

import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class GridDrawer {

    private final HexagonMap map;
    private javafx.scene.text.Font font = new Font(13);

    /**
     * @param map the global hex map
     */
    GridDrawer(final HexagonMap map) {
        this.map = map;
    }

    void draw(final Group root) {
        final Collection hexagons = map.getAllHexagons();
        for (final Hexagon hexagon : hexagons) {
            hexagon.addEventFilter(MouseEvent.MOUSE_CLICKED, me -> {
                final GridPosition pos = ((Hexagon) me.getSource()).position;
                for (final HexagonCallback callBack : map.onHexClickedCallback) {
                    if (callBack == null) {
                        continue;
                    }
                    final Hexagon hex = map.getHexagon(pos);
                    if (hex != null) {
                        callBack.handle(hex);
                    }
                }

            });
            hexagon.addEventFilter(MouseEvent.MOUSE_ENTERED, me -> {
                final GridPosition pos = ((Hexagon) me.getSource()).position;
                for (final HexagonCallback callBack : map.onHexEnteredCallback) {
                    if (callBack == null) {
                        continue;
                    }
                    final Hexagon hex = map.getHexagon(pos);
                    if (hex != null) {
                        callBack.handle(hex);
                    }
                }
            });

            hexagon.addEventFilter(MouseEvent.MOUSE_EXITED, me -> {
                final GridPosition pos = ((Hexagon) me.getSource()).position;
                for (final HexagonCallback callBack : map.onHexExitCallback) {
                    if (callBack == null) {
                        continue;
                    }
                    final Hexagon hex = map.getHexagon(pos);
                    if (hex != null) {
                        callBack.handle(hex);
                    }
                }
            });

            root.getChildren().add(hexagon);

            if (map.renderCoordinates) {
                final Text text = new Text(hexagon.position.getCoordinates());
                text.setFont(font);
                final double textWidth = text.getBoundsInLocal().getWidth();
                final double textHeight = text.getBoundsInLocal().getHeight();
                text.setX(hexagon.getGraphicsXoffset() - textWidth / 2);
                text.setY(hexagon.getGraphicsYoffset() + textHeight / 4); // Not
                                                                          // sure
                                                                          // why,
                                                                          // but
                                                                          // 4
                                                                          // seems
                                                                          // like
                                                                          // a
                                                                          // good
                                                                          // value

                root.getChildren().add(text);
            }
        }

    }



    void draw(final Canvas canvas) {
        //registerCanvasMouseLiner(canvas); // TODO move elsewhere

        final GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());

        for (final Hexagon hexagon : map.getAllHexagons()) {
            // draw hexagon according to Node specifications
        	hexagon.draw(gc);

            if (map.renderCoordinates) {
            	hexagon.renderCoordinates(gc);
            }
        }
    }

    void drawContour(final Canvas canvas) {
        //registerCanvasMouseLiner(canvas); // TODO move elsewhere

        final GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());

        for (final Hexagon hexagon : map.getAllHexagons()) {
            // draw hexagon contour according to Node specifications
        	hexagon.drawContour(gc);

            if (map.renderCoordinates) {
            	hexagon.renderCoordinates(gc);
            }
        }
    }

    /**
     * @param x pixel coordinate
     * @param y pixel coordinate
     * @param hexagonHeight size of hexagons
     * @param xPadding x-padding between tiles
     * @param yPadding y-padding between tiles
     * @return the GridPosition that contains that pixel
     */
    public static GridPosition pixelToPosition(int x, int y, final int hexagonHeight, final int xPadding,
            final int yPadding) {
        x = x - xPadding;
        y = y - yPadding;
        final double hexagonRadius = (double) hexagonHeight / 2;
        final double q = (1.0 / 3.0 * Math.sqrt(3.0) * x - 1.0 / 3.0 * y) / hexagonRadius;
        final double r = 2.0 / 3.0 * y / hexagonRadius;
        return GridPosition.hexRound(q, r);
    }

    void setFont(final Font font) {
        this.font = font;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy