Debian Codegraph @

src/dr/geo/ViewTransform.java

raw symbols SCIP
1 /*
2 * ViewTransform.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Alexei Drummond
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 }