Java Drawing DrawTop

Language

JP  US  UK

 

Modifying shape

 H. Jyounishi, Tokyo Japan
 

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

Summary: The function of modifying a shape can change a corner radius of a rounded rectangle, move a junction point of a polyline or a cubic curve, add a new junction point to it or delete a junction point from it. In addition, the function can change a tangent direction of a cubic curve.
Classes on this page: ModifyShape, ModifyShapeAction, ModifyShapeLS


1. Overview
1.1 Examples
(1) Changing a corner radius return=>page top
Original round rectangle Enlarge the corner radius Maximizing the corner radius

Figure 1.1 Changing the corner radius
(The blue marks are the control points)


(2) Modifying a polyline return=>page top

A junction point or a line segment of a selected polyline can be moved. A new junction point can be added to the polyline and a existing junction point can be removed from it.
In Figure 1.2, the marks filled with blue are used for dragging a junction point of a polyline. And the marks filled with white are used for dragging a line segment of a polyline.
: If a junction point is dragged with holding down the Shift or Ctrl key, then the junction point will be moved horizontally or vertically. Similarly, a line segment is dragged with holding down the Shift or Ctrl key, then the direction of the line segment will be forcibly changed to horizontal or vertical direction.

(a) Original

(b) Move the left side line to right.

(c) Change line segmens to horizontal or vertical segments by moving them with holding down Shift/Ctrl key.

(d) Completed drawing

Figure 1.2 Moving a junction point a line segment of the polyline


(e) Add junction points to Figure (d) (f) Delete a junction point from figure (e)

Figure 1.3 adding and deleting a junction point


(3) Modifying a cubic curve return=>page top

As well as a polyline, a junction point and a tangent line (direction) of a selected cubic curve can be moved. A new junction point can be added to the cubic curve and a junction point can be deleted from it.
In Figure 1.4, the marks filled with blue and filled with white are used for dragging a junction point and dragging a tangent line of the cubic curve respectively.

: If a junction point is dragged with holding down the Shift or Ctrl key, then the junction point will be moved horizontally or vertically.

(a)Original (b)Move the second junction point to upper left (c)Change the tangent direction at the start point

Figure 1.4 Moving a junction point and changing a tangent direction

(4) Processing senment points return=>page top

(a)Original (b)Adds points to the bottom edge(ARC) and the right side edge(LINE) (c)Deletes the upper right point (d)Deletes the left lower and right lower poins
(e)Smooths the curve at the upper left point (f)Makes all junction points into cusp points (g)Disconnects the curve at all junction points
(g)Ordinary display


1.2 Dialog return=>page top

Select the radio button representing the operation to be performed.


Moves a segment or junction point
>
Adds point on a edge

Deletes a junction point

Smooths the curve at a junction point

Makes a junction point into cusp point

Disconnect a curevs at a junction point

Figure 2. Dialog(for a polyline)



1.3 Control flow return=>page top

The ModifyShape class displays the dialog of Figure 2 and the ModifyShapeAction performs the action processing of the buttons in the dialog. The ModifyShapeLS class receives the mouse motion and calls the corresponding method of the ShapeElement subclass to update the shape data.

Step Description
Step1.Start ∙ Select "modify shape" command in the popup menu or the pulldown menu.

The ModifyShape.showDialog method will be called via the ExecCommand class of the command dispatcher.

∙ The ModifyShape.showDialog method creates a new object of the ModifyShapeLS and registers it to the ListenerPanel.
Step2. Displaying junction points ∙ The ModifyShape.showDialog method sets the MODIFYING_SHAPE_MODE to a selected shape (ShapeContainer).

The selected shape (ShapeContainer) calls the DrawShapeUtil.drawSegmentModifiers method to draw junction points and middle points of line segments.

: If the selected shape is a cubic curve, then the DrawShapeUtil.drawSegmentModifiers method draws tangent lines of the the selected shape.
Step3.Modifying the shape (1)Moving a junction point or a line segment

If the "move" button is selected in the dialog (Figure 2), then the following operation will be performed.
The methods of the ModifyShapeLS transfers the mouse motion to the corresponding ShapeElement. subclass as follows.
mousePressed -> ShapeElement.mouseStart method.
mouseDragged -> ShapeElement.modify method.
mouseReleased -> ShapeElement.mouseEnd method.

: After calling these methods, the mouseDragged method calls the ConnectionUtil.resizeConnectors method.

(2) Adding a new junction point, deleting a junction point

If the "add point" or "delete point" button is selected in the dialog (Figure 2), then the following operation will be performed.
The ModifyShapeLS.mouseClicked method transfers the mouse position to the addPoint or deletePoint method of the ShapeElement subclass.

: If the deletePoint method was called, then the mouseClicked method calls the ConnectionUtil.resizeConnectors method.
Step4.End

If the "Exit" button is clicked, then terminates the processing.



2. Class ModifyShape   return=>page top
public class ModifyShape extends JDialog

The ModifyShape class displays the dialog of Figure 2 and the ModifyShapeAction processes the action events of the buttons in the dialog. The ModifyShapeLS class receives the mouse motion and calls the corresponding method of the ShapeElement subclass to update the shape data.

Field Description

action

ModifyShapeAction action
Sets the ModifyShapeAction object to this field.

modifyShapeLS

ModifyShapeLS modifyShapeLS
Set the ModifyShapeLS object.

targerContainer ShapeContainer targerContainer
The ShapeContainer of the selected shape.

messageLabel1

JLabel messageLabel1
Sets the label object on which the showMessage methods displays a message.

messageLabel2

JLabel messageLabel2
Sets the label object on which the showMessage methods displays a message.

moveButton JRadioButton moveButton Set the "move" button object to this field. Registers the ModifyShapeAction object to the button object as the java.awt.event.ActionListner.
When this button is clicked, the ModifyShapeAction.actionPerformed method displays the message of "move button selected" and sets the operation mode of the MOVE_MODE to the ModifyShapeLS.
addPointButton JRadioButton addPointButton
Set the "add point" button object to this field. Registers the ModifyShapeAction object to the button object as a java.awt.event.ActionListner.
deletePointButton

JRadioButton deletePointButton
Set the "delete point" button object to this field. Registers the ModifyShapeAction object to the button object as a java.awt.event.ActionListner.

smoothButton JRadioButton smoothPButton
Set the "smooth" button object to this field. Registers the ModifyShapeAction object to the button object as a java.awt.event.ActionListner.
cuspButton JRadioButton cuspButton
Set the "cusp" button object to this field. Registers the ModifyShapeAction object to the button object as a java.awt.event.ActionListner.
disconnectButton JRadioButton disconnectButton
Set the "disconnect" button object to this field. Registers the ModifyShapeAction object to the button object as a java.awt.event.ActionListner.
exitButton

JButton exitButton
Set the "Exit" button object to this field. Registers the ModifyShapeAction object to the button object as the java.awt.event.ActionListner.
If this button is clicked, then terminates the ModifyShapeLS and closes the dialog.

dialogDimension

private Dimension dialogDimension=new Dimension(300,80)
The size of the dialog.

Method Description
showDialog public void showDialog()
∙ Checks the selected shape

Gets the selected shape by the ContainerManager.getSelectedContainers method and checks the selected shape is appropriate. If not appropriate, this method displays the error message by the JOptionPane.showMessageDialog.

∙ Calls the ModifyShapeLS.start method.
∙ Sets the display position of the dialog.

Shows the dialog at the left side of the selected shape.
∙ Adds the label for message output and the buttons to the dialog.

Adds the actionListener of the ModifyShapeAction to the buttons. If the button is clicked or selected, then the button's action will be processed by the ModifyShapeAction.actionPerformed method.

∙ Adds the WindowListener to the dialog.

Before the window is closed, the windowClosing method is called

showMessage protected void showMessage(int id, String message)
Two message labels are laid vertically in the dialog.
The 1 or 2 of the id specifies the messageLabel1 or the messageLabel2 respectively.
getTargetContainer protected ShapeContainer getTargetContainer()
Returns the targetContainer.
getModifyShapeLS protected ModifyShapeLS getModifyShapeLS()
Returns the modifyShapeLS.


3. Class ModifyShapeAction return=>page top
class ModifyShapeAction extends AbstractAction implements WindowListener

This class processes the action events of the buttons in the dialog and the window event.

Field Description
dialog <ModifyShape dialog
Sets the ModifyShape object to this field.

Method

Description

actionPerformed public void actionPerformed(ActionEvent e)
When the button is clicked in the dialog, executes the action processing of the button.
∙ The "move/add point/delete point" buttons

Sets the MOVE_MODE, ADD_POINT_MODE or DELETE_POINT_MODE
to the ModifyShapeLS by the setMode method of this class.
∙ The "Exit" button

Calls to the closeDialog method.

closeDialog private void closeDialog()
Closes the dialog. Calls the end method of the ModifyShapeLS.
windowClosing

public void windowClosing(WindowEvent e)
Calls the closeDialog method.



4. Class ModifyShapeLS return=>page top
public class ModifyShapeLS implements MouseListener, MouseMotionListener

This class processes the mouse events for moving a junction point, adding a new junction point and deleting a junction point. The mouse events are interpreted and transferred to the corresponding methods of the ShapeElement subclass to update the shape data.

:Special remarks
Item Description
Target curve topology The "ModifyShape" command changes the topology of a target curve by connecting it to another curve at its end point, or dividing it to multiple sub-curves using "Disconnect" sub-command.
To handle changing of the number of the target curve, the ShapeElement which stores the target curve must be a GeneralCurveElement and its parametric curve must be a GeneralCurve2DE.
For this reason, the ShapeElement of the target curve is converted to a GeneralCurveElement at the first step (start method of this class) of the "ModifyShape" command.
If multiple curves are generated at the end of the "ModifyShape" command, this class stores each of the multiple curves to a suitable ShapeElemnt at the last step (end method of this class).
undoSetup It is good to store an undo setting to UndoDrawManager whenever the target-curve's topology changes.
In order to do this, the undoSetupStart method is called at the start method of this class, and the undoSetupEnd is called at the setCommand.
Moreover, the undoSetupStart method is called at the setCommand method for the next sub-command processing.
Finally, the undoSetupEnd is called in the end method of this class and all changes of the target-curve's topology are recorded.

Field Description
shapeContainer ShapeContainer shapeContainer
Sets the shape object to be modified to this field.
connectionUtil ConnectionUtil connectionUtil
Sets the ConnectionUtil object which resizes or transforms the connectors connecting the shapeContainer.
mode int mode
The operation mode of this class.
The ModifyShapeAction.actionPerformed method sets the MOVE_MODE, ADD_POINT_MODE or DELETE_POINT_MODE to this field.
startPoint Point2D startPoint:The drag start point
endPoint Point2D endPoint:The drag current or end point.
oldPoint

Point2D oldPoint:The old drag point.
The endPoint is set to this field at the end of the mouseDragged method.

angledLine DiscreteAngledLine lineAngle: DiscreteAngledLine object.

Method Description
start public void start(ShapeContainer shapeContainer)
This method is called by the ModifyShape.showDialog method.
∙ Registers this object to the ListenerPanel as a MouseListener and a MouseMotionListener.

Removes the SelectionLS object from the ListenerPanel temporarily to avoid malfunction .

∙ Calls the ConnectionUtil.setTargets method.

The ConnectionUtil.setTargets method creates the list of the connectors which connect to the shapes to be modified.

∙ Calls the ContainerManager.undoSetupStart method for undo setup.
=> The undo support function of the ContainerManager
end public void end()
∙ Removes this object from the ListenerPanel and calls the SelectionLS.start method to restart the shape selection process.
∙Calls the ConnectionUtil.end method.
∙Calls the ContainerManager.undoSetupEnd method.
setCommand public void setCommand(int mode) Sets the operation mode to this class by command.
The ModifyShapeAction.actionPerformed sets the Command. ADD_POINT/ DELETE_POINT/ SMOOTH/ CUSP/ DISCONNECT to this class.
mousePressed public void mousePressed(MouseEvent e)
∙ Sets the mouse position to the startPoint.
∙ Calls the ShapeElement.mouseStart method.
mouseDragged public void mouseDragged(MouseEvent e)
If the mode equals MOVE_MODE, then performs the following processing.
∙ Set the current mouse position to the endPoint.
∙ Calls the modify method of the ShapeElement to move a junction point, a line segment or a tangent line.
∙ Calls the ConnectionUtil.resizeConnectors method to resize or transform the connectors.
mouseReleased public void mouseReleased(MouseEvent e) Sets null to the startPoint, endPoint and oldPoint.
mouseClicked public void mouseClicked(MouseEvent e)
If the mode equals ADD_POINT_MODE or DELETE_POINT_MODE, then performs the following processing.
∙ Calls the addPoint or deletePoint method of the ShapeElement.
∙ Calls the ConnectionUtil.resizeConnectors method to resize or transform the connectors.
mouseMoved public void mouseMoved(MouseEvent e)
⋅ Nothing is done.
mouseEntered public void mouseEntered(MouseEvent e)
⋅ Nothing is done.
mouseExited public void mouseExited(MouseEvent e)
⋅ Nothing is done.


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