binibiningtinamoran

LineInfoDisplayer.java

Nov 13th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import javafx.scene.shape.Line;
  2.  
  3. @FunctionalInterface
  4. public interface LineInfoDisplayer {
  5.  
  6.     String getInfo(Line line);
  7.  
  8.     public static enum InfoType {
  9.         DISTANCE, MIDPOINT, VERTHORZ;
  10.     }
  11.      
  12.     public static LineInfoDisplayer createLineInfoDisplayer(InfoType type) {
  13.         //if (Line.line != null)
  14.         if (type == InfoType.DISTANCE) {
  15.             return line -> {
  16.                 String outputString = "";
  17.  
  18.                 if (line != null) {
  19.                     double xParam = Math.abs(line.getEndX() - line.getStartX());
  20.                     double yParam = Math.abs(line.getEndY() - line.getStartY());
  21.                     double distance = Math.hypot(xParam, yParam);
  22.                     outputString = String.format("DISTANCE: %,.2f", distance);
  23.                 }
  24.                 return outputString;
  25.             };
  26.         } else if (type == InfoType.MIDPOINT) {
  27.             return line -> {
  28.                 String outputString = "";
  29.  
  30.                 if (line != null) {
  31.                     double xMid = (line.getStartX() + line.getEndX()) / 2;
  32.                     double yMid = (line.getStartY() + line.getEndY()) / 2;
  33.                     outputString = String.format("MIDPOINT: %,.0f,%,.0f", xMid, yMid);
  34.                 }
  35.                 return outputString;
  36.             };
  37.         } else if (type == InfoType.VERTHORZ) {
  38.             return line -> {
  39.                 String outputString = "";
  40.  
  41.                 if (line != null) {
  42.                     boolean isVertical = Double.compare(line.getStartX(), line.getEndX()) == 0;
  43.                     boolean isHorizontal = Double.compare(line.getStartY(), line.getEndY()) == 0;
  44.                     outputString = String.format("HORIZONTAL? %b\nVERTICAL? %b",isHorizontal,
  45.                             isVertical);
  46.                 }
  47.                 return outputString;
  48.  
  49.             };
  50.         } else {
  51.             return null;
  52.         }
  53.     } // end createLineInfoDisplayer()
  54. } // end LineInfoDisplayer
Add Comment
Please, Sign In to add comment