Graph Drawing Algorithms II

Hi guys,
I'm researching graph layout algorithms. Most of the packages available don't suit because they require the user to specify a number of nodes and edges between them and then it draws them very prettily.
Because I want an extension of a Circle class to be drawn and I need it to be drawn on a customised JPanel called Canvas most of these programs aren't suitable. I have the program laying them out itself but atm they look crude and horrible.
What I'd like is a plug in java program that can assign coordinates and a basic structure to the graph that I entered.
Thank you very much for reading this far,

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class CircleTest extends JPanel
    CircleModel[] circles;
    CircleModel selectedCircle;
    final int
        PAD  = 20,
        SIZE = 10;
    public CircleTest()
        Point[] cps = { new Point(25,25), new Point(80,60), new Point(35,80) };
        circles = new CircleModel[cps.length];
        circles[0] = new CircleModel(cps[0], 15, Color.green.darker(), Color.cyan);
        circles[1] = new CircleModel(cps[1], 20, Color.red,            Color.orange);
        circles[2] = new CircleModel(cps[2], 15, Color.blue,           Color.magenta);
    protected void paintComponent(Graphics g)
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D)g;
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
         double w = getWidth();
         double h = getHeight();
         double xInc = SIZE * (w - 2*PAD) / CircleModel.MAX_X;
         double yInc = SIZE * (h - 2*PAD) / CircleModel.MAX_Y;
         g2.setPaint(new Color(220,200,240));
         // vertical model grid lines
         double x = PAD;
         for(int j = 0; j <= SIZE; j++, x += xInc)
             g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
         // horizontal model grid lines
         double y = PAD;
         for(int j = 0; j <= SIZE; j++, y += yInc)
             g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
         double vw = w - 2*PAD;
         double vh = h - 2*PAD;
         for(int j = 0; j < circles.length; j++)
             CircleModel circle = circles[j];
             Color color = circle.color;
             if(circle == selectedCircle)
                 color = circle.selectColor;
             g2.setPaint(color);
             Point2D p = circle.getLocation(vw, vh);
             double[] s = circle.getSize(vw, vh);
             g2.draw(new Ellipse2D.Double(PAD+p.getX(), PAD+p.getY(), s[0], s[1]));
             g2.setPaint(Color.red);
             p = circle.getCenter(vw, vh);
             g2.fill(new Ellipse2D.Double(PAD+p.getX()-2, PAD+p.getY()-2, 4, 4));
    public void setSelection(CircleModel selected)
        selectedCircle = selected;
        repaint();
    public Rectangle2D getViewRect()
        Rectangle2D.Double r = new Rectangle2D.Double();
        r.x = PAD;
        r.y = PAD;
        r.width  = getWidth()  - 2*PAD;
        r.height = getHeight() - 2*PAD;
        return r;
    public static void main(String[] args)
        CircleTest test = new CircleTest();
        SweepTester sweeper = new SweepTester(test);
        test.addMouseMotionListener(sweeper);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(test);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
class SweepTester extends MouseMotionAdapter
    CircleTest circleTest;
    CircleModel lastSelection;
    public SweepTester(CircleTest ct)
        circleTest = ct;
    public void mouseMoved(MouseEvent e)
        Point p = e.getPoint();
        boolean haveSelection = false;
        CircleModel[] circles = circleTest.circles;
        Rectangle2D r2 = circleTest.getViewRect();
        for(int j = 0; j < circles.length; j++)
            if(circles[j].contains(p, r2))
                circleTest.setSelection(circles[j]);
                lastSelection = circles[j];
                haveSelection = true;
                break;
        if(!haveSelection && lastSelection != null)
            lastSelection = null;
            circleTest.setSelection(null);
class CircleModel
    Point2D center;
    double radius;
    Color color;
    Color selectColor;
    final static int
        MAX_X = 100,
        MAX_Y = 100;
    public CircleModel(Point p, int r, Color c, Color sc)
        center = p;
        radius = r;
        color = c;
        selectColor = sc;
    protected Point2D getLocation(double w, double h)
        Point2D.Double modelLoc = new Point2D.Double();
        modelLoc.x = center.getX() - radius;
        modelLoc.y = center.getY() - radius;
        Point2D viewLoc = modelToView(modelLoc, w, h);
        return viewLoc;
    protected Point2D getCenter(double w, double h)
        return modelToView(center, w, h);
    protected double[] getSize(double w, double h)
        Point2D cp = getCenter(w,h);
        Point2D loc = getLocation(w,h);
        double width  = 2 * (cp.getX() - loc.getX());
        double height = 2 * (cp.getY() - loc.getY());
        return new double[] { width, height };
    private Point2D modelToView(Point2D modelP, double viewWidth, double viewHeight)
        Point2D.Double viewP = new Point2D.Double();
        viewP.x = viewWidth  * modelP.getX() / MAX_X;
        viewP.y = viewHeight * modelP.getY() / MAX_Y;
        return viewP;
    public boolean contains(Point p, Rectangle2D r)
        Point2D loc = getLocation(r.getWidth(), r.getHeight());
        double[] size = getSize(r.getWidth(), r.getHeight());
        Ellipse2D e = new Ellipse2D.Double(r.getX() + loc.getX(),
                                           r.getY() + loc.getY(), size[0], size[1]);
        return e.contains(p);
}

Similar Messages

  • I want to make a Graph Drawing Servlet

    I need to make line graphs, having days on the X axis and certain values on the Y axis. I need to show this on a JSP. I was thinking of making a servlet returning the image, but I'm not finding any Java solution for the graph drawing part. I would really like to avoid having to manually draw the graph. Does anyone know a simple way to do this?

    The Google Chart API is pretty simple and powerful.
    http://code.google.com/apis/chart/

  • Graph Drawing Framework

    I'm looking for a framework to use to draw Graphs (nodes and connecting links). Does anyone out there know of anything already implemented like this... Something like ARGO UML???
    TIA

    Hi!
    Take a look at http://www.yworks.de ! They have a powerful graph drawing framework and API called yFiles.

  • Code of line drawing algorithm

    anybody can tell me code of line drawing algorithm..if i hav two points a(x1,y1) and b(x2,y2) then
    how kan i draw line using these points..

    Use drawLine(int x1, int y1, int x2, int y2).
    As I understand it you should only do the drawing in a paint method (or paintComponent method if you're using swing.) If you're wanting the lines to be "permanent" then draw to a drawing.
    e.g. from a Logo/Turtle prog that I'm working on:
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if ( ! initDone ) {
    i_drawing = (BufferedImage)createImage(700,500);
    g_drawing = i_drawing.createGraphics();
    initDone=true;
    if (penDown && drawOp) {
    g_drawing.drawLine((int)posX, (int)posY, (int)targX, (int)targY);
    drawOp = false;
    g.drawImage(i_drawing, 0, 0, this);
    posX = targX;
    posY = targY;
    This draws the line on the image (i_drawing) and then displays the image.
    Hope this helps.

  • Filter out marked data from XY graph (Draw graph)

    Hi there,
    I've a question about "filtering" data from an XY Graph, please see the attachement.
    The graph shows some sample data (white plot) and a red "ballon" plot (you can press left mouse button
    and move over the graph to draw the red plot).
    My need is to get all samples inside the red ballon, but presently I got stuck with it!
    Any Ideas?!
    (The attached VI is saved by LabVIEW2009 version)
    Solved!
    Go to Solution.
    Attachments:
    Paint_XY_Graph.vi ‏24 KB

    To determine id a point is inside of the red baloon it must satisfy the condition where the X,Y is less than any X,Y point and greater than any other X,Y point.   To filter them out remove outliers from the array or set their values to nan to not plot them 
    Paul Falkenstein
    Coleman Technologies Inc.
    CLA, CPI, AIA-Vision
    Labview 4.0- 2013, RT, Vision, FPGA

  • Line Graph Drawing

    Hi i have developed a class for the information manipulation of a Line graph
    I would Like some help in actually drawing the graph so i can pass values
    manipulated here to the drawing of the graph any help is greatly appreciated thank you
    package ICUv2;
    import java.awt.Point;
    import java.util.Random;
    public class ecgSensor extends Paitent {
        private int x;
        private int y;
        private int xpos;
        private int ypos;
        private int min;
        private int max;
        private Point rate;
         public ecgSensor(int x, int y, int min, int max, int xpos, int ypos ){
        setX(x);
        setY(y);
        setMin(min);
        setMax(max);
        setRate(getXpos() , getYpos());
        public ecgSensor(int x, int y, int min, int max ){
        this(x,y,min,max,0,0);
        setX(x);
        setY(y);
        setMin(min);
        setMax(max);
        public ecgSensor(){
        this(5, 5, 50, 100);
        @Override
        public String toString(){
        return String.format("%s :%s", getX(), getY());
        public static void main (String[] args){
        ecgSensor a = new ecgSensor() ;
        System.out.print(a.toString());
        //System.out.print(a.getX());
        //System.out.print("  ");
        //System.out.print(a.getY());
        public Point getRate(){
        return rate;
        public void setRate(int x, int y){
        this.rate = new Point (x, y);
        public int getX(){
        return x;
        public int getY(){
        return y;
        public void setX(int x){
        this.x = x;
        public void setY(int y){
        this.y = y;
        public  int getMin(){
        return min ;
        public  int getMax(){
        return max;
        public  void setMin(int min){
        this.min = min;
        public  void setMax(int max){
        this.max = max;
        public int getXpos(){
        return xpos;
        public int getYpos(){
        return ypos;
        public void setXpos(int xpos){
        this.xpos = xpos;
        public void setYpos(int ypos){
        this.ypos = ypos;
        public int incYpos(){
        return this.ypos;
        public void setincYpos(int inc){
        setYpos(getYpos()-inc);
        public int decYpos(){
        return this.ypos;
        public void setdecYpos(int dec){
        setYpos(getYpos()+ dec);
        public class ecgReadings implements Runnable {
        Random ecgr = new Random();
        int Change = ecgr.nextInt(20);
        public void run(){
            this.toString();
            while (true) {
                  if (x%2==0){
                    setXpos(getXpos()+ 10);
                    setdecYpos(Change);
                    }else{
                    setXpos(getXpos()+ 10);
                    setincYpos(Change);
           try{
             Thread.sleep(1000);
             }catch(InterruptedException e){}
           }// end of while
        }// end of run method
    }// end of  ReviseBloodrate class
    }

    xiaolixx wrote:
    Hi i have developed a class for the information manipulation of a Line graph
    I would Like some help in actually drawing the graph so i can pass values
    manipulated here to the drawing of the graph ..Right. Did you have a question, or did you think your plaintiff cry for help would get someone to do it all for you?
    ..any help is greatly appreciated thank you
    package ICUv2;
    By convention, package names are alllowercase (It's funny you should rediscover your shift key, just for some package name where it is inappropriate!). Class names should be EachWordUpperCase, while attributes and methods should be firstWordLowerCaseAllTheRestUpper.
    By using the common conventions for code, you help to communicate the problem to others who are used to seeing that nomenclature.
    And as I said before.. "For better help sooner, post an SSCCE".

  • Graph Theory Algorithm-All possible paths between 2 vertices w/ constraints

    Hi all,
    I have a project I'm working with. I need to find all possible paths between two vertices on a graph.
    I realize this will be NP-complete or worse, but right now i'm looking for a brute force method to do this via algorithm/pseudocode
    Given:
    connected, weighted edges, directed Graph G = (V,E) with no loops
    Given v1 and v2 are vertices in V, and C = constraint value,
    I would like a way to find all possible paths from v1 to v2 that have a length less than C. Length = adding up all the edges on a path
    Can anyone provide any help on this?
    Thanks!

    Sure, no problemo.
    Create a bucket of paths, initially empty. (Bucket is a technical term for a collection)
    Start at v1. Take all the edges that lead from v1 to any place else, like x.
    Each one of those paths consists of a single edge is a path from v1 to somewhere and furthermore it has a length. It is a partial path with a length Now do it again, grab any path out of the bucket, leave from its terminal point x and extend it by an edge and create a bunch more paths that are now two edges long, which you can throw back into that same bucket.
    If you ever get to v2, you have a path from v1 to p2. Add it to your solutions list. If you ever exceed C, well throw it out because it is too long. And if you can't extend from a particular vertex then toss it as well.
    All you ever do is pull a partial path from the bucket, create all its possible one edge extensions, keep the winners, toss out the impossible, and throw all new still valid partial paths back into the bucket.
    If there are loops and if there are edges with zero or negative weight, this would not necessarily terminate. But you say that is not a problem for you.
    Just for the record, nothing is worse than NP-complete.

  • Graph drawing in JSP

    How can i get my jsp to draw a graph automatically with results from a database. The main problem is tryiing to get the JSP to display a graph. The rest I could probably figure out. Can anyone help me please??????????????????????

    or use JFreeChart. You can generate the chart image in a servlet and use an image HTML tag to show it. Search the forums, I have posted some example of that somewhere.

  • 3D Graph Layout Algorithms?

    Hello there! I'm developing a project for my first computer science thesis at the university of Bari, Italy. This project consists in a OpenGL graph visualization tool but I'm having trouble in implementing a correct layout algorithm.
    The problem is that I have been unable to find any information regarding the actual algorithms.. all I've discovered is that there are many commercial products out there that implement these algorithms.. :) I need to know how to implement them myself!
    I have a set of spheres (and the x-y-z coordinates of their center), and a set of edges which describe the various connection between those spheres. I'm currently using a sort of very simple spring embedder algorithm which works in this way:
    Phase 1:
    get sphere 1 (s1)
    get sphere 2 (s2)
    check s1 against all other spheres
    if their euclidean distance is too small they are moved farther away in this way:
    if (s1.x < s2.x)
    s1.x -= delta
    s2.x += delta
    if (s1.y < s2.y)
    s1.y -= delta
    and so on. This is the only way to avoid collisions between spheres that I thought of, since they are not puntiform in fact they have a certain radious. Then in phase 2 I iterate between all the edges and if two connected spheres are too distant from each other they are moved nearer in a way like the one described above. But it doesn't seem to work as it should in fact this algorithm seems to lock the spheres after a number of iteration but rarely produces pleasing visual configurations.
    So could anyone please point me in the right direction? What kind of algorithm could I use? A description would be very useful.. Thanks in advance!

    Hi, I am coauthor and developer of some of the spring-embedder algorithms used in the Java graph layout library 'yFiles' (we call them 'organic layout algorithms'). Some of the algorithms actually work in 3D and all of them can be adapted very easily to run in 3D. We are using combinations and variants and enhanced versions of some of the following algorithms, as well as some of our own, but for a starter this should be enough information for you:
    google for 'GRIP layout' 'GUIDE layout', Kamada-Kawai, Fruchterman-Reingold, Eigenvalue graph layout, GEM spring embedder,
    Implementing those algorithms well can lead to very nice results: feel free to take a look at our gallery of graph layouts:
    http://www.yworks.com/en/products_yfiles_practicalinfo_gallery.htm
    Regards, and a happy new year, Sebastian

  • Graph drawing in JS

    is it possible to to draw a graph in a web page automatically with results from a database.(using JSP)

    I suggest that you use servlet to draw the graphics.
    In your jsp, you can call the servlet like this.
    <img src="myServlet" width="100" height="100">

  • Graph drawing using JSP

    is it possible to to draw a graph in a web page automatically with results from a database.(using JSP)

    Did it took 8 months to solve it?
    Anyway, glad you got it fixed.

  • Anyone use a Graph Drawing Kit

    I'm looking for some extension libraries that could help me draw graphs of data. I'm using java version se 1.4.2 and netBeans in conjunction with apaches HSSF extension to write excel files. I know that they have some cool graph and picture functionality included, however, I would like to render graphs in my GUI as well.
    Any ideas?
    thanks

    Yeah I guess charts would be helpful too .. so I
    guess I'll check out jfree.org ... also is JGraph a
    normal swing class?Both JFreeChart and JGraph are open source third party libraries and are not part of the JRE/JDK distribution.

  • XY-Graph, draw Measured value with Timepstamp (e.g. - 2,34; 12:31:12)

    Hi there,
    I have a proble to draw a XY plot. How is it possible to set the X-axis with the recorder timestamp ?
    pls see the attachmend!
    Thank you!
    Attachments:
    Test.vi ‏22 KB

    You can translate a string to a timestamp by converting each element to a number and building a time cluster. The attached VI shows an example of the technique, but it's not finished and probably has some bugs. If your format is set, you can write a small piece of code to do this.
    Try to take over the world!
    Attachments:
    time21.vi ‏124 KB

  • Graph drawing - drawing an arc to and from the same node, how??

    Hey everyone
    This is probably going to have an easy solution but I really cant figure it out!! The problem is drawing a loop in a state machine, ie a path to and from the same node therefore a loop!
    Is there a way to actually do this?
    Thanks in advance!

    Graphics.drawArc(left, top, width, height, start, span) where start is the start angle in degrees, and span is the number of degrees of arc. Assuming you're like me, you're probably putting in the end angle rather than the span.
    Here's one I made earlier- http://www.metamodelling.net/files/ReflexiveArc.java - steps through nine common placements for a circular reflexive arc.
    Pete

  • PROBLEM WITH GRAPH DRAWING PACKAGE

    Hi all,
    I have been trying to compile and run some code using "JOPenChart" a free charting tool, but I am having many problems.
    Here is the small class:
    import de.progra.charting.*;
    import de.progra.charting.model.*;
    import de.progra.charting.render.*;
    import java.awt.*;
    import java.io.*;
    public class test{
    public static void makeFirstChart() {
         int[][] model = {{0, 100, 200000}};     // Create data array
         double[] columns = {0.0, 1.0, 2000.0};  // Create x-axis values
         String[] rows = {"DataSet 1"};          // Create data set title
         String title = "A First Test";          // Create diagram title
         int width = 640;                        // Image size
         int height = 480;
         // Create data model
         DefaultChartDataModel data = new DefaultChartDataModel(model, columns, rows);
         // Create chart with default coordinate system
         DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y);
         // Add a line chart renderer
         c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(), data,
         RowColorModel.getInstance(data)), 1);
         // Set the chart size
         c.setBounds(new Rectangle(0, 0, width, height));
         // Export the chart as a PNG image
         try {
              ChartEncoder.createEncodedImage(new FileOutputStream(System.getProperty("user.home")+"/first.png"), c, "png");
              } catch(Exception e)
                   e.printStackTrace();
    }//method
    public static void main (String [] args)
         makeFirstChart();
    }//classI have downloaded the JAR files from the web site. They are called:
    "jep.jar"
    "servlet.jar"
    "charting-0.94.jar"
    but they do not seem to make any difference and I still get compile errors:
    C:\Proj\code\chart>javac test.java
    test.java:1: package de.progra.charting does not exist
    import de.progra.charting.*;
    test.java:2: package de.progra.charting.model does not exist
    import de.progra.charting.model.*;
    test.java:3: package de.progra.charting.render does not exist
    import de.progra.charting.render.*;I cannot work out how to let the cimpiler know that I want to use the said JAR files, or what else to do to solve the above situation...
    Please help as this is CRITICAL to my University project!!!!
    Any help would be much apprecicated!
    Many thanks.

    Does your classpath include the jars you list?
    DB

Maybe you are looking for