Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with a visual Graph: Drawing a line/edge between 2 nodes

807603Jan 13 2008 — edited Jan 14 2008
I'm creating a push down automata. For a while now i have been stuck because
i cant figure out how to pass node locations to my edge correctly.
Here is what I have, i know its inefficient but i just need to have it working soon.

I know there has got to be a better way than what im doing, but i just cant think how.

I just need the coordinates of 2 nodes and to be able to draw a line between them
Thank You for reading.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pda2;

import java.awt.Graphics;
import java.util.*;

/**
 *
 * @author Admin
 */
public class Node {
    
     Vector edges = new Vector();
     int x, y, tempX, tempY, a, b;
     char nLabel;
     public int c;
     boolean wasVisited;
     Edge edge;
   
     
     public Node()
     {
         
     }
    
    public Node(int x, int y, char label)
    {
        edge = new Edge();
        c = 0;
        c++;
      
       nLabel = label;
       
       this.x = x;
       this.y = y;
       
       getLoc();
       
       
       
       wasVisited=false;
       
    }
    
    
     

    public void getLoc()
    {
        if(c == 1 || c == 3 || c == 5 || c==9)
        {
            
            tempX = x;
            tempY = y;
            edge.setCoords(tempX, tempY);
            
        }
        else if(c==2 || c==4 || c==6 || c==8 || c==10)
        {
            a = x;
            b = y;
            edge.setCoords1(a, b);
                    
           
    }
    }
    
    public void draw(Graphics g)
    {
        g.drawOval(x, y, 25, 25);
    }
    
    
    
    
    }







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

package pda2;

import java.awt.Graphics;

/**
 *
 * @author Admin
 */
public class Edge {
    char label;
    Node nodeOne;
    Node nodeTwo;
    int x,y,a,b;
    Node node;
    
    
    
    public Edge(char lab, Node one, Node two)
    {
        node = new Node();
        label = lab;
        nodeOne = one;
        nodeTwo = two;
        x = node.tempX;
        y = node.tempY;
        a = node.a;
        b = node.b;
       
        
        
        
    }
    public Edge()
    {
        
    }
    public void setCoords(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    
     public void setCoords1(int x, int y)
    {
        a = x;
        b = y;
    }
    public void draw(Graphics g)
    {
        g.drawLine(x, y, a, b);
    }
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 11 2008
Added on Jan 13 2008
1 comment
289 views