DARK_BYTE 0 Junior Poster in Training

Hi I need to animate user defined algorithms and I have a myGraph class. The dfs() and bfs() will go into the user-defined algorithm the user types and saves in a text-editor included with my program.
I am checking the adjacency matrix to know if two vertex are connected by an edge. The problem I am having is that vertices are intersecting each other. How can I solve this problem; having a limit for the number of elements/vertex is acceptable.
Thanks in advance!

graphNode class:

public class graphNode
{
	public String label;
	public boolean visited=false;
	public graphNode(String l)
	{
		this.label=l;
	}
}

myGraph class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */

import java.awt.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import javax.swing.*;


public class myGraph extends JComponent

{
	public graphNode rootNode;
	public ArrayList graphNodes=new ArrayList();
	public int[][] adjMatrix;//Edges will be represented as adjacency Matrix
        public int[] posX;
        public int[] posY;
	int size;
        Point p1;
        Point p2;
        double dy;
        double dx;
        double dia = 25.0;
        double barb = 15.0;
        double theta;
        double phi = Math.toRadians(20);
        double x1,y1,x,y,x2,y2;


	public void setrootNode(graphNode n)
	{
		this.rootNode=n;
	}

	public graphNode getrootNode()
	{
		return this.rootNode;
	}

	public void addgraphNode(graphNode n)
	{
		graphNodes.add(n);
                this.repaint();
	}

	//This method will be called to make connect two graphNodes
	public void connectgraphNode(graphNode start,graphNode end)
	{
		if(adjMatrix==null)
		{
			size=graphNodes.size();
			adjMatrix=new int[size][size];
                        posX = new int[size];
                        posY = new int[size];
		}

		int startIndex=graphNodes.indexOf(start);
		int endIndex=graphNodes.indexOf(end);
		adjMatrix[startIndex][endIndex]=1;
		adjMatrix[endIndex][startIndex]=1;
                this.repaint();
	}

	private graphNode getUnvisitedChildgraphNode(graphNode n)
	{

		int index=graphNodes.indexOf(n);
		int j=0;
		while(j<size)
		{
			if(adjMatrix[index][j]==1 && ((graphNode)graphNodes.get(j)).visited==false)
			{
				return (graphNode)graphNodes.get(j);
			}
			j++;
		}
		return null;
	}

	//BFS traversal of a tree is performed by the bfs() function
	public void bfs()
	{

		//BFS uses Queue data structure
		Queue q=new LinkedList();
		q.add(this.rootNode);
		printgraphNode(this.rootNode);
		rootNode.visited=true;
		while(!q.isEmpty())
		{
			graphNode n=(graphNode)q.remove();
			graphNode child=null;
			while((child=getUnvisitedChildgraphNode(n))!=null)
			{
				child.visited=true;
				printgraphNode(child);
				q.add(child);
			}
		}
		//Clear visited property of graphNodes
		cleargraphNodes();
	}

	//DFS traversal of a tree is performed by the dfs() function
	public void dfs()
	{
		//DFS uses Stack data structure
		Stack s=new Stack();
		s.push(this.rootNode);
		rootNode.visited=true;
		printgraphNode(rootNode);
		while(!s.isEmpty())
		{
			graphNode n=(graphNode)s.peek();
			graphNode child=getUnvisitedChildgraphNode(n);
			if(child!=null)
			{
				child.visited=true;
				printgraphNode(child);
				s.push(child);
			}
			else
			{
				s.pop();
			}
		}
		//Clear visited property of graphNodes
		cleargraphNodes();
	}


	//Utility methods for clearing visited property of graphNode
	private void cleargraphNodes()
	{
		int i=0;
		while(i<size)
		{
			graphNode n=(graphNode)graphNodes.get(i);
			n.visited=false;
			i++;
		}
	}

	//Utility methods for printing the graphNode's label
	private void printgraphNode(graphNode n)
	{
		System.out.print(n.label+" ");
	}



        public void paintComponent(Graphics g)
	{
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            Font font = new Font("Dialog", Font.BOLD, 12);
            g2d.setFont(font);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            //only draw if root is not equal to null
            //because if it is there exist no element/node
            graphNode myNode = null;
            int i =0;
            int count = 0;
            int j =1;
            size = graphNodes.size();
            while (count<size)
            {
                int hold = count;
                i=0;
               
                while((count<hold+4)&&(count<size))
                {
                    myNode = (graphNode) graphNodes.get(count);
                    g2d.drawString(myNode.label, i*60+40, j*60);
                    posX[count]=(i*60+40);
                    posY[count]=(j*60);
                    count++;
                    i++;
                 }
             
                j++;
                              
            }
            for(int k=0; k<size; k++)
            {
                for(int l=0; l<size; l++)
                {
                    if(adjMatrix[k][l]==1)
                    {
                        if(posY[k]==posY[l])
                        {
                            posY[l]-=25;
                        }

                        g2d.setColor(Color.BLACK);
                        p1 = new Point(posX[k],posY[k]);
                        p2 = new Point(posX[l],posY[l]);
                        dy = p2.y - p1.y; //vertical distance
                        dx = p2.x - p1.x; //horizontal distance
                        //arc tangent
                        theta = Math.atan2(dy, dx);
                        x1 = p1.x + (dia/2)*Math.cos(theta);
                        y1 = p1.y + (dia/2)*Math.sin(theta);
                        theta += Math.PI;
                        x2 = p2.x + (dia/2)*Math.cos(theta);
                        y2 = p2.y + (dia/2)*Math.sin(theta);
                        g2d.draw(new Line2D.Double(x1, y1, x2, y2));
                        /*x = x2 + barb*Math.cos(theta+phi);
                        y = y2 + barb*Math.sin(theta+phi);
                        g2d.draw(new Line2D.Double(x2, y2, x, y));
                        x = x2 + barb*Math.cos(theta-phi);
                        y = y2 + barb*Math.sin(theta-phi);
                        g2d.draw(new Line2D.Double(x2, y2, x, y));*/
                    }
                }
            }
            


        }

}

main:

import javax.swing.*;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
		//Lets create graphNodes as given as an example in the article
		graphNode nA=new graphNode("A");
		graphNode nB=new graphNode("B");
		graphNode nC=new graphNode("C");
		graphNode nD=new graphNode("D");
		graphNode nE=new graphNode("E");
		graphNode nF=new graphNode("F");
              		
		//Create the graph, add graphNodes, create edges between graphNodes
		myGraph g=new myGraph();
		g.addgraphNode(nA);
		g.addgraphNode(nB);
		g.addgraphNode(nC);
		g.addgraphNode(nD);
		g.addgraphNode(nE);
		g.addgraphNode(nF);
                g.setrootNode(nA);
             
		
		g.connectgraphNode(nA,nB);
		g.connectgraphNode(nA,nC);
		g.connectgraphNode(nA,nD);
		
		g.connectgraphNode(nB,nE);
		g.connectgraphNode(nB,nF);
		g.connectgraphNode(nC,nF);
		
		
		

            JFrame frame = new JFrame("Rectangles");
	    frame.add(g);
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setSize(360, 300);
	    frame.setLocationRelativeTo(null);
	    frame.setVisible(true);
		
		
		
		
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.