Draw a Flowchart

This section illustrates you how to draw a Flowchart to compute the sum of first 50 natural numbers.

Draw a Flowchart

Draw a Flowchart

     

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

import java.util.*;
import org.eclipse.draw2d.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.draw2d.geometry.*;

public class FlowchartExample {
  public static void main(String args[]) {
  Shell shell = new Shell();
  shell.setSize(200450);
  shell.open();
  shell.setText("Flowchart Example");
  LightweightSystem lightWeight = new LightweightSystem(shell);
  Chart flowchart = new Chart();
  lightWeight.setContents(flowchart);

  TerminatorBox start = new TerminatorBox();
  start.setName("Start");
  start.setBounds(new Rectangle(402010020));

  ProcessBox process1 = new ProcessBox();
  process1.setName("SUM=0,NUM=0");
  process1.setBounds(new Rectangle(258013020));

  ProcessBox process2 = new ProcessBox();
  process2.setName("NUM=NUM+1");
  process2.setBounds(new Rectangle(4014010020));

  ProcessBox process3 = new ProcessBox();
  process3.setName("SUM=SUM+NUM");
  process3.setBounds(new Rectangle(2520013020));

  DecisionBox decision = new DecisionBox();
  decision.setName("Is NUM=50?");
  decision.setBounds(new Rectangle(3026012060));

  ProcessBox process4 = new ProcessBox();
  process4.setName("SUM");
  process4.setBounds(new Rectangle(4034010020));

  TerminatorBox stop = new TerminatorBox();
  stop.setName("End");
  stop.setBounds(new Rectangle(4040010020));

  Path path1 = new Path();
  path1.setSourceAnchor(start.outAnchor);
  path1.setTargetAnchor(process1.inAnchor);
  Path path2 = new Path();
  path2.setSourceAnchor(process1.outAnchor);
  path2.setTargetAnchor(process2.inAnchor);
  Path path3 = new Path();
  path3.setSourceAnchor(process2.outAnchor);
  path3.setTargetAnchor(process3.inAnchor);
  Path path4 = new Path();
  path4.setSourceAnchor(process3.outAnchor);
  path4.setTargetAnchor(decision.inAnchor);
  Path path5 = new Path();
  path5.setSourceAnchor(decision.yesAnchor);
  path5.setTargetAnchor(process4.inAnchor);
  Path path6 = new Path();
  path6.setSourceAnchor(decision.noAnchor);
  path6.setTargetAnchor(process2.inAnchor);
  Path path7 = new Path();
  path7.setSourceAnchor(process4.outAnchor);
  path7.setTargetAnchor(stop.inAnchor);

  flowchart.add(start);
  flowchart.add(process1);
  flowchart.add(process2);
  flowchart.add(process3);
  flowchart.add(decision);
  flowchart.add(process4);
  flowchart.add(stop);
  flowchart.add(path1);
  flowchart.add(path2);
  flowchart.add(path3);
  flowchart.add(path4);
  flowchart.add(path5);
  flowchart.add(path6);
  flowchart.add(path7);

  Display display = Display.getDefault();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  }
}
class Chart extends FreeformLayeredPane {
  public Chart() {
  setLayoutManager(new FreeformLayout());
  setBackgroundColor(ColorConstants.lightGray);
  setOpaque(true);
  }
}
class TerminatorBox extends ActivityFigure {
  FixedAnchor inAnchor, outAnchor;

  public TerminatorBox() {
  inAnchor = new FixedAnchor(this);
  inAnchor.point = new Point(10);
  targetAnchors.put("in_term", inAnchor);
  outAnchor = new FixedAnchor(this);
  outAnchor.point = new Point(12);
  sourceAnchors.put("out_term", outAnchor);
  }
public void paintFigure(Graphics g) {
  Rectangle r = bounds;
  g.drawArc
 (r.x + r.width / 
8, r.y, r.width / 4, r.height - 190180);
  g.drawLine
 (r.x + r.width / 
4, r.y, r.x + * r.width / 4, r.y);
  g.drawLine(r.x + r.width / 4, r.y + r.height - 1,
  r.x + * r.width / 4, r.y + r.height - 1);
  g.drawArc
 (r.x + 
* r.width / 8, r.y, r.width / 4, r.height - 1270,
  180);
  g.drawText
 (message, r.x + 
* r.width / 8, r.y + r.height / 8);
  }
}
class FixedAnchor extends AbstractConnectionAnchor {
  Point point;
  public FixedAnchor(IFigure owner) {
  super(owner);
  }
  public Point getLocation(Point loc) {
  Rectangle rect = getOwner().getBounds();
  int x = rect.x + point.x * rect.width / 2;
  int y = rect.y + point.y * rect.height / 2;
  Point p = new PrecisionPoint(x, y);
  getOwner().translateToAbsolute(p);
  return p;
  }
}
class DecisionBox extends ActivityFigure {
  FixedAnchor inAnchor, yesAnchor, noAnchor;
  public DecisionBox() {
  inAnchor = new FixedAnchor(this);
  inAnchor.point = new Point(10);
  targetAnchors.put("in_dec", inAnchor);
  noAnchor = new FixedAnchor(this);
  noAnchor.point = new Point(21);
  sourceAnchors.put("no", noAnchor);
  yesAnchor = new FixedAnchor(this);
  yesAnchor.point = new Point(12);
  sourceAnchors.put("yes", yesAnchor);
  }
public void paintFigure(Graphics g) {
  Rectangle rect = bounds;
  PointList pl = new PointList(4);
  pl.addPoint(rect.x + rect.width / 2, rect.y);
  pl.addPoint(rect.x, rect.y + rect.height / 2);
  pl.addPoint
 (rect.x + rect.width / 
2, rect.y + rect.height - 1);
  pl.addPoint
 (rect.x + rect.width, rect.y + rect.height / 
2);
  g.drawPolygon(pl);
  g.drawText(message, 
rect.x + rect.width / 
5, rect.y + * rect.height / 8);
  g.drawText("N"
  rect.x + 
* rect.width / 8, rect.y + * rect.height / 8);
  g.drawText("Y", rect.x + rect.width / 2, rect.y + 
  rect.height / 
4);
  }
}
class Path extends PolylineConnection {
  public Path() {
  setTargetDecoration(new PolylineDecoration());
  setConnectionRouter(new ManhattanConnectionRouter());
  }
}
class ProcessBox extends ActivityFigure {
  FixedAnchor inAnchor, outAnchor;
  public ProcessBox() {
  inAnchor = new FixedAnchor(this);
  inAnchor.point = new Point(10);
  targetAnchors.put("in_proc", inAnchor);
  outAnchor = new FixedAnchor(this);
  outAnchor.point = new Point(12);
  sourceAnchors.put("out_proc", outAnchor);
  }
  public void paintFigure(Graphics g) {
  Rectangle rect = bounds;
  g.drawText(message, 
  rect.x + rect.width / 
4, rect.y + rect.height / 4);
  g.drawRectangle
   (rect.x, rect.y, rect.width - 
1, rect.height - 1);
  }
}
abstract class ActivityFigure extends Figure {
  Rectangle rect = new Rectangle();
  Hashtable targetAnchors = new Hashtable();
  Hashtable sourceAnchors = new Hashtable();
  String message = new String();
  public void setName(String msg) {
  message = msg;
  repaint();
  }
}

Output will be displayed as:

Download Source Code