KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > graphic > ClippingRectangle


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.graphic;
20
21 /**
22  * Immutable class of a rectangular clipping area.
23  *
24  * @author Franz-Josef Elmer
25  */

26 public class ClippingRectangle implements ClippingShape {
27   private final double _minX, _minY, _maxX, _maxY;
28
29   /**
30    * Creates an instance for the specified coordinates of
31    * two opposite corner points.
32    */

33   public ClippingRectangle(double x1, double y1, double x2, double y2) {
34     _minX = Math.min(x1, x2);
35     _minY = Math.min(y1, y2);
36     _maxX = Math.max(x1, x2);
37     _maxY = Math.max(y1, y2);
38   }
39
40   /**
41    * Returns <tt>true</tt> if the specified point is inside this
42    * rectangle.
43    */

44   public boolean isInside(GraphPoint point) {
45     double x = point.getX();
46     double y = point.getY();
47     return _minX <= x && x <= _maxX && _minY <= y && y <= _maxY;
48   }
49
50   /** Returns the minimum x value. */
51   public double getMinX() {
52     return _minX;
53   }
54
55   /** Returns the maximum x value. */
56   public double getMaxX() {
57     return _maxX;
58   }
59
60   /** Returns the minimum y value. */
61   public double getMinY() {
62     return _minY;
63   }
64
65   /** Returns the maximum y value. */
66   public double getMaxY() {
67     return _maxY;
68   }
69
70   /** Returns this instance. */
71   public ClippingRectangle getBoundingBox() {
72     return this;
73   }
74   
75   /** Returns a {@link Rectangle}. */
76   public BasicGraphicalElement getGraphicalElement() {
77     return new Rectangle(new GraphPoint(0.5 * (_minX + _maxX),
78                                         0.5 * (_minY + _maxY)),
79                          _maxX - _minX, _maxY - _minY, null);
80   }
81 }
82
Popular Tags