title: Drawing a Control Flow Graph
body: <p>My task is to make a control flow graph. First, I have managed to seperate my code into basic blocks. For example, this program here:</p>
  
  <pre><code>1 begin
  
  2   int x, y, power;
  
  3   float z;
  
  4   input (x, y);
  
  5   if (y&lt;0)
  
  6     power=-y;
  
  7   else
  
  8     power=y;
  
  9   z=1;
  
  10   while (power!=0){
  
  11     z=z*x;
  
  12   power=power-1;
  
  13   }
  
  14   if (y&lt;0)
  
  15     z=1/z;
  
  16   output(z);
  
  17 end
  </code></pre>
  
  <p>will turn into the following basic blocks:</p>
  
  <p>BLOCK 1</p>
  
  <pre><code>Line  2   int x, y, power;
  Line  3   float z;
  Line  4   input (x, y);
  Line  5   if (y&lt;0)
  </code></pre>
  
  <p>BLOCK 2</p>
  
  <pre><code>Line  6     power=-y;
  </code></pre>
  
  <p>BLOCK 3</p>
  
  <pre><code>Line  8     power=y;
  </code></pre>
  
  <p>BLOCK 4</p>
  
  <pre><code>Line  9   z=1;
  </code></pre>
  
  <p>BLOCK 5</p>
  
  <pre><code>Line 10   while (power!=0){
  </code></pre>
  
  <p>BLOCK 6</p>
  
  <pre><code>Line 11     z=z*x;
  Line 12   power=power-1;
  </code></pre>
  
  <p>BLOCK 7</p>
  
  <pre><code>Line 14   if (y&lt;0)
  </code></pre>
  
  <p>BLOCK 8</p>
  
  <pre><code>Line 15     z=1/z;
  </code></pre>
  
  <p>BLOCK 9</p>
  
  <pre><code>Line 16   output(z);
  </code></pre>
  
  <p>I have done this by scanning through the file, and using Pattern and Matcher to split the program up based on if, while, etc statements. The basic blocks are an ArrayList and all these blocks are kept in an ArrayList>. </p>
  
  <p>Next, I have kept a HashMap of the which basic blocks are connected to each other. For example, block 1 will be connected to block 2 and block 3 as it is an if statement (if this go one way, else go the other). This HashMap is of > for the block number, and the list of block numbers that it is connected to. </p>
  
  <p>So, I have the basic blocks, and a list of connections between these basic blocks. My problem now is that I am not sure how to display this in a graphical form. Before, I was using the paint() method and drawing circles and lines to represent a simple control graph where each block only had one connection to the next block. However, I do not know how to do this when there are multiple connections. Is there any easy way of doing this?</p>
  
  <p>Thanks!</p>