1 8 package com.nightlabs.editor2d.util; 9 10 import java.awt.geom.Point2D ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import com.nightlabs.editor2d.DrawComponent; 15 import com.nightlabs.editor2d.PositionConstants; 16 import com.nightlabs.math.MathUtil; 17 18 public class EditorModelUtil 19 { 20 public EditorModelUtil() { 21 super(); 22 } 23 24 public static boolean checkAlignment(int alignment) 25 { 26 if (alignment == PositionConstants.ALIGNMENT_LEFT || 27 alignment == PositionConstants.ALIGNMENT_CENTER || 28 alignment == PositionConstants.ALIGNMENT_RIGHT) 29 { 30 return true; 31 } 32 else 33 throw new IllegalArgumentException ("Param alignment = "+alignment+" is not valid!"); 34 } 35 36 public static void alignDrawComponents(List drawComponents, double distance, int alignment) 37 { 38 checkAlignment(alignment); 39 if (drawComponents != null) 40 { 41 DrawComponent firstDC = null; 42 DrawComponent lastDC = null; 43 Point2D firstPoint = null; 44 Point2D lastPoint = null; 45 if (alignment == PositionConstants.ALIGNMENT_LEFT) 46 { 47 firstDC = (DrawComponent) drawComponents.get(drawComponents.size()-1); 48 lastDC = (DrawComponent) drawComponents.get(0); 49 firstPoint = new Point2D.Double (firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY()); 50 lastPoint = new Point2D.Double (lastDC.getBounds().getMinX(), lastDC.getBounds().getMinY()); 51 for (int i=drawComponents.size()-1; i>=0; i--) 53 { 54 DrawComponent dc = (DrawComponent) drawComponents.get(i); 55 Point2D p = MathUtil.getPointOnLineWithDistance(firstPoint, lastPoint, distance * i); 56 dc.setLocation((int)p.getX(), (int)p.getY()); 58 } 59 } 60 else if (alignment == PositionConstants.ALIGNMENT_RIGHT) 61 { 62 firstDC = (DrawComponent) drawComponents.get(0); 65 lastDC = (DrawComponent) drawComponents.get(drawComponents.size()-1); 66 firstPoint = new Point2D.Double (firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY()); 67 lastPoint = new Point2D.Double (lastDC.getBounds().getMinX(), lastDC.getBounds().getMinY()); 68 for (int i=drawComponents.size()-1; i>=0; i--) 70 { 71 DrawComponent dc = (DrawComponent) drawComponents.get(i); 72 Point2D p = MathUtil.getPointOnLineWithDistance(firstPoint, lastPoint, distance * i); 73 dc.setLocation((int)p.getX(), (int)p.getY()); 75 } 76 } 77 else if (alignment == PositionConstants.ALIGNMENT_CENTER) 78 { 79 } 81 } 82 } 83 84 public static float checkFactor(float factor) 85 { 86 if (factor == Float.NEGATIVE_INFINITY || factor == Float.POSITIVE_INFINITY || factor == Float.NaN) 87 factor = 1; 88 89 return factor; 90 } 91 92 public static double getConstrainedValue(double value, double limit) 93 { 94 if (value > limit || value < -limit) { 95 return value%limit; 96 } 97 return value; 98 } 99 100 public static double calcDiffRotation(double newRotation, double oldRotation) 101 { 102 double degreesToRotate = 0; 103 newRotation = getConstrainedValue(newRotation, 360.0d); 104 105 if (newRotation == 0) 106 degreesToRotate = -oldRotation; 107 else if (newRotation <= oldRotation) 108 degreesToRotate = -(oldRotation - newRotation); 109 else 110 degreesToRotate = newRotation - oldRotation; 111 112 116 return degreesToRotate; 117 } 118 119 127 public static java.awt.Point getLeftTopCenterLocation(java.awt.Rectangle r1, java.awt.Rectangle r2) 128 { 129 int diffWidth; 130 if (r1.width > r2.width) 131 diffWidth = (r1.width - r2.width) / 2; 132 else 133 diffWidth = (r2.width - r1.width) / 2; 134 135 int diffHeight; 136 if (r1.height > r2.height) 137 diffHeight = (r1.height - r2.height) / 2; 138 else 139 diffHeight = (r2.height - r1.height) / 2; 140 141 int diffX; 142 if (r1.x > r2.x) 143 diffX = r1.x - r2.x; 144 else 145 diffX = r2.x - r1.x; 146 147 int diffY; 148 if (r1.y > r2.y) 149 diffY = r1.y - r2.y; 150 else 151 diffY = r2.y - r1.y; 152 153 return new java.awt.Point (diffX + diffWidth, diffY + diffHeight); 154 } 155 } 156 | Popular Tags |