Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[Share] Graph PP filter_list
Author
Message
[Share] Graph PP #1
Few days ago I thought of making a project related to graphs and came up with this so far.. It is still incomplete, so you might find few portions in the code that remain unused. I would like to share the source code here and would love to hear some feedback and suggestions for it too.

Node.java
Code:
package graphpp;

import java.util.ArrayList;

public class Node {
    
    private String name;
    private ArrayList<Edge> destList;
    private ArrayList<Edge> srcList;
    private ArrayList<Edge> edgeList;
    private int dest,src;
    
    
    private boolean emphasized;
    private int x,y;
    
    public Node() {
        destList=new ArrayList<Edge>();
        srcList=new ArrayList<Edge>();
        edgeList=new ArrayList<Edge>();
        dest=-1;
        src=-1;
    }
    
    public void setName(String name) {
        this.name=name;
    }
    public String getName() {
        return name;
    }
    
    public void setEmphasize(boolean emphasize) {
        emphasized=emphasize;
    }
    public boolean isEmphasized() {
        return emphasized;
    }
    
    public void setXPos(int x) {
        this.x=x;
    }
    public int getXPos() {
        return x;
    }
    public void setYPos(int y) {
        this.y=y;
    }
    public int getYPos() {
        return y;
    }
    
    public void addDestEdge(Edge edge) {
        destList.add(edge);
        edgeList.add(edge);
    }
    public Edge getNextDestEdge() {
        dest=(dest+1)%destList.size();
        return destList.get(dest);
    }
    public int getDestSize() {
        return destList.size();
    }
    public ArrayList<Edge> getDestList() {
        return (new ArrayList<Edge>(destList));
    }
    
    public void addSrcEdge(Edge edge) {
        srcList.add(edge);
        edgeList.add(edge);
    }
    public Edge getNextSrcEdge() {
        src=(src+1)%srcList.size();
        return destList.get(src);
    }
    public int getSrcSize() {
        return srcList.size();
    }
    public ArrayList<Edge> getSrcList() {
        return (new ArrayList<Edge>(srcList));
    }
    
    public ArrayList<Edge> getAllEdgesList() {
        return (new ArrayList<Edge>(edgeList));
    }
    public int getDegree() {
        return edgeList.size();
    }
}

Edge.java
Code:
package graphpp;

public class Edge {
    
    private Node source,destination;
    private int weight;
    private boolean emphasize;
    
    public void setSource(Node source) {
        this.source=source;
    }
    public Node getSource() {
        return source;
    }
    
    public void setDestination(Node destination) {
        this.destination=destination;
    }
    public Node getDestination() {
        return destination;
    }
    
    public void setWeight(int weight) {
        this.weight=weight;
    }
    public int getWeight() {
        return weight;
    }
    
    public void setEmphasize(boolean emphasize) {
        this.emphasize=emphasize;
    }
    public boolean isEmphasized() {
        return emphasize;
    }
}

Graph.java
Code:
package graphpp;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Graph extends JPanel implements ActionListener{

    
    private JFrame frame;
    private JToolBar toolbar;
    private JButton newButton;
    private JToggleButton joinButton;
    private JCheckBox directedCheckBox;
    
    private Color backColor,foreColor,foreColor2,selectedColor,lineColor;
    private int borderWidth=2,diameter=40;
    
    private int width,height;
    
    private  ArrayList<Node> nodes;
    private ArrayList<Edge> edges;
    private Node pressedNode;
    private boolean pressed,edgeMode,directedGraph;
    private int xOffset,yOffset;
    private Timer animeTimer;
    private int fontSize=20;
    private Node selectedNode;
    
    public Graph() {
        frame=new JFrame("Graph PP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        width=Toolkit.getDefaultToolkit().getScreenSize().width;
        height=Toolkit.getDefaultToolkit().getScreenSize().height;
        
        frame.setSize(width, height);
        
        backColor=new Color(0x0099cc);
        foreColor2=new Color(0x00ee76);
        foreColor=new Color(0x1464f4);
        selectedColor=new Color(0xadadff);
        lineColor=new Color(0xcd661d);
        
        nodes=new ArrayList<Node>();
        edges=new ArrayList<Edge>();
        pressed=false;
        directedGraph=true;
        pressedNode=null;
        xOffset=0;
        yOffset=0;
        animeTimer=new Timer(3,this);
        selectedNode=null;
        
        newButton=new JButton("New Node");
        newButton.addActionListener(this);
        joinButton=new JToggleButton("Join Nodes");
        joinButton.addActionListener(this);
        joinButton.setEnabled(false);
        directedCheckBox=new JCheckBox("Directed Graph",true);
        directedCheckBox.addActionListener(this);
        
        toolbar=new JToolBar();
        toolbar.setFloatable(false);
        
        toolbar.add(newButton);
        toolbar.add(joinButton);
        toolbar.add(directedCheckBox);
        
        frame.add(toolbar,BorderLayout.NORTH);
        
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int x=e.getX();
                int y=e.getY();
                for(int i=0;i<nodes.size();i++) {
                    Node temp=nodes.get(i);
                    //if the mouse was pressed on a node
                    if(x>=temp.getXPos() && x<=temp.getXPos()+diameter
                            && y>=temp.getYPos() && y<=temp.getYPos()+diameter) {
                        //if user has enables join edge toggle button to draw new edges
                        if(edgeMode) {
                            if(selectedNode==null) {
                                selectedNode=temp;
                                repaint();
                            }
                            else {
                                makeNewEdge(selectedNode,temp);
                                repaint();
                            }
                            return;
                        }
                        updatePressedNode(temp,x,y);
                        return;
                    }
                }
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                if(pressed) {
                    pressed=false;
                    pressedNode=null;
                    xOffset=0;
                    yOffset=0;
                    removeMouseMotionListener(this);
                    animeTimer.stop();
                }
            }
        });
        
        frame.add(this);
        frame.setVisible(true);
    }
    
    private void updatePressedNode(Node a,int x,int y) {
        pressedNode=a;
        pressed=true;
        xOffset=x-a.getXPos();
        yOffset=y-a.getYPos();
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if(pressed) {
                    pressedNode.setXPos(e.getX()-xOffset);
                    pressedNode.setYPos(e.getY()-yOffset);
                }
            }
        });
        animeTimer.start();
    }
    
    private void drawEdges(Graphics2D g2d) {
        for(int i=0;i<nodes.size();i++) {
            Node temp=nodes.get(i);
            Node temp2=null;
            g2d.setColor(lineColor);
            for(int j=0;j<temp.getDestSize();j++) {
                Edge ed=temp.getNextDestEdge();
                temp2=ed.getDestination();
                if(temp.getXPos()==temp2.getXPos() && temp.getYPos()==temp2.getYPos())
                    g2d.drawOval(temp.getXPos()+diameter/2, temp.getYPos()+diameter/2, diameter, diameter);
                else
                    g2d.drawLine(temp.getXPos()+diameter/2, temp.getYPos()+diameter/2,
                temp2.getXPos()+diameter/2, temp2.getYPos()+diameter/2);
                int x=(temp.getXPos()+temp2.getXPos())/2+diameter/2;
                int y=(temp.getYPos()+temp2.getYPos())/2+diameter/2;
                g2d.drawString(String.valueOf(ed.getWeight()), x,y); //draw the weight of edge
                
                //draw the arrows on edges if the graph is directed
                if(directedGraph) {
                    drawArrow(g2d,x,y,temp,temp2);
                }
            }
        }
    }
    
    private void drawArrow(Graphics2D g2d,int x,int y,Node a,Node b) {
        double m=0;
        if(a.getXPos()-b.getXPos()!=0) {
            m=((double)(b.getYPos()-a.getYPos())/(b.getXPos()-a.getXPos()));
        }
        else {
            m=(b.getYPos()-a.getYPos())/0.01;
        }
        if(b.getXPos()>a.getXPos()) {
            g2d.drawLine(x,y,(int)(x-(diameter/3)*Math.cos(Math.atan(m)+Math.toRadians(30))),(int)(y-(diameter/3)*Math.sin(Math.atan(m)+Math.toRadians(30))));
            g2d.drawLine(x,y,(int)(x-(diameter/3)*Math.cos(Math.atan(m)-Math.toRadians(30))),(int)(y-(diameter/3)*Math.sin(Math.atan(m)-Math.toRadians(30))));
        }
        else {
            g2d.drawLine(x,y,(int)(x+(diameter/3)*Math.cos(Math.atan(m)-Math.toRadians(30))),(int)(y+(diameter/3)*Math.sin(Math.atan(m)-Math.toRadians(30))));
            g2d.drawLine(x,y,(int)(x+(diameter/3)*Math.cos(Math.atan(m)+Math.toRadians(30))),(int)(y+(diameter/3)*Math.sin(Math.atan(m)+Math.toRadians(30))));
        }
    }
    
    private void drawNodes(Graphics2D g2d) {
        for(int i=0;i<nodes.size();i++) {
            Node temp=nodes.get(i);
            g2d.setColor(foreColor);
            g2d.fillOval(temp.getXPos(), temp.getYPos(), diameter, diameter);
            if(temp!=selectedNode) {
                g2d.setColor(foreColor2);
            }
            else {
                g2d.setColor(selectedColor);
            }
            g2d.fillOval(temp.getXPos()+borderWidth, temp.getYPos()+borderWidth, diameter-2*borderWidth, diameter-2*borderWidth);
            g2d.setColor(foreColor);
            g2d.drawString(temp.getName(), temp.getXPos()+(diameter/2)-(temp.getName().length()*fontSize/3), temp.getYPos()+(diameter/2)+(fontSize/3));
        }
    }
    
    private void makeNewEdge(Node a,Node b) {
        Edge ed=new Edge();
        ed.setSource(a);
        String s=JOptionPane.showInputDialog(this, "Weight","");
        if(s!=null) {
            int w=Integer.parseInt(s);
            ed.setWeight(w);
            ed.setDestination(b);
            b.addSrcEdge(ed);
            a.addDestEdge(ed);
            selectedNode=null;
            edges.add(ed);
        }
    }
    
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d=(Graphics2D)g;
        g2d.setStroke(new BasicStroke(2));
        g2d.setColor(backColor);
        g2d.setFont(new Font(Font.MONOSPACED,Font.BOLD,fontSize));
        g2d.fillRect(0, 0, getWidth(), getHeight());
        drawEdges(g2d);
        drawNodes(g2d);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object event=e.getSource();
        if(event==newButton) {
            String name=JOptionPane.showInputDialog(frame,"A Short Name for the Nodes separated by Single Space", "");
            if(name!=null) {
                String names[]=name.split(" ");
                for(int q=0;q<names.length;q++) {
                    Node temp=new Node();
                    int xP=0,yP=0;
                    for(int i=0;i<nodes.size();i++) {
                        Node t=nodes.get(i);
                        if(t.getXPos()>=xP && t.getXPos()<=xP+diameter
                                && t.getYPos()>=yP && t.getYPos()<=yP+diameter) {
                            xP+=diameter+1;
                            if(xP+diameter>width) {
                                xP=0;
                                yP=yP+diameter+1;
                            }
                            i=0;
                        }
                    }
                    temp.setXPos(xP);
                    temp.setYPos(yP);
                    temp.setName(names[q]);
                    nodes.add(temp);
                    joinButton.setEnabled(true);
                }
                repaint();
            }
        }
        else if(event==joinButton) {
            edgeMode=!edgeMode;
            if(!edgeMode) {
                selectedNode=null;
                repaint();
            }
        }
        else if(event==animeTimer) {
            repaint();
        }
        else if(e.getSource()==directedCheckBox) {
            directedGraph=directedCheckBox.isSelected();
            repaint();
        }
    }
    
    public int getDegree(Node n) {
        return n.getDestSize()+n.getSrcSize();
    }
    
    
    
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Graph();
        }
        });
    }
}

Screenshots

[Image: up1.png]

[Image: up2.png]

[Image: up3.png]

[Image: up4.png]
Folow me on My YouTube Channel if you're into art.

Reply

[Share] Graph PP #2
Few days ago I thought of making a project related to graphs and came up with this so far.. It is still incomplete, so you might find few portions in the code that remain unused. I would like to share the source code here and would love to hear some feedback and suggestions for it too.

Node.java
Code:
package graphpp;

import java.util.ArrayList;

public class Node {
    
    private String name;
    private ArrayList<Edge> destList;
    private ArrayList<Edge> srcList;
    private ArrayList<Edge> edgeList;
    private int dest,src;
    
    
    private boolean emphasized;
    private int x,y;
    
    public Node() {
        destList=new ArrayList<Edge>();
        srcList=new ArrayList<Edge>();
        edgeList=new ArrayList<Edge>();
        dest=-1;
        src=-1;
    }
    
    public void setName(String name) {
        this.name=name;
    }
    public String getName() {
        return name;
    }
    
    public void setEmphasize(boolean emphasize) {
        emphasized=emphasize;
    }
    public boolean isEmphasized() {
        return emphasized;
    }
    
    public void setXPos(int x) {
        this.x=x;
    }
    public int getXPos() {
        return x;
    }
    public void setYPos(int y) {
        this.y=y;
    }
    public int getYPos() {
        return y;
    }
    
    public void addDestEdge(Edge edge) {
        destList.add(edge);
        edgeList.add(edge);
    }
    public Edge getNextDestEdge() {
        dest=(dest+1)%destList.size();
        return destList.get(dest);
    }
    public int getDestSize() {
        return destList.size();
    }
    public ArrayList<Edge> getDestList() {
        return (new ArrayList<Edge>(destList));
    }
    
    public void addSrcEdge(Edge edge) {
        srcList.add(edge);
        edgeList.add(edge);
    }
    public Edge getNextSrcEdge() {
        src=(src+1)%srcList.size();
        return destList.get(src);
    }
    public int getSrcSize() {
        return srcList.size();
    }
    public ArrayList<Edge> getSrcList() {
        return (new ArrayList<Edge>(srcList));
    }
    
    public ArrayList<Edge> getAllEdgesList() {
        return (new ArrayList<Edge>(edgeList));
    }
    public int getDegree() {
        return edgeList.size();
    }
}

Edge.java
Code:
package graphpp;

public class Edge {
    
    private Node source,destination;
    private int weight;
    private boolean emphasize;
    
    public void setSource(Node source) {
        this.source=source;
    }
    public Node getSource() {
        return source;
    }
    
    public void setDestination(Node destination) {
        this.destination=destination;
    }
    public Node getDestination() {
        return destination;
    }
    
    public void setWeight(int weight) {
        this.weight=weight;
    }
    public int getWeight() {
        return weight;
    }
    
    public void setEmphasize(boolean emphasize) {
        this.emphasize=emphasize;
    }
    public boolean isEmphasized() {
        return emphasize;
    }
}

Graph.java
Code:
package graphpp;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Graph extends JPanel implements ActionListener{

    
    private JFrame frame;
    private JToolBar toolbar;
    private JButton newButton;
    private JToggleButton joinButton;
    private JCheckBox directedCheckBox;
    
    private Color backColor,foreColor,foreColor2,selectedColor,lineColor;
    private int borderWidth=2,diameter=40;
    
    private int width,height;
    
    private  ArrayList<Node> nodes;
    private ArrayList<Edge> edges;
    private Node pressedNode;
    private boolean pressed,edgeMode,directedGraph;
    private int xOffset,yOffset;
    private Timer animeTimer;
    private int fontSize=20;
    private Node selectedNode;
    
    public Graph() {
        frame=new JFrame("Graph PP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        width=Toolkit.getDefaultToolkit().getScreenSize().width;
        height=Toolkit.getDefaultToolkit().getScreenSize().height;
        
        frame.setSize(width, height);
        
        backColor=new Color(0x0099cc);
        foreColor2=new Color(0x00ee76);
        foreColor=new Color(0x1464f4);
        selectedColor=new Color(0xadadff);
        lineColor=new Color(0xcd661d);
        
        nodes=new ArrayList<Node>();
        edges=new ArrayList<Edge>();
        pressed=false;
        directedGraph=true;
        pressedNode=null;
        xOffset=0;
        yOffset=0;
        animeTimer=new Timer(3,this);
        selectedNode=null;
        
        newButton=new JButton("New Node");
        newButton.addActionListener(this);
        joinButton=new JToggleButton("Join Nodes");
        joinButton.addActionListener(this);
        joinButton.setEnabled(false);
        directedCheckBox=new JCheckBox("Directed Graph",true);
        directedCheckBox.addActionListener(this);
        
        toolbar=new JToolBar();
        toolbar.setFloatable(false);
        
        toolbar.add(newButton);
        toolbar.add(joinButton);
        toolbar.add(directedCheckBox);
        
        frame.add(toolbar,BorderLayout.NORTH);
        
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int x=e.getX();
                int y=e.getY();
                for(int i=0;i<nodes.size();i++) {
                    Node temp=nodes.get(i);
                    //if the mouse was pressed on a node
                    if(x>=temp.getXPos() && x<=temp.getXPos()+diameter
                            && y>=temp.getYPos() && y<=temp.getYPos()+diameter) {
                        //if user has enables join edge toggle button to draw new edges
                        if(edgeMode) {
                            if(selectedNode==null) {
                                selectedNode=temp;
                                repaint();
                            }
                            else {
                                makeNewEdge(selectedNode,temp);
                                repaint();
                            }
                            return;
                        }
                        updatePressedNode(temp,x,y);
                        return;
                    }
                }
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                if(pressed) {
                    pressed=false;
                    pressedNode=null;
                    xOffset=0;
                    yOffset=0;
                    removeMouseMotionListener(this);
                    animeTimer.stop();
                }
            }
        });
        
        frame.add(this);
        frame.setVisible(true);
    }
    
    private void updatePressedNode(Node a,int x,int y) {
        pressedNode=a;
        pressed=true;
        xOffset=x-a.getXPos();
        yOffset=y-a.getYPos();
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if(pressed) {
                    pressedNode.setXPos(e.getX()-xOffset);
                    pressedNode.setYPos(e.getY()-yOffset);
                }
            }
        });
        animeTimer.start();
    }
    
    private void drawEdges(Graphics2D g2d) {
        for(int i=0;i<nodes.size();i++) {
            Node temp=nodes.get(i);
            Node temp2=null;
            g2d.setColor(lineColor);
            for(int j=0;j<temp.getDestSize();j++) {
                Edge ed=temp.getNextDestEdge();
                temp2=ed.getDestination();
                if(temp.getXPos()==temp2.getXPos() && temp.getYPos()==temp2.getYPos())
                    g2d.drawOval(temp.getXPos()+diameter/2, temp.getYPos()+diameter/2, diameter, diameter);
                else
                    g2d.drawLine(temp.getXPos()+diameter/2, temp.getYPos()+diameter/2,
                temp2.getXPos()+diameter/2, temp2.getYPos()+diameter/2);
                int x=(temp.getXPos()+temp2.getXPos())/2+diameter/2;
                int y=(temp.getYPos()+temp2.getYPos())/2+diameter/2;
                g2d.drawString(String.valueOf(ed.getWeight()), x,y); //draw the weight of edge
                
                //draw the arrows on edges if the graph is directed
                if(directedGraph) {
                    drawArrow(g2d,x,y,temp,temp2);
                }
            }
        }
    }
    
    private void drawArrow(Graphics2D g2d,int x,int y,Node a,Node b) {
        double m=0;
        if(a.getXPos()-b.getXPos()!=0) {
            m=((double)(b.getYPos()-a.getYPos())/(b.getXPos()-a.getXPos()));
        }
        else {
            m=(b.getYPos()-a.getYPos())/0.01;
        }
        if(b.getXPos()>a.getXPos()) {
            g2d.drawLine(x,y,(int)(x-(diameter/3)*Math.cos(Math.atan(m)+Math.toRadians(30))),(int)(y-(diameter/3)*Math.sin(Math.atan(m)+Math.toRadians(30))));
            g2d.drawLine(x,y,(int)(x-(diameter/3)*Math.cos(Math.atan(m)-Math.toRadians(30))),(int)(y-(diameter/3)*Math.sin(Math.atan(m)-Math.toRadians(30))));
        }
        else {
            g2d.drawLine(x,y,(int)(x+(diameter/3)*Math.cos(Math.atan(m)-Math.toRadians(30))),(int)(y+(diameter/3)*Math.sin(Math.atan(m)-Math.toRadians(30))));
            g2d.drawLine(x,y,(int)(x+(diameter/3)*Math.cos(Math.atan(m)+Math.toRadians(30))),(int)(y+(diameter/3)*Math.sin(Math.atan(m)+Math.toRadians(30))));
        }
    }
    
    private void drawNodes(Graphics2D g2d) {
        for(int i=0;i<nodes.size();i++) {
            Node temp=nodes.get(i);
            g2d.setColor(foreColor);
            g2d.fillOval(temp.getXPos(), temp.getYPos(), diameter, diameter);
            if(temp!=selectedNode) {
                g2d.setColor(foreColor2);
            }
            else {
                g2d.setColor(selectedColor);
            }
            g2d.fillOval(temp.getXPos()+borderWidth, temp.getYPos()+borderWidth, diameter-2*borderWidth, diameter-2*borderWidth);
            g2d.setColor(foreColor);
            g2d.drawString(temp.getName(), temp.getXPos()+(diameter/2)-(temp.getName().length()*fontSize/3), temp.getYPos()+(diameter/2)+(fontSize/3));
        }
    }
    
    private void makeNewEdge(Node a,Node b) {
        Edge ed=new Edge();
        ed.setSource(a);
        String s=JOptionPane.showInputDialog(this, "Weight","");
        if(s!=null) {
            int w=Integer.parseInt(s);
            ed.setWeight(w);
            ed.setDestination(b);
            b.addSrcEdge(ed);
            a.addDestEdge(ed);
            selectedNode=null;
            edges.add(ed);
        }
    }
    
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d=(Graphics2D)g;
        g2d.setStroke(new BasicStroke(2));
        g2d.setColor(backColor);
        g2d.setFont(new Font(Font.MONOSPACED,Font.BOLD,fontSize));
        g2d.fillRect(0, 0, getWidth(), getHeight());
        drawEdges(g2d);
        drawNodes(g2d);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object event=e.getSource();
        if(event==newButton) {
            String name=JOptionPane.showInputDialog(frame,"A Short Name for the Nodes separated by Single Space", "");
            if(name!=null) {
                String names[]=name.split(" ");
                for(int q=0;q<names.length;q++) {
                    Node temp=new Node();
                    int xP=0,yP=0;
                    for(int i=0;i<nodes.size();i++) {
                        Node t=nodes.get(i);
                        if(t.getXPos()>=xP && t.getXPos()<=xP+diameter
                                && t.getYPos()>=yP && t.getYPos()<=yP+diameter) {
                            xP+=diameter+1;
                            if(xP+diameter>width) {
                                xP=0;
                                yP=yP+diameter+1;
                            }
                            i=0;
                        }
                    }
                    temp.setXPos(xP);
                    temp.setYPos(yP);
                    temp.setName(names[q]);
                    nodes.add(temp);
                    joinButton.setEnabled(true);
                }
                repaint();
            }
        }
        else if(event==joinButton) {
            edgeMode=!edgeMode;
            if(!edgeMode) {
                selectedNode=null;
                repaint();
            }
        }
        else if(event==animeTimer) {
            repaint();
        }
        else if(e.getSource()==directedCheckBox) {
            directedGraph=directedCheckBox.isSelected();
            repaint();
        }
    }
    
    public int getDegree(Node n) {
        return n.getDestSize()+n.getSrcSize();
    }
    
    
    
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Graph();
        }
        });
    }
}

Screenshots

[Image: up1.png]

[Image: up2.png]

[Image: up3.png]

[Image: up4.png]
Folow me on My YouTube Channel if you're into art.

Reply

[Share] Graph PP #3
Few days ago I thought of making a project related to graphs and came up with this so far.. It is still incomplete, so you might find few portions in the code that remain unused. I would like to share the source code here and would love to hear some feedback and suggestions for it too.

Node.java
Code:
package graphpp;

import java.util.ArrayList;

public class Node {
    
    private String name;
    private ArrayList<Edge> destList;
    private ArrayList<Edge> srcList;
    private ArrayList<Edge> edgeList;
    private int dest,src;
    
    
    private boolean emphasized;
    private int x,y;
    
    public Node() {
        destList=new ArrayList<Edge>();
        srcList=new ArrayList<Edge>();
        edgeList=new ArrayList<Edge>();
        dest=-1;
        src=-1;
    }
    
    public void setName(String name) {
        this.name=name;
    }
    public String getName() {
        return name;
    }
    
    public void setEmphasize(boolean emphasize) {
        emphasized=emphasize;
    }
    public boolean isEmphasized() {
        return emphasized;
    }
    
    public void setXPos(int x) {
        this.x=x;
    }
    public int getXPos() {
        return x;
    }
    public void setYPos(int y) {
        this.y=y;
    }
    public int getYPos() {
        return y;
    }
    
    public void addDestEdge(Edge edge) {
        destList.add(edge);
        edgeList.add(edge);
    }
    public Edge getNextDestEdge() {
        dest=(dest+1)%destList.size();
        return destList.get(dest);
    }
    public int getDestSize() {
        return destList.size();
    }
    public ArrayList<Edge> getDestList() {
        return (new ArrayList<Edge>(destList));
    }
    
    public void addSrcEdge(Edge edge) {
        srcList.add(edge);
        edgeList.add(edge);
    }
    public Edge getNextSrcEdge() {
        src=(src+1)%srcList.size();
        return destList.get(src);
    }
    public int getSrcSize() {
        return srcList.size();
    }
    public ArrayList<Edge> getSrcList() {
        return (new ArrayList<Edge>(srcList));
    }
    
    public ArrayList<Edge> getAllEdgesList() {
        return (new ArrayList<Edge>(edgeList));
    }
    public int getDegree() {
        return edgeList.size();
    }
}

Edge.java
Code:
package graphpp;

public class Edge {
    
    private Node source,destination;
    private int weight;
    private boolean emphasize;
    
    public void setSource(Node source) {
        this.source=source;
    }
    public Node getSource() {
        return source;
    }
    
    public void setDestination(Node destination) {
        this.destination=destination;
    }
    public Node getDestination() {
        return destination;
    }
    
    public void setWeight(int weight) {
        this.weight=weight;
    }
    public int getWeight() {
        return weight;
    }
    
    public void setEmphasize(boolean emphasize) {
        this.emphasize=emphasize;
    }
    public boolean isEmphasized() {
        return emphasize;
    }
}

Graph.java
Code:
package graphpp;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Graph extends JPanel implements ActionListener{

    
    private JFrame frame;
    private JToolBar toolbar;
    private JButton newButton;
    private JToggleButton joinButton;
    private JCheckBox directedCheckBox;
    
    private Color backColor,foreColor,foreColor2,selectedColor,lineColor;
    private int borderWidth=2,diameter=40;
    
    private int width,height;
    
    private  ArrayList<Node> nodes;
    private ArrayList<Edge> edges;
    private Node pressedNode;
    private boolean pressed,edgeMode,directedGraph;
    private int xOffset,yOffset;
    private Timer animeTimer;
    private int fontSize=20;
    private Node selectedNode;
    
    public Graph() {
        frame=new JFrame("Graph PP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        width=Toolkit.getDefaultToolkit().getScreenSize().width;
        height=Toolkit.getDefaultToolkit().getScreenSize().height;
        
        frame.setSize(width, height);
        
        backColor=new Color(0x0099cc);
        foreColor2=new Color(0x00ee76);
        foreColor=new Color(0x1464f4);
        selectedColor=new Color(0xadadff);
        lineColor=new Color(0xcd661d);
        
        nodes=new ArrayList<Node>();
        edges=new ArrayList<Edge>();
        pressed=false;
        directedGraph=true;
        pressedNode=null;
        xOffset=0;
        yOffset=0;
        animeTimer=new Timer(3,this);
        selectedNode=null;
        
        newButton=new JButton("New Node");
        newButton.addActionListener(this);
        joinButton=new JToggleButton("Join Nodes");
        joinButton.addActionListener(this);
        joinButton.setEnabled(false);
        directedCheckBox=new JCheckBox("Directed Graph",true);
        directedCheckBox.addActionListener(this);
        
        toolbar=new JToolBar();
        toolbar.setFloatable(false);
        
        toolbar.add(newButton);
        toolbar.add(joinButton);
        toolbar.add(directedCheckBox);
        
        frame.add(toolbar,BorderLayout.NORTH);
        
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int x=e.getX();
                int y=e.getY();
                for(int i=0;i<nodes.size();i++) {
                    Node temp=nodes.get(i);
                    //if the mouse was pressed on a node
                    if(x>=temp.getXPos() && x<=temp.getXPos()+diameter
                            && y>=temp.getYPos() && y<=temp.getYPos()+diameter) {
                        //if user has enables join edge toggle button to draw new edges
                        if(edgeMode) {
                            if(selectedNode==null) {
                                selectedNode=temp;
                                repaint();
                            }
                            else {
                                makeNewEdge(selectedNode,temp);
                                repaint();
                            }
                            return;
                        }
                        updatePressedNode(temp,x,y);
                        return;
                    }
                }
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                if(pressed) {
                    pressed=false;
                    pressedNode=null;
                    xOffset=0;
                    yOffset=0;
                    removeMouseMotionListener(this);
                    animeTimer.stop();
                }
            }
        });
        
        frame.add(this);
        frame.setVisible(true);
    }
    
    private void updatePressedNode(Node a,int x,int y) {
        pressedNode=a;
        pressed=true;
        xOffset=x-a.getXPos();
        yOffset=y-a.getYPos();
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if(pressed) {
                    pressedNode.setXPos(e.getX()-xOffset);
                    pressedNode.setYPos(e.getY()-yOffset);
                }
            }
        });
        animeTimer.start();
    }
    
    private void drawEdges(Graphics2D g2d) {
        for(int i=0;i<nodes.size();i++) {
            Node temp=nodes.get(i);
            Node temp2=null;
            g2d.setColor(lineColor);
            for(int j=0;j<temp.getDestSize();j++) {
                Edge ed=temp.getNextDestEdge();
                temp2=ed.getDestination();
                if(temp.getXPos()==temp2.getXPos() && temp.getYPos()==temp2.getYPos())
                    g2d.drawOval(temp.getXPos()+diameter/2, temp.getYPos()+diameter/2, diameter, diameter);
                else
                    g2d.drawLine(temp.getXPos()+diameter/2, temp.getYPos()+diameter/2,
                temp2.getXPos()+diameter/2, temp2.getYPos()+diameter/2);
                int x=(temp.getXPos()+temp2.getXPos())/2+diameter/2;
                int y=(temp.getYPos()+temp2.getYPos())/2+diameter/2;
                g2d.drawString(String.valueOf(ed.getWeight()), x,y); //draw the weight of edge
                
                //draw the arrows on edges if the graph is directed
                if(directedGraph) {
                    drawArrow(g2d,x,y,temp,temp2);
                }
            }
        }
    }
    
    private void drawArrow(Graphics2D g2d,int x,int y,Node a,Node b) {
        double m=0;
        if(a.getXPos()-b.getXPos()!=0) {
            m=((double)(b.getYPos()-a.getYPos())/(b.getXPos()-a.getXPos()));
        }
        else {
            m=(b.getYPos()-a.getYPos())/0.01;
        }
        if(b.getXPos()>a.getXPos()) {
            g2d.drawLine(x,y,(int)(x-(diameter/3)*Math.cos(Math.atan(m)+Math.toRadians(30))),(int)(y-(diameter/3)*Math.sin(Math.atan(m)+Math.toRadians(30))));
            g2d.drawLine(x,y,(int)(x-(diameter/3)*Math.cos(Math.atan(m)-Math.toRadians(30))),(int)(y-(diameter/3)*Math.sin(Math.atan(m)-Math.toRadians(30))));
        }
        else {
            g2d.drawLine(x,y,(int)(x+(diameter/3)*Math.cos(Math.atan(m)-Math.toRadians(30))),(int)(y+(diameter/3)*Math.sin(Math.atan(m)-Math.toRadians(30))));
            g2d.drawLine(x,y,(int)(x+(diameter/3)*Math.cos(Math.atan(m)+Math.toRadians(30))),(int)(y+(diameter/3)*Math.sin(Math.atan(m)+Math.toRadians(30))));
        }
    }
    
    private void drawNodes(Graphics2D g2d) {
        for(int i=0;i<nodes.size();i++) {
            Node temp=nodes.get(i);
            g2d.setColor(foreColor);
            g2d.fillOval(temp.getXPos(), temp.getYPos(), diameter, diameter);
            if(temp!=selectedNode) {
                g2d.setColor(foreColor2);
            }
            else {
                g2d.setColor(selectedColor);
            }
            g2d.fillOval(temp.getXPos()+borderWidth, temp.getYPos()+borderWidth, diameter-2*borderWidth, diameter-2*borderWidth);
            g2d.setColor(foreColor);
            g2d.drawString(temp.getName(), temp.getXPos()+(diameter/2)-(temp.getName().length()*fontSize/3), temp.getYPos()+(diameter/2)+(fontSize/3));
        }
    }
    
    private void makeNewEdge(Node a,Node b) {
        Edge ed=new Edge();
        ed.setSource(a);
        String s=JOptionPane.showInputDialog(this, "Weight","");
        if(s!=null) {
            int w=Integer.parseInt(s);
            ed.setWeight(w);
            ed.setDestination(b);
            b.addSrcEdge(ed);
            a.addDestEdge(ed);
            selectedNode=null;
            edges.add(ed);
        }
    }
    
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d=(Graphics2D)g;
        g2d.setStroke(new BasicStroke(2));
        g2d.setColor(backColor);
        g2d.setFont(new Font(Font.MONOSPACED,Font.BOLD,fontSize));
        g2d.fillRect(0, 0, getWidth(), getHeight());
        drawEdges(g2d);
        drawNodes(g2d);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object event=e.getSource();
        if(event==newButton) {
            String name=JOptionPane.showInputDialog(frame,"A Short Name for the Nodes separated by Single Space", "");
            if(name!=null) {
                String names[]=name.split(" ");
                for(int q=0;q<names.length;q++) {
                    Node temp=new Node();
                    int xP=0,yP=0;
                    for(int i=0;i<nodes.size();i++) {
                        Node t=nodes.get(i);
                        if(t.getXPos()>=xP && t.getXPos()<=xP+diameter
                                && t.getYPos()>=yP && t.getYPos()<=yP+diameter) {
                            xP+=diameter+1;
                            if(xP+diameter>width) {
                                xP=0;
                                yP=yP+diameter+1;
                            }
                            i=0;
                        }
                    }
                    temp.setXPos(xP);
                    temp.setYPos(yP);
                    temp.setName(names[q]);
                    nodes.add(temp);
                    joinButton.setEnabled(true);
                }
                repaint();
            }
        }
        else if(event==joinButton) {
            edgeMode=!edgeMode;
            if(!edgeMode) {
                selectedNode=null;
                repaint();
            }
        }
        else if(event==animeTimer) {
            repaint();
        }
        else if(e.getSource()==directedCheckBox) {
            directedGraph=directedCheckBox.isSelected();
            repaint();
        }
    }
    
    public int getDegree(Node n) {
        return n.getDestSize()+n.getSrcSize();
    }
    
    
    
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Graph();
        }
        });
    }
}

Screenshots

[Image: up1.png]

[Image: up2.png]

[Image: up3.png]

[Image: up4.png]
Folow me on My YouTube Channel if you're into art.

Reply

RE: [Share] Graph PP #4
Looking forward to it. I have given this a quick look.I know it is still in development still I came up with a few points.

1. In the Node() constructor, you have given empty assignments for name. Like name =""; I would prefer to use null instead.

2. Instead of this "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" use the following ;

Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

3. Use a Mouse Adapter to get rid of those empty mouse event methods.

4. When I add the new vertices then on clicking the JPanel the vertices appear whereas they should appear as soon as they JOptionPane dialog is closed.

Sorry I will have a deeper look in your code later. I am a bit busy now. But its good to have you here.
[Image: OilyCostlyEwe.gif]

Reply

RE: [Share] Graph PP #5
Looking forward to it. I have given this a quick look.I know it is still in development still I came up with a few points.

1. In the Node() constructor, you have given empty assignments for name. Like name =""; I would prefer to use null instead.

2. Instead of this "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" use the following ;

Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

3. Use a Mouse Adapter to get rid of those empty mouse event methods.

4. When I add the new vertices then on clicking the JPanel the vertices appear whereas they should appear as soon as they JOptionPane dialog is closed.

Sorry I will have a deeper look in your code later. I am a bit busy now. But its good to have you here.
[Image: OilyCostlyEwe.gif]

Reply

RE: [Share] Graph PP #6
Looking forward to it. I have given this a quick look.I know it is still in development still I came up with a few points.

1. In the Node() constructor, you have given empty assignments for name. Like name =""; I would prefer to use null instead.

2. Instead of this "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" use the following ;

Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

3. Use a Mouse Adapter to get rid of those empty mouse event methods.

4. When I add the new vertices then on clicking the JPanel the vertices appear whereas they should appear as soon as they JOptionPane dialog is closed.

Sorry I will have a deeper look in your code later. I am a bit busy now. But its good to have you here.
[Image: OilyCostlyEwe.gif]

Reply

RE: [Share] Graph PP #7
(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 1. In the Node() constructor, you have given empty assignments for name. Like name =""; I would prefer to use null instead.

I thought they meant the same thing. When are we suppose to use empty string assignments and when are we suppose to use null assignments? Could you explain that to me?

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 2. Instead of this "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" use the following ;

Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
I think if I dispose instead of exiting when I hit the close button, the jvm process would not stop after i expect it to, right?

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 3. Use a Mouse Adapter to get rid of those empty mouse event methods.
I have been suggested that in past too. I guess I'll have to make a habit of it. I don't know why, I find Listeners more neater that Adapters.. :Laughing:

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 4. When I add the new vertices then on clicking the JPanel the vertices appear whereas they should appear as soon as they JOptionPane dialog is closed.
Strangely I never experienced that before. I updated a line in the code and now it should work fine in that department.


Thank you very much for taking time to go through my code. I'll keep your suggestions in mind while coding. :Smile:
Folow me on My YouTube Channel if you're into art.

Reply

RE: [Share] Graph PP #8
(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 1. In the Node() constructor, you have given empty assignments for name. Like name =""; I would prefer to use null instead.

I thought they meant the same thing. When are we suppose to use empty string assignments and when are we suppose to use null assignments? Could you explain that to me?

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 2. Instead of this "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" use the following ;

Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
I think if I dispose instead of exiting when I hit the close button, the jvm process would not stop after i expect it to, right?

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 3. Use a Mouse Adapter to get rid of those empty mouse event methods.
I have been suggested that in past too. I guess I'll have to make a habit of it. I don't know why, I find Listeners more neater that Adapters.. :Laughing:

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 4. When I add the new vertices then on clicking the JPanel the vertices appear whereas they should appear as soon as they JOptionPane dialog is closed.
Strangely I never experienced that before. I updated a line in the code and now it should work fine in that department.


Thank you very much for taking time to go through my code. I'll keep your suggestions in mind while coding. :Smile:
Folow me on My YouTube Channel if you're into art.

Reply

RE: [Share] Graph PP #9
(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 1. In the Node() constructor, you have given empty assignments for name. Like name =""; I would prefer to use null instead.

I thought they meant the same thing. When are we suppose to use empty string assignments and when are we suppose to use null assignments? Could you explain that to me?

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 2. Instead of this "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" use the following ;

Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
I think if I dispose instead of exiting when I hit the close button, the jvm process would not stop after i expect it to, right?

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 3. Use a Mouse Adapter to get rid of those empty mouse event methods.
I have been suggested that in past too. I guess I'll have to make a habit of it. I don't know why, I find Listeners more neater that Adapters.. :Laughing:

(10-25-2013, 03:30 PM)Psycho_Coder Wrote: 4. When I add the new vertices then on clicking the JPanel the vertices appear whereas they should appear as soon as they JOptionPane dialog is closed.
Strangely I never experienced that before. I updated a line in the code and now it should work fine in that department.


Thank you very much for taking time to go through my code. I'll keep your suggestions in mind while coding. :Smile:
Folow me on My YouTube Channel if you're into art.

Reply

RE: [Share] Graph PP #10
Quote:I thought they meant the same thing. When are we suppose to use empty string assignments and when are we suppose to use null assignments? Could you explain that to me?

String name = null means that you don't initialize anything at all, where as using "" would mean that it would mean that it would check for some string data within the quotes and that general gives you a warning "String cannot be left empty" or something equivalent.

Using "" means that you are creating a reference object of size 0.
using null means that you create no object.

So, did you get it ?

Why create even a single useless object.

Quote:I think if I dispose instead of exiting when I hit the close button, the jvm process would not stop after i expect it to, right?

Read this : http://www.hackcommunity.com/Thread-Java...#pid162671

Quote:I have been suggested that in past too. I guess I'll have to make a habit of it. I don't know why, I find Listeners more neater that Adapters.. :Laughing:

You see I have been taught that programming is an art and the your pc or laptop is your canvas. If you use extra colors and try to make it more brighter then it might have some negetive effects such as the color might went off the line which would make it look less beautiful. Your PC is like your girl why do you intend to put more more burden on it by letting it execute more lines of codes, just let her enjoy. So, I hope you understood. Also it is a good programming practice to avoid writing unnecessary methods.

I liked the way you have used the math to get your things done. I can evaluate very well what you have hiding inside you.

Quote:Strangely I never experienced that before. I updated a line in the code and now it should work fine in that department.


Thank you very much for taking time to go through my code. I'll keep your suggestions in mind while coding. :Smile:


No Need to thank me, it was my pleasure help you if I can in any manner. I was taught by someone and know I can pass that onto others. Even its a part of my learning too.
[Image: OilyCostlyEwe.gif]

Reply







Users browsing this thread: 1 Guest(s)