Java Drawing Test Codes Mouse hit test for a closed shape
2010.8.26

Language

Topics

Mouse hit test for a closed shape
2010.08.26


Mouse hit test for an unclosed shape
2010.08.26


Intersection between two arbitrary curves
2010.09.01


Normal lines to a arbitrary curve
2010.09.01


Common normal lines between two shapes
2016.10.20


Text Editor using the Input Method Framework
2010.12.01


Component Library
2012.09.16


Costom Color Chooser
2012.9.23



Chapter: 1. Source code download, 2. Test results, 3. Test item and method, 4. Comment on the resuls, 5. Test code

1. Source code downloadreturn=>page top

=> RegionHitTest.zip



2. Test resultsreturns=>page top

Figure1 Shapes to be tested

The "mouse hit test" using the contains method defined in the java.awt.Shape interface is effective for a closed shape. However, the test is also performed for an open shape just for reference.


Figure2 Test resuls;
Performs the hit test for each gridpoint and display the result by color.
blue:inside the closed shape, gray:outside, red:on the boundary


3. Test item and methodreturns=>page top
This test checks whether the contains method defined in the java.awt.Shape interface is effective or not to a closed shape in "mouse hit test". Here the "mouse hit test" is a test to determine where the mouse position is located for the closed shape on the screen.
It is enough if the mouse position is classified into three cases: (1) inside the closed shape, (2) outside the shape, or (3) on the boundary of the shape.
The "mouse hit test" is used for shape selection operation in practice, that is, when the boundary of the shape is clicked, then the shape is selected and the operations like moving or resizing the shape becomes available.
For this reason, the test should be performed on assumption that the shape's boundary has some width, at least 6 pixels or more. If the width of the boundary is assumed as 0 pixel, the shape selection operation may be very difficult for human operation, and it is not practical.

rNow, the above mentioned contains method tests if the specified point are inside the shape's boundary. This test is performed on the assumption that the the width of the shape's boundary is 0 pixel. So the "mouse hit test" can be executed as follows:

We define four test points around the current mouse position and tests whether each test point is inside the shape's boundary or not.
Here the distance from the mouse position to a test point nealy corresponds to the half width of the shape's boundary.


(1)If all the four test points are contained in the shape, the mouse position is considered to be located inside the shape.
(2)If all the four test points aren't contained in the shape, the mouse position is considered to be located outside the shape.
(3)Otherwise, the mouse position is considered to be on the boundary of the shape.




(1) Inside the closed shape (2) Outside the closed shape (3) On the boundary of the closed shape


4. Comment on the resulsreturns=>page top
As you can see in the Figure 2, this test cannot be applicable to the unclosed shapes, for example, Ex1, Ex5, Ex6 and Ex9. If the mouse is located on the connecting line from the start point of the unclosed shape to its end point, the mouse position is regarded as it is on the boundary of the unclosed shape. So the unclosed shape will be selected by the mouse click, if the mouse is located on the nonexistent connecting line.
Therefore other method is necessary for unclosed shapes.
=>Mouse hit test for an unclosed shape


5. Test codereturns=>page top

5.1 Classes Overview

Class

Description

HRegionHitTest

public class RegionHitTest extends JFrame
Creates and assembles the objects of the Jframe, JScrollPane, JViewport and the Panel.

RegionHitPanel

public class RegionHitPanel extends JPanel

Draws several closed shapes and their test results on this object shown as the Figure 1 and the Figure 2.

TestCase class TestCase
Stores each test configuration.
TestCurve

public class Curve

Creates curves needed for the tests.

Parametric Curves Segment2D, Curve2D, Rectangle2DE, RoundRectangle2DE, Ellipse2DE, Line2DE, Polyline2DE, CubicCurve2DE, GeneralCurve2DE, FergusonCurve2D
Geometric library Matrix, Matrix2D


5.2 Specifications
5.2.1 public class RegionHitPanel extends JPanel

Method

Description

createTestCase

public void createTestCase()

Creates TestCase objects.

getEnlargedBoundingBox

public Rectangle2D getEnlargedBoundingBox(Rectangle2D boundingBox, double wideEx, double heightEx)

Parameters:
box - The Rectangle2D object.
wideEx - The horizontal amount of expanding.
heightEx - The vertical amount of expanding.

Returns:

The Enlarged Rectangle2D object.

Processing:
Returns the Enlarged Rectangle2D object. Its width is the width of box + 2*wideEx andits height width is the height of box + 2*heghtEx.

getGridPoints

public Point2D[] getGridPoints(Rectangle2D box)

parameters:
box - The Rectangle2D object.
Returns: The created grid points in the box.
Processing:
Creates a set of the grid points in the box and returns it.
=>The grid point shown in the Figue 2.

containsPT

public int containsPT(Shape shape, Point2D point)

parameters:
shape - The closed shape.
point - The point to be tested.

Returns:
0 - outside the shape.
1 - inside the shape.
2 - on the boundary of the shape.
Processing:
Tests whether the point is inside the shape, ot outside the shapeR or on the boundary of the shape, and returns the test result by 0, 1 or 2.

paint

public void paint(Graphics g)

Draws the shapes in the TestCase objects on this panel by the paintShape method, and draws their test results by the paintContainsTest method.

paintShapes

public void paintShapes(Graphics g)

Draws the shapes in the TestCase objects on this panel (Figure1).

paintContainsTest

public void paintContainsTest(Graphics g)

Draws the test resuls on this panel (Figure2).



5.2.2 public class TestCase

Field

Description

title

public String title = "";

Testcase title. Figure 1, Figure 2

targetShape public Curve2D targetShape
Stores the target shape object. The shape object => Curve2D
targetShapes public Curve2D[] targetShapes
Stores the multiple target shape objects. The shape object => Curve2D
This field is used in NormalLinesBetweenShapesBasic, NormalLinesBetweenShapes.
testShape public Curve2D testShape
Stores the test shape object.
testShapes public Curve2D[] testShapes
Stores the multiple test shape objects.
This field is used in NormalLinesBetweenShapesBasic, NormalLinesBetweenShapes.
testPoints public Point2D[] testPoints
Returns the grid points created by the getGridPoints method as testPoints. The created grid points are shown in Figure 2 and each grid point is shown by colors according to it lies outside or inside the targetshape.
moveVector public Vector2D moveVector
Specify the moveVector when the test is executed by translating and copying testShape etc.
moveMax public int moveMax=0
The number of the times for translating and copying.
drawRectangle public Rectangle2D drawRectangle
Display area of the test.
maxBoundingBox public Rectangle2D maxBoundingBox
Set this field by setMaxBoundingBoxで method. Stores the rectangle which covers all given boundingBoxes.

Method

Description

moveTestShape public static Curve2D moveTestShape(Curve2D testShape, double moveX, double moveY);
Returns the Curve2D objects which are created by translating and copying the testShape etc.
setMaxBoundingBox public void setMaxBoundingBox(Rectangle2D box);
Updates the the field variable maxBoundingBox to be able to cover both rectangles given by the current field value maxBoundingBox and given by the parameter box.
setMaxBoundingBox ublic void setMaxBoundingBox(Point2D point);
Updates the the field variable maxBoundingBox to be able to cover the rectangles given by the current field value maxBoundingBox and the point given by the parameter point.


5.2.3 public class TestCurve
Creates the shapes below.



Copyright (c) 2009-2013
All other trademarks are property of their respective owners.