IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

2D Java Discussion :

[JGraph] Modifier la position des arcs


Sujet :

2D Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 19
    Par défaut [JGraph] Modifier la position des arcs
    Bonjour,
    Je viens de débuter avec jgraph. Pour mes premiers essais, j'ai récupéré une classe exemple "HelloWord". Je l'ai modifiée en rajoutant un nouveau noeud et un nouveau lien. Mais le problème est que le lien se positionne à la même place que celui qui existait. Voici le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
     
    import java.awt.Color;
    import java.awt.geom.Rectangle2D;
     
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
     
    import org.jgraph.JGraph;
    import org.jgraph.graph.DefaultEdge;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.DefaultGraphModel;
    import org.jgraph.graph.DefaultPort;
    import org.jgraph.graph.GraphConstants;
    import org.jgraph.graph.GraphModel;
     
    public class HelloWorld {
    public static void main(String[] args) {
    // Construct Model and Graph
    GraphModel model = new DefaultGraphModel();
    JGraph graph = new JGraph(model);
     
    // Control-drag should clone selection
    graph.setCloneable(false);
    // Enable edit without final RETURN keystroke
     
    graph.setInvokesStopCellEditing(true);
    // When over a cell, jump to its default port (we only have one, anyway)
     
    graph.setJumpToDefaultPort(true);
     
    // Insert all three cells in one call, so we need an array to store them
    DefaultGraphCell[] cells = new DefaultGraphCell[5];
     
    // Create Hello Vertex
    cells[0] = createVertex("Hello", 20, 20, 40, 20, Color.BLUE, false);
     
    // Create World Vertex
    cells[1] = createVertex("World", 20, 80, 40, 20, Color.ORANGE, true);
    cells[2] = createVertex("ess", 20, 140, 40, 20, Color.RED, true);
     
    // Create Edge
    DefaultEdge edge1 = new DefaultEdge();
    DefaultEdge edge2 = new DefaultEdge();
     
    // Fetch the ports from the new vertices, and connect them with the edge
     
    edge1.setSource(cells[0].getChildAt(0));
    edge1.setTarget(cells[1].getChildAt(0));
     
    edge2.setSource(cells[0].getChildAt(0));
    edge2.setTarget(cells[2].getChildAt(0));
     
    cells[3] = edge1;
    cells[4] = edge2;
     
    // Set Arrow Style for edge
    int arrow = GraphConstants.ARROW_CLASSIC;
    GraphConstants.setLineEnd(edge1.getAttributes(), arrow);
    GraphConstants.setEndFill(edge1.getAttributes(), true);
     
    GraphConstants.setLineEnd(edge2.getAttributes(), arrow);
    GraphConstants.setEndFill(edge2.getAttributes(), true);
     
    // Insert the cells via the cache, so they get selected
    graph.getGraphLayoutCache().insert(cells);
     
    // Show in Frame
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JScrollPane(graph));
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    }
     
    public static DefaultGraphCell createVertex(String name, double x,
    double y, double w, double h, Color bg, boolean raised) {
     
    // Create vertex with the given name
    DefaultGraphCell cell = new DefaultGraphCell(name);
     
    // Set bounds
    GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(x, y, w, h));
     
    // Set fill color
    if (bg != null) {
    GraphConstants.setGradientColor(cell.getAttributes(), Color.orange);
    GraphConstants.setOpaque(cell.getAttributes(), true);
    }
     
    // Set raised border
    if (raised)
    GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createRaisedBevelBorder());
    else
    // Set black border
    GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
    // Add a Port
    DefaultPort port = new DefaultPort();
    cell.add(port);
    port.setParent(cell);
     
    return cell;
    }
    }
    Moi je ne voudrais pas que les liens se superposent. Pour cela, j'ai vu ce bout de code sur le tuto ( ftp://ftp-developpez.com/mbaron/javase/javavisu.pdf ) , mais je ne vois pas bien comment l'utiliser pour résoudre mon problème.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
     
    import org.jgraph.graph.CellView;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.EdgeView;
    import org.jgraph.graph.PortView;
     
    public class MyPortView extends PortView {
     
    	public Point2D getLocation(EdgeView edge, Point2D nearest) {
    		CellView vertex = getParentView();
    		Point2D pos = null;
    		if (vertex != null) {
    		Rectangle2D r = vertex.getBounds();
    		double highOrLow = 0;
    		if (((DefaultGraphCell)vertex.getCell()).getChildAt(0) == cell) {
    		highOrLow = r.getMinY();
    		} else {
    		highOrLow = r.getMaxY();
    		}
    		if (r != null)
    		return new Point2D.Double(r.getCenterX(), highOrLow);
    		}
    		return pos;
    		}
     
    }
    Si quelqu'un peut m'indiquer comment m'y prendre ou poster un code qui fait la même chose que ce que je souhaite(c'est à dire éviter la superposition des liens), ca m'aiderait fort bien.

    En vous remerciant par avance.

  2. #2
    Membre émérite
    Avatar de bpy1401
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2003
    Messages
    511
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2003
    Messages : 511
    Par défaut
    Bonjour Kylie

    Voici une solution, pas très belle, mais qui te donne la procédure à suivre pour intégrer l'exemple que tu donne.

    Pour l'utilisation , j'ai modifié un peu MyPortView.
    Le code est le suivant

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
     
    import org.jgraph.graph.CellView;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.EdgeView;
    import org.jgraph.graph.PortView;
     
    public class MyPortView extends PortView {
     
      public MyPortView(Object port) {
        super(port);
      }
     
      public Point2D getLocation(EdgeView edge, Point2D nearest) {
        CellView vertex = getParentView();
        Point2D pos = null;
        if (vertex != null) {
          Rectangle2D r = vertex.getBounds();
          double leftOrRight = r.getCenterX();
     
          if (vertex != null) {
            DefaultGraphCell cell = (DefaultGraphCell) vertex.getCell();
            String name = (String) cell.getUserObject();
            if (name.equals("World")) {
              leftOrRight = r.getMaxX();
            } else if (name.equals("ess")) {
              leftOrRight = r.getMinX();
            }
          }
     
          if (r != null)
            return new Point2D.Double(leftOrRight, r.getCenterY());
        }
        return pos;
      }
    }
    Donc si le port est associé à l'objet World, la flèche sera connecté à droite de l'objet, si l'objet est ess, la flèche sera à gauche, pour Hello, la flèche est toujours au centre de l'objet.

    Pour pouvoir utiliser MyPortView, tu doit creer un objet du type DefaultCellViewFactory. Cette nouvelle classe permet de surcharger plusieurs m"éthode dont celle qui sert à créer les View pour les ports. Voici la code de cette classe

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    import org.jgraph.graph.CellViewFactory;
    import org.jgraph.graph.DefaultCellViewFactory;
    import org.jgraph.graph.DefaultPort;
    import org.jgraph.graph.PortView;
     
    public class MyViewFactory  extends DefaultCellViewFactory  {
     
      protected  PortView createPortView(Object port ) {
     
        if (port instanceof DefaultPort) {
           return  new MyPortView(port);
        }
     
        return new PortView(port);
      }
     
    }
    Il ne reste plus qu'a prendre en compte cette nouvelle factory comme ceci
    graph.getGraphLayoutCache().setFactory(new MyViewFactory());

    le code d'HelloWorld devient alors :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    import java.awt.Color;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
     
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
     
    import org.jgraph.JGraph;
    import org.jgraph.graph.CellView;
    import org.jgraph.graph.DefaultCellViewFactory;
    import org.jgraph.graph.DefaultEdge;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.DefaultGraphModel;
    import org.jgraph.graph.DefaultPort;
    import org.jgraph.graph.EdgeView;
    import org.jgraph.graph.GraphConstants;
    import org.jgraph.graph.GraphModel;
    import org.jgraph.graph.PortView;
     
    public class HelloWorld {
     
     
      public static void main(String[] args) {
        // Construct Model and Graph
        GraphModel model = new DefaultGraphModel();
        JGraph graph = new JGraph(model);
        graph.getGraphLayoutCache().setFactory(new MyViewFactory());
     
        // Control-drag should clone selection
        graph.setCloneable(false);
        // Enable edit without final RETURN keystroke
     
        graph.setInvokesStopCellEditing(true);
        // When over a cell, jump to its default port (we only have one, anyway)
     
        graph.setJumpToDefaultPort(true);
     
        // Insert all three cells in one call, so we need an array to store them
        DefaultGraphCell[] cells = new DefaultGraphCell[5];
     
        // Create Hello Vertex
        cells[0] = createVertex("Hello", 20, 20, 40, 20, Color.BLUE, false);
        DefaultPort port;
     
        // Create World Vertex
        cells[1] = createVertex("World", 20, 80, 40, 20, Color.ORANGE, true);
        cells[2] = createVertex("ess", 20, 140, 40, 20, Color.RED, true);
     
        // Create Edge
        DefaultEdge edge1 = new DefaultEdge("left");
        DefaultEdge edge2 = new DefaultEdge("right");
     
        // Fetch the ports from the new vertices, and connect them with the edge
     
        edge1.setSource(cells[0].getChildAt(0));
        edge1.setTarget(cells[1].getChildAt(0));
     
        edge2.setSource(cells[0].getChildAt(0));
        edge2.setTarget(cells[2].getChildAt(0));
     
        cells[3] = edge1;
        cells[4] = edge2;
     
        // Set Arrow Style for edge
        int arrow = GraphConstants.ARROW_CLASSIC;
        GraphConstants.setLineEnd(edge1.getAttributes(), arrow);
        GraphConstants.setEndFill(edge1.getAttributes(), true);
     
        GraphConstants.setLineEnd(edge2.getAttributes(), arrow);
        GraphConstants.setEndFill(edge2.getAttributes(), true);
     
        // Insert the cells via the cache, so they get selected
        graph.getGraphLayoutCache().insert(cells);
     
        // Show in Frame
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JScrollPane(graph));
        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
      }
     
      public static DefaultGraphCell createVertex(String name, double x, double y, double w, double h, Color bg,
          boolean raised) {
     
        // Create vertex with the given name
        DefaultGraphCell cell = new DefaultGraphCell(name);
     
        // Set bounds
        GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(x, y, w, h));
     
        // Set fill color
        if (bg != null) {
          GraphConstants.setGradientColor(cell.getAttributes(), Color.orange);
          GraphConstants.setOpaque(cell.getAttributes(), true);
        }
     
        // Set raised border
        if (raised)
          GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createRaisedBevelBorder());
        else
          // Set black border
          GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
        // Add a Port
        DefaultPort port = new DefaultPort();
        cell.add(port);
        port.setParent(cell);
     
        return cell;
      }
    }
    Si je n'ai rien oublier tu devrais pouvoir voir le résultat.
    j'espère que cela te permettra de résoudre ton problème

    Cordialement
    Page sur Developpez : http://pbriand.developpez.com

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 19
    Par défaut
    Bonjour Briand Patrick,
    C'est bon, je les ai essayés et ca me donne le résultat. Merci à toi !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Modifier la position des items d'un popup menu
    Par popo dans le forum Débuter
    Réponses: 3
    Dernier message: 13/05/2011, 15h16
  2. [surfc] Modifier la position des contours
    Par kis93 dans le forum MATLAB
    Réponses: 3
    Dernier message: 11/03/2008, 09h37
  3. Comment modifier la position d'une Form avec des RadioButton ?
    Par bionicleman dans le forum Composants VCL
    Réponses: 1
    Dernier message: 18/12/2007, 17h19
  4. pb pour modifier la luminositée des couleurs
    Par mathieutlse dans le forum Langage
    Réponses: 2
    Dernier message: 08/01/2003, 14h45
  5. Changer dynamiquement la position des onglets
    Par ginnovy dans le forum C++Builder
    Réponses: 2
    Dernier message: 11/09/2002, 18h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo