| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo; |
| 27 |
|
| 28 |
import java.awt.geom.AffineTransform; |
| 29 |
import java.awt.geom.Point2D; |
| 30 |
import java.awt.geom.Rectangle2D; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class ViewTransform { |
| 36 |
|
| 37 |
double scaleX; |
| 38 |
double scaleY; |
| 39 |
|
| 40 |
int width; |
| 41 |
int height; |
| 42 |
|
| 43 |
Point2D topLeft; |
| 44 |
Point2D bottomRight; |
| 45 |
|
| 46 |
AffineTransform transform; |
| 47 |
|
| 48 |
public ViewTransform(Rectangle2D bounds, int width, int height) { |
| 49 |
|
| 50 |
set(bounds, width, height); |
| 51 |
} |
| 52 |
|
| 53 |
public AffineTransform getTransform() { |
| 54 |
return transform; |
| 55 |
} |
| 56 |
|
| 57 |
public Rectangle2D getViewport() { |
| 58 |
return new Rectangle2D.Double(topLeft.getX(), topLeft.getY(), |
| 59 |
bottomRight.getX() - topLeft.getX(), bottomRight.getY() - topLeft.getY()); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public void set(Rectangle2D bounds, int width, int height) { |
| 64 |
topLeft = new Point2D.Double(bounds.getMinX(), bounds.getMinY()); |
| 65 |
bottomRight = new Point2D.Double(bounds.getMaxX(), bounds.getMaxY()); |
| 66 |
this.width = width; |
| 67 |
this.height = height; |
| 68 |
computeScalesAndTransform(); |
| 69 |
} |
| 70 |
|
| 71 |
public void setBounds(Rectangle2D bounds) { |
| 72 |
topLeft = new Point2D.Double(bounds.getMinX(), bounds.getMinY()); |
| 73 |
bottomRight = new Point2D.Double(bounds.getMaxX(), bounds.getMaxY()); |
| 74 |
computeScalesAndTransform(); |
| 75 |
} |
| 76 |
|
| 77 |
public void setSize(int width, int height) { |
| 78 |
this.width = width; |
| 79 |
this.height = height; |
| 80 |
computeScalesAndTransform(); |
| 81 |
} |
| 82 |
|
| 83 |
private void computeScalesAndTransform() { |
| 84 |
scaleX = width / (bottomRight.getX() - topLeft.getX()); |
| 85 |
scaleY = height / (bottomRight.getY() - topLeft.getY()); |
| 86 |
|
| 87 |
transform = computeFullTransform(); |
| 88 |
} |
| 89 |
|
| 90 |
private AffineTransform getTranslate() { |
| 91 |
return AffineTransform.getTranslateInstance(-topLeft.getX(), -bottomRight.getY()); |
| 92 |
} |
| 93 |
|
| 94 |
private AffineTransform getScale() { |
| 95 |
return AffineTransform.getScaleInstance(scaleX, -scaleY); |
| 96 |
} |
| 97 |
|
| 98 |
private AffineTransform computeFullTransform() { |
| 99 |
AffineTransform transform = getScale(); |
| 100 |
transform.concatenate(getTranslate()); |
| 101 |
return transform; |
| 102 |
} |
| 103 |
} |