Java examples for JavaFX:Rectangle
opposite JavaFX Rectangle Corner
import javafx.event.EventTarget; import javafx.geometry.Bounds; import javafx.geometry.Point2D; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Region; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.shape.Rectangle; public class Main{ static double SLOP = 8; static public Point2D oppositeCorner(MouseEvent ev, Rectangle r) { return oppositeCorner(ev.getX(), ev.getY(), r); }//from www.java 2 s. co m static public Point2D oppositeCorner(Point2D pt, Rectangle r) { return oppositeCorner(pt.getX(), pt.getY(), r); } static public Point2D oppositeCorner(Point2D pt, StackPane r) { return oppositeCorner(pt.getX(), pt.getY(), r.getBoundsInLocal()); } static public Point2D oppositeCorner(Point2D pt, Bounds r) { return oppositeCorner(pt.getX(), pt.getY(), r); } static public Point2D oppositeCorner(double inX, double inY, Rectangle r) { double outX = -1, outY = -1; double x = inX; double y = inY; double left = r.getX(); double right = left + r.getWidth(); double top = r.getY(); double bottom = top + r.getHeight(); if (Math.abs(x - left) < SLOP) outX = right; else if (Math.abs(x - right) < SLOP) outX = left; else return null; if (Math.abs(y - top) < SLOP) outY = bottom; else if (Math.abs(y - bottom) < SLOP) outY = top; else return null; return new Point2D(outX, outY); } static public Point2D oppositeCorner(double inX, double inY, Bounds r) { double outX = -1, outY = -1; double x = inX; double y = inY; double left = r.getMinX(); double right = left + r.getWidth(); double top = r.getMinY(); double bottom = top + r.getHeight(); if (Math.abs(x - left) < SLOP) outX = right; else if (Math.abs(x - right) < SLOP) outX = left; else return null; if (Math.abs(y - top) < SLOP) outY = bottom; else if (Math.abs(y - bottom) < SLOP) outY = top; else return null; return new Point2D(outX, outY); } static public Point2D oppositeCorner(MouseEvent ev) { if (ev == null) return null; EventTarget target = ev.getTarget(); if (target instanceof Rectangle) return RectangleUtil.oppositeCorner(ev.getX(), ev.getY(), (Rectangle) target); if (ev.getTarget() instanceof StackPane) return RectangleUtil.oppositeCorner(ev.getX(), ev.getY(), ((StackPane) target).getBoundsInLocal()); if (ev.getTarget() instanceof VBox) return RectangleUtil.oppositeCorner(ev.getX(), ev.getY(), ((VBox) target).getBoundsInLocal()); return null; } }