KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > renderer > Transformation


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.renderer;
20
21 import jcckit.graphic.Anchor;
22 import jcckit.graphic.GraphPoint;
23 import jcckit.graphic.ClippingRectangle;
24
25 /**
26  * Transformation between device-independent coordinates
27  * and standard Java coordinates. The aspect-ratio will
28  * be the same. The position in the canvas is determined by a
29  * {@link jcckit.graphic.Rectangle Rectangle} defining a (virtual)
30  * paper which is placed in the canvas according to an anchor point.
31  * Depending on the aspect ratio of the canvas the paper width or
32  * height occupies the canvas width or height.
33  *
34  * @author Franz-Josef Elmer
35  */

36 public class Transformation {
37   private final double _scale, _x0, _y0;
38
39   /**
40    * Creates an instance for the specified canvas size, paper size,
41    * and anchor points of the paper.
42    * @param width Width of the canvas.
43    * @param height Height of the canvas.
44    * @param paper Rectangle defining the paper in device-independent
45    * coordinates.
46    * @param horizontalAnchor Horizontal anchor of the paper in the canvas.
47    * @param verticalAnchor Vertical anchor of the paper in the canvas.
48    */

49   public Transformation(int width, int height, ClippingRectangle paper,
50                         Anchor horizontalAnchor, Anchor verticalAnchor) {
51     double pWidth = paper.getMaxX() - paper.getMinX();
52     double pHeight = paper.getMaxY() - paper.getMinY();
53     _scale = Math.min(width / pWidth, height / pHeight);
54     _x0 = 0.5 * horizontalAnchor.getFactor() * (width - _scale * pWidth)
55           - _scale * paper.getMinX();
56     _y0 = 0.5 * verticalAnchor.getFactor() * (_scale * pHeight - height)
57           + height + _scale * + paper.getMinY();
58   }
59
60   /** Transforms the device-independent x coordinate into Java coordinates. */
61   public int transformX(double x) {
62     return trim(_scale * x + _x0);
63   }
64
65   /** Transforms the device-independent y coordinate into Java coordinates. */
66   public int transformY(double y) {
67     return trim(_y0 - _scale * y);
68   }
69
70   /** Transforms the device-independent width into Java width. */
71   public int transformWidth(double width) {
72     return trim(_scale * width + 0.5);
73   }
74
75   /** Transforms the device-independent height into Java height. */
76   public int transformHeight(double height) {
77     return trim(_scale * height + 0.5);
78   }
79   
80   private static int trim(double number)
81   {
82     return number > Short.MAX_VALUE
83               ? Short.MAX_VALUE
84               : (number < Short.MIN_VALUE ? Short.MIN_VALUE : (int) number);
85   }
86
87   /**
88    * Transforms a point in Java coordinates back into device-independent
89    * coordinates.
90    */

91   public GraphPoint transformBack(int x, int y) {
92     return new GraphPoint((x - _x0) / _scale, (_y0 - y) / _scale);
93   }
94 }
95
96
Popular Tags