import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;

public class Visualization extends JComponent
{
 private int maxNodes;
 private int maxCells;
 private int xCoord;
 private int yCoord;
 private int width;
 private int height;
 private Vector storeConnection;
 private Dimension slideSize;
 private BufferedImage[] slide;
 private Thread internalThread;

 private volatile int numberOfProcess;
 private volatile boolean noStopRequested;
 private volatile boolean firstBuild;

 final static float dash[] = {10.0f};
 final static BasicStroke dashed = new BasicStroke(1.0f,
              BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);

 public Visualization(int maxNodes, int maxCells)
 {
  this.maxNodes = maxNodes;
  this.maxCells = maxCells;
  storeConnection = new Vector();
  System.out.println("Visualization........");
  System.out.println("maxNodes : " + maxNodes);
  System.out.println("maxCells : " + maxCells);
  noStopRequested = true;
  numberOfProcess = 0;
  slideSize = new Dimension(520,400);
  firstBuild = true;
 // buildBase();

  setMinimumSize(slideSize);
  setPreferredSize(slideSize);
  setMaximumSize(slideSize);
  setSize(slideSize);
/*
  Runnable running = new Runnable()
  {
   public void run()
   {
    System.out.println("run()....");
    try
    {
     while(noStopRequested)
     {
      try
      {
       Thread.sleep(1000); // sleep for 1000 milisecond = 1 second
       repaint();
      }
      catch(InterruptedException ex)
      {
       System.out.println("InterruptedException: " + ex);
      }
     }// end of while-loop
    }// end of try & catch
    catch(Exception e)
    {
     System.out.println("Exception: " + e);
    }
   }// end of run's method
  };

  internalThread = new Thread(running, "Visualization");
  internalThread.start();
*/
 }// end of Visualization's constructor

 public void buildBase()
 {
  RenderingHints renderHints = new RenderingHints(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
  renderHints.put(RenderingHints.KEY_RENDERING,
              RenderingHints.VALUE_RENDER_QUALITY);
  slide = new BufferedImage[maxNodes];
  System.out.println("buildBase()...."); 
  for(int i=0; i<maxNodes; i++)
  {
   slide[i] = new BufferedImage(slideSize.width,
                        slideSize.height,
                        BufferedImage.TYPE_INT_RGB);
   Graphics2D g2d = slide[i].createGraphics();
   g2d.setRenderingHints(renderHints);
   drawingNodes(g2d,maxNodes);
   drawingCells(g2d,maxCells);
  }
  
 }

 public void drawingNodes(Graphics2D g2, int numberOfNode)
 {
  System.out.println("drawingNodes..: " + maxNodes);
  int xPost = 10;
  int yPost = 10;
  width = 20;
  height= 20;
  g2.setColor(Color.red);
  for(int i=0; i< numberOfNode; i++)
  {
   if(xPost+width > 500)
   {
    yPost+=40;
    xPost =10;
   }
   g2.fill(new Rectangle(xPost,yPost,width,height));
   xPost += 40;
  }
 }

 public void drawingCells(Graphics2D g2, int numberOfCell)
 {
  System.out.println("drawingCells..: " + maxCells);
  int xPost = 10;
  int yPost = 70;
  width = 20;
  height= 20;
  g2.setColor(Color.blue);
  for(int i=0; i< numberOfCell; i++)
  {
   if(xPost+width > 500)
   {
    yPost+=40;
    xPost =10;
   }
   g2.fill(new Rectangle(xPost,yPost,width,height));
   xPost += 40;
  }
 }

 public void drawingLines(Graphics2D g2)
 {
  System.out.println("drawingLines..");
  int storeSize = storeConnection.size();
  g2.setStroke(dashed);
  int midPoint = 10;
  int xDifferent= 20;
  int yDifferent= 40;
  int xPost = 10;
  int xStartWidth= 40;
  int yStartPost1= 20;
  int yStartPost2= 70;
  int xPost1=0, xPost2=0, yPost1=xPost+midPoint,
      yPost2=midPoint+yStartPost2;
  int node, cell;
  MyComponent comp;
  for(int i=0; i<storeSize; i++)
  {
   comp = (MyComponent)storeConnection.elementAt(i);
   if((comp.getTypes() != 0) && (comp.getTypes() !=4))
   {
    switch(comp.getTypes())
    {
     case 1 : g2.setColor(Color.cyan); break; //subscribe
     case 2 : g2.setColor(Color.orange); break;//publish
     case 3 : g2.setColor(Color.yellow); break;//pub/sub
     default: break;
    }
    node = comp.getNodes();
    cell = comp.getCells();
    System.out.println("===========");
    System.out.println("cell: " + cell);
    if(cell > 11)
    {
     cell = (cell - 10) - 1;
     yPost2 = yStartPost2 + yDifferent;
    }
    xPost1 = xDifferent+(node*xDifferent);
    xPost2 = xDifferent+(cell*xStartWidth);
    System.out.println("node: " +node+ " | xPost1: " + xPost1);
    System.out.println("node: " +node+ " | yPost1: " + yPost1);
    System.out.println("cell: " +cell+ " | xPost2: " + xPost2);
    System.out.println("cell: " +cell+ " | yPost2: " + yPost2);
    g2.drawLine(xPost1,yPost1, xPost2, yPost2);
   }
  }
 }

 public void paintComponent(Graphics g)
 {
  Graphics2D g2 = (Graphics2D)g;
  System.out.println("paintComponent..");
  g2.setBackground(Color.white);
  if(firstBuild)
  {
   System.out.println("firstBuild..");
   drawingNodes(g2,maxNodes);
   drawingCells(g2,maxCells);
   firstBuild = false;
  }
  else
  {
   drawingNodes(g2,maxNodes);
   drawingCells(g2,maxCells);
   drawingLines(g2);
  }
 }

 public boolean updateVisual(int[] input)
 {
  if(input.length>3)
  {
   System.out.println("Input array is exceeding 3");
   return false;
  } 
  storeConnection.addElement(new MyComponent(input)); 
  repaint();
  return true;
 }

 public boolean updateVisual(int[][] input)
 {
  return true;
 }

 private class MyComponent
 {
  private int nodes;
  private int cells;
  private int types;
  private final static int maxComponent = 3;
  public MyComponent()
  {
   nodes = 0;
   cells = 0;
   types = 0;
  }

  public MyComponent(int[] input)
  {
   if(input.length > maxComponent)
   {
     System.out.println("Illegal length of input");
     System.exit(0);
   }
   for(int i=0; i<3; i++)
   {
    if(i==0) nodes = input[i];
    if(i==1) cells = input[i];
    if(i==2) types = input[i];
   }
  }
  public int getNodes()
  {
   return nodes;
  }
  public int getCells()
  {
   return cells;
  }
  public int getTypes()
  {
   return types;
  }
 }
}


 

1