This section illustrates you how to draw a Flowchart to compute the sum of first 50 natural numbers.
To draw a flowchart, we have used the package org.eclipse.draw2d.* and org.eclipse.swt.*. The Shell class provides the shell on which the flowchart is displayed. The shell is just like a frame that is used in Swing.
The class LightweightSystem display the figures on the SWT canvas. To draw the terminator box, process box decision box in flowchart, we have create the classes TerminatorBox, Processbox and DecisionBox respectively and set the size using Rectangle class.
To connect the boxes, we draw the lines by creating a class Path which is extended from the class PolylineConnection. The class PolylineConnection determine the connections points and provides the methods setTargetDecoration() and setConnectionRouter(). The method setTargetDecoration() sets the decoration of the target point by creating a right-pointing triangle and the method setConnectionRouter() handles the layout of polyline by providing a orthogonal route between the source and target anchors(or points). The class Path is created to set the path from source anchor to target anchor
The class AbstractConnectionAnchor give support to anchors for their location on the figure. For this, we have create a class FixedAnchor. The inAnchor and outAnchor are the objects of this class. The inAnchor shows the target point and the outAnchor shows the source point.
Following code draws the Y and N on the decision box by the following code:
|g.drawText("N", rect.x + 7 * rect.width / 8, rect.y + 3 * rect.height / 8);
g.drawText("Y", rect.x + rect.width / 2 - 2, rect.y + 3 * rect.height / 4);
Here is the code of FlowchartExample.java
Output will be displayed as:
Liked it! Share this Tutorial