View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.gauger;
21  
22  import java.awt.Graphics;
23  import java.awt.Rectangle;
24  import java.awt.geom.Point2D;
25  
26  
27  /**
28   * Defines vertical linear transform.
29   *
30   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
31   * @version $id$
32   */
33  public class LinearVerticalTransform extends ScaleTransform
34  {
35  	private static final int MAXIMUM_WIDTH = 80;
36  	private static final int MINIMUM_WIDTH = 25;
37  	private static final double RELATIVE_X_OFFSET = 0.1;
38  	private static final double RELATIVE_Y_OFFSET = 0.05;
39  	private int width = 0;
40  	private int height = 0;
41  	private int xOffset = 0;
42  	private int yOffset = 0;
43  	private int tickOffset = 0;
44  
45  	/**
46  	 * @see ScaleTransform#setParameters(int, int, int, int, int)
47  	 */
48  	public void setParameters(int w, int h, int marginX, int marginY,
49  	    int tickOffset)
50  	{
51  		this.tickOffset = tickOffset;
52  
53  		width = w - (int)(2 * w * RELATIVE_X_OFFSET) - 5;
54  		height = h - (int)(2 * h * RELATIVE_Y_OFFSET) - 5;
55  		width = Math.min(width, MAXIMUM_WIDTH);
56  		width = Math.max(width, MINIMUM_WIDTH);
57  
58  		xOffset = (int)(0.5 * (w - width));
59  		yOffset = (int)(0.5 * (h - height));
60  
61  		Rectangle outline = new Rectangle(xOffset, yOffset, width, height);
62  		addSegment(new RectangleSegment(outline));
63  
64  		setLabelPosition((int)(w / 2), (int)(0.5 * (h - height - yOffset)));
65  	}
66  
67  	/**
68  	 * @see ScaleTransform#mapUVtoXY(PointDouble, Point)
69  	 */
70  	public void mapUVtoXY(Point2D scaleSpace, Point2D cartesianSpace)
71  	{
72  		double x = (xOffset + width * scaleSpace.getY());
73  		double y = (yOffset + scaleWidth(0.0) * (scaleSpace.getX())
74  			+ tickOffset);
75  		cartesianSpace.setLocation(x, y);
76  	}
77  
78  	/**
79  	 * @see ScaleTransform#maxXYtoUV(Point, PointDouble)
80  	 */
81  	public void mapXYtoUV(Point2D cartesianSpace, Point2D scaleScape)
82  	{
83  	}
84  
85  	/**
86  	 * @see ScaleTransform#scaleWidth(double)
87  	 */
88  	public double scaleWidth(double v)
89  	{
90  		return Math.abs(height - 2 * tickOffset);
91  	}
92  
93  	/**
94  	 * @see ScaleTransform#scaleHeight(double)
95  	 */
96  	public double scaleHeight(double u)
97  	{
98  		return width;
99  	}
100 
101 	/**
102 	 * @see com.cosylab.gui.components.gauger.ScaleTransform#measureTick(Graphics,
103 	 *      double, String)
104 	 */
105 	public int measureTick(Graphics g, double x, String text)
106 	{
107 		return g.getFontMetrics().getHeight();
108 	}
109 }
110 
111 /* __oOo__ */