Java Drawing DrawTop

Language

JP  US  UK

 

Connection Listener

 H. Jyounishi, Tokyo Japan
 

Frame (Index), No frame                 version:0.3(latest)  

Summary: This class provides the function that calculates and displays a point or a characteristic point on a shape's boundary when the mouse is moved to the shape and close to the shape's boundary..
Class on this page: ConnectionLS (LS: Listener), ConnectionListener, ConnectionEvent

1. Overview

If the mouse is close to the boundary of a shape, then this class calculates a mouse hit point - a point lying on the shape's boundary and close to the mouse - and displays it with the mark or . The mouse hit point is exactly lying on the boundary, and especially the mouse hit point with the mark of is a characteristic point on the shape's boundary (Figure 1, Figure 2).
This class also provides the classes such as Rotate or Translate - which don't implement the MouseListener or MouseMotionListener - with an easy way to calculate and display a mouse hit point, and an easy way to get the exact coordinates of the mouse hit point. The other classes such as CreateShapeLS or MoveResizeShapeLS utilizes the drawMouseHitPT method of this class to connect a connector to a shape at a mouse hit point (connection point).

See=> CreateShapeLS: Connector enable option, MoveResizeShapeLS: Figure 2.1, Figure 2.2, Rotate, Translate.

Figure 1. Characteristic points

The characteristic points are endpoints of a line/curve segment, and north, south, east and west points of an ellipse/circle.


Figure 2. The examples of the marks.


:
A mouse hit point is expected to be lying on a shape's boundary and close to the current mouse position. The method to calculate a near (or nearest) point on the boundary from the current mouse position is often inappropriate to get an accurate mouse hit point. For example, consider the case that we create a horizontal line and connect it to an ellipse (Figure_(a)). By the method mentioned above, the created line may be slightly declined (Figure_(b)), because the near (or nearest) point on the ellipse's boundary from the current mouse position is slightly below the horizontal line in the case of Figure_(b). So we should calculate a point which is projected horizontally from the end point of the line and connect the horizontal line to the projected point (Figure_(c)).

Figure_(a) Creating a horizontal line Figure_(b) The created line is slightly declined. Figure_(c) A perfect horizontal line

The method to calculate a mouse hit point.
∙ To calculate a projected point, we use the Curve2DUtil.getProjectionLines in the getHitPT method.
∙ To calculate a nearest point, we use the Curve2DUtil.getShortestLine in the getHitPT method.


2. Class ConnectionLS return=>page top

Field Description
listenerList protected EventListenerList listenerList = new EventListenerList();
A list storing the ConnectionListener.
startPoint
Point2D startPoint
This field stores the start point of mouse dragging.
endPoint Point2D endPoint
This field stores the current or end point of mouse dragging.
mouseHitPoint CurvePT mouseHitPoint
Stores the returned value of the drawMouseHitPT method to this field.
clicked
boolean clicked
The mouseClicked method sets this field true, and if the mouse is unexpectedly moved in the mouse click operation, then the mouseReleased method sets this field true.

Method Description
isMouseListener boolean isMouseListener(MouseListener listener)
Inquires whether this object is registered to the ListenerPanel or not.
isMouseMotionListener public boolean isMouseMotionListener(MouseMotionListener listener)
Inquires whether this object is registered to the ListenerPanel or not.
activateListener private void activateListener(boolean activate)
If the activate parameter is true, this method registers this object to the ListenerPanel as a MouseListener and MouseMotionListener.
If the activate parameter is false, this method removes this object from the ListenerPanel.
addConnection
Listener
public void addConnectionListener(ConnectionListener listener)
Adds the specified ConnectionListener object to the listenerList for receiving a ConnectionEvent from the ConnectionLS when shapes are selected in the ConnectionLS.
removeConnection
Listener
public void removeConnectionListener(ConnectionListener listener)
Remove a ConnectionListener object from the listenerList.
removeConnection
Listener
public void removeConnectionListener()
Remove all the ConnectionListener object from the listenerList.
fireEvent public void fireEvent(ConnectionEvent event)
Reports a ConnectionEvent to the objects registered in the listenerList when shapes are selected in the ConnectionLS.
mousePressed
public void mousePressed(MouseEvent e)
Calls the drawMouseHitPT method and sets its returned value to the mouseHitPoint field.
Sets the current mouse position to the startPoint field.
mouseDragged public void mouseDragged(MouseEvent e)
Calls the drawMouseHitPT method and sets its returned value to the mouseHitPoint field.
Sets the current mouse position to the endPoint field.
mouseReleased
public void mouseReleased(MouseEvent e)
If the mouse is moved unexpectedly and slightly in the mouse click operation, then the following processing will be done and sets true to the clicked field.
This method calls the fireEvent method to send the startPoint or the point of the mouseHitPoint to the objects which implement ConnectionListener.
mouseClicked public void mouseClicked(MouseEvent e)
This method calls the fireEvent method to send the startPoint or the point of the mouseHitPoint to the objects which implement ConnectionListener.
mouseEntered public void mouseEntered(MouseEvent e)
Nothing is done.
mouseExited public void mouseExited(MouseEvent e)
Nothing is done.
mouseMoved public void mouseMoved(MouseEvent e)
Nothing is done.
drawMouseHitPT public CurvePT drawMouseHitPT(int ctrl, Point2D mousePT, Vector2D vec, ShapeContainer movingContainer)
Parameters:
ctrl - If the mouse is dragged with holding down the Shift/Ctrl/Alt key, then the value of ctrl is 1/2/3
mousePT - The mouse position.
vec - The direction in which the mouse hit point is searched. If vec==null, it is searched in any direction around the mousePT.
movingContainer - The ShapeContainer objects which are excluded from searching a mouse hit point.
Returns:
If the mouse hit point is found on a shape's boundary, returns it, otherwise returns null.
Processing:
Calls the next method with the different parameters.
drawMouseHitPT private CurvePT drawMouseHitPT(Point2D mousePT, Vector2D vec, double boundaryTol, double pointTol, ShapeContainer movingContainer)
Parameters:
mousePT - The mouse position.
vec - The direction in which the mouse hit point is searched. If vec==null, it is searched in any direction around the mousePT.
boundaryTol - The tolerance by which the mousePT is on a boundary of a shape.
pointTol - The tolerance by which the mousePT is equal to a characteristic point of a shape.
movingContainer - The ShapeContainer objects which are excluded from searching a mouse hit point.
: In the case of creating a shape, the shape being created must be excluded, or in the case of moving/resizing shapes, the shapes being moved/resized must be excluded.
Returns:
If the mouse hit point is found on a shape's boundary, then returns it otherwise returns null.
Processing:
∙ Calls the MousePsotionLS.getAllMousePositionInfo method.

This is to get the mouse hit testing result (MousePositionInfo objects) and checks whether or not a mouse hit shape exists.

∙ If a hit shape exists, calls the getHitPT method to calculate the mouse hit point accurately.
∙ If a mouse hit point is found, draws the mark (or ) at the mouse hit point by the DrawShapeUtil.drawTempShape method.
getHitPT private CurvePT getHitPT(Point2D mousePT, Vector2D vec, double boundaryTol,
ShapeContainer target)

Parameters:
mousePT - The mouse position.
vec - The direction in which the mouse hit point is searched. If vec==null, it is searched in any direction around the mousePT.
boundaryTol - The tolerance by which the mousePT is on a boundary of a shape.
target - The ShapeContainer object. A mouse hit point is searched on the boundary of the target.
Returns:
If the mouse hit point is found on the boundary of the target, returns it, otherwise returns null.
Processing:
∙ If the vec isn't null nor 0 vector, then this method tries to find a mouse hit point by the Curve2DUtil.getProjectionLines method.
∙ If the vec is null or 0 vector, then this method tries to find a mouse hit point by the Curve2DUtil.getShortestLine method.
getCloseCharacteristic
Point
public double getCloseCharacteristicPoint(ShapeContainer container, double t, double pointTol)
Parameters:
container - The ShapeContainer object.
t - The curve parameter representing the point on the boundary of the container.
pointTol - The tolerance by which the mousePT is equal to a characteristic point of a shape.
Returns:
Return the curve parameter of the characteristic point, if one of the characteristic points of the container is close enough to the point denoted by the t, otherwise return -1.0.
Processing:
Gets the characteristic points by the Curve2D.getCharacteristicPoints method, and tests if the distance from each of the characteristic points to the point denoted by the t is less than DrawParameters.ConnectionSmallTolerance. If it is less, then returns the curve parameter of the the characteristic point.
drawMark private void drawMark(Point2D point, int type)
Parameters:
point - The mark position.
type - The mark type (0/1).

If type==0, then draws the mark of , otherwise draws the mark of at the point.
by the DrawPanelUtil.drawTempShape method.



3. Interface ConnectionListener return=>page top
A listener interface for receiving a ConnectionEvent. A class that receives such event implements this interface, and then registers itself by calling the addConnectionListener method of the ConnectionLS.

Method Description
connected
public void connected(ConnectionEvent event)
Invoked when the action described by the specified ConnectionEvent occurs.


4. Class ConnectionEvent return=>page top
public class ConnectionEvent extends EventObject
Field Description
curvePT protected CurvePT curvePT=null
The CurvePT object representing a point on a curve.

Method Description
Constructor public ConnectionEvent(Object source, CurvePT curvePT)
Parameters:
source - The object on which the Event initially occurred.
curvePT - The CurvePT object representing a point on a curve.
getCurvePT public CurvePT getCurvePT()
Return the value of the curvePT field.



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