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

AWT/Swing Java Discussion :

[JGraph]Création d'une Cellule avec un RoundRectangle


Sujet :

AWT/Swing Java

Vue hybride

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

    Informations forums :
    Inscription : Juillet 2007
    Messages : 14
    Par défaut [JGraph]Création d'une Cellule avec un RoundRectangle
    Bonjour,
    j'utilise actuellement la librairie JGraph et j'aimerais savoir comment créer des cellules aux formes que j'aurais choisi comme des RoundRectangle, des losanges ou des cercles.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    GraphConstants.setBounds(map, new Rectangle2D.Double(point.getX(),
    				point.getY(), 0, 0));
    la méthode setBounds ne prend en paramètre que des rectangles 2D. J'ai beau avoir cherché sur la documentation de JGraph et dans les exemples fournis, je n'ai pas trouvé résultat à mon problème.

    Quelqu'un aurait il une idée ?

    Merci d'avance.

  2. #2
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2007
    Messages
    697
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 697
    Par défaut
    Salut,
    l'heritage tu y a pensé ?
    Même s'il est vrai que RoundRectangle2D n'herite pas de Rectangle2D tu peut peut-etre creer ta propre classe(c'est vrai c'est chaud).

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 14
    Par défaut
    oui j'ai déja pensé a l'héritage... mais je vois pas l'interet de me compliqué la vie pour faire quelque chose qui est déja possible !

    Merci quand même pour la réponse.

  4. #4
    Membre confirmé
    Inscrit en
    Mars 2008
    Messages
    80
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 80
    Par défaut
    Il faut 3 classes:
    1)HelloWorld:
    public class HelloWorld {
    public static void main(String[] args) {
    GraphModel model = new DefaultGraphModel();
    GraphLayoutCache view = new GraphLayoutCache(model,
    new
    DefaultCellViewFactory());
    JGraph graph = new JGraph(model, view);
    graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory() {
    // CellViews for each type of cell
    protected VertexView createVertexView(Object cell) {
    if (cell instanceof MyCell)
    return new JGraphEllipseView(cell);
    return new VertexView(cell);
    }
    });

    graph.getGraphLayoutCache().insert(new MyCell(new JGraphEllipseView()));
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JScrollPane(graph));
    frame.pack();
    frame.setVisible(true);
    }
    }


    2)MyCell:
    public class MyCell extends DefaultGraphCell
    {
    public MyCell(Object userObject) {
    super(userObject);
    //getAttributes().applyMap(createCellAttributes(point));
    }
    public Map createCellAttributes(Point2D point, Integer w, Integer h, ImageIcon icon) {
    Map map = new Hashtable();
    GraphConstants.setResize(map,false);
    GraphConstants.setAutoSize(map,false);
    // Add a Bounds Attribute to the Map
    GraphConstants.setBounds(map, new Rectangle2D.Double(point.getX(), point.getY(),0,0));
    GraphConstants.setVerticalTextPosition(map, SwingConstants.TOP);
    //GraphConstants.setFont(map, new Font("Dialog", Font.PLAIN, 10));
    GraphConstants.setLineWidth(map, 2.0f);
    GraphConstants.setOpaque(map,false);
    GraphConstants.setBorder(map, BorderFactory.createTitledBorder("smAA"));
    return map;
    }}

    3)JGraphEllipseView:
    public class JGraphEllipseView extends VertexView {
    /**
    */
    public static transient JGraphEllipseRenderer renderer = new JGraphEllipseRenderer();
    /**
    */
    public JGraphEllipseView() {
    super();
    }
    /**
    */
    public JGraphEllipseView(Object cell) {
    super(cell);
    }
    /**
    * Returns the intersection of the bounding rectangle and the
    * straight line between the source and the specified point p.
    * The specified point is expected not to intersect the bounds.
    */
    public Point2D getPerimeterPoint(EdgeView edge, Point2D source, Point2D p) {
    Rectangle2D r = getBounds();
    double x = r.getX();
    double y = r.getY();
    double a = (r.getWidth() + 1) / 2;
    double b = (r.getHeight() + 1) / 2;
    // x0,y0 - center of ellipse
    double x0 = x + a;
    double y0 = y + b;
    // x1, y1 - point
    double x1 = p.getX();
    double y1 = p.getY();
    // calculate straight line equation through point and ellipse center
    // y = d * x + h
    double dx = x1 - x0;
    double dy = y1 - y0;
    if (dx == 0)
    return new Point((int) x0, (int) (y0 + b * dy / Math.abs(dy)));
    double d = dy / dx;
    double h = y0 - d * x0;
    // calculate intersection
    double e = a * a * d * d + b * b;
    double f = -2 * x0 * e;
    double g = a * a * d * d * x0 * x0 + b * b * x0 * x0 - a * a * b * b;
    double det = Math.sqrt(f * f - 4 * e * g);
    // two solutions (perimeter points)
    double xout1 = (-f + det) / (2 * e);
    double xout2 = (-f - det) / (2 * e);
    double yout1 = d * xout1 + h;
    double yout2 = d * xout2 + h;
    double dist1Squared = Math.pow((xout1 - x1), 2)
    + Math.pow((yout1 - y1), 2);
    double dist2Squared = Math.pow((xout2 - x1), 2)
    + Math.pow((yout2 - y1), 2);
    // correct solution
    double xout, yout;
    if (dist1Squared < dist2Squared) {
    xout = xout1;
    yout = yout1;
    } else {
    xout = xout2;
    yout = yout2;
    }
    return getAttributes().createPoint(xout, yout);
    }
    /**
    */

    public CellViewRenderer getRenderer() {
    return renderer;
    }
    /**
    */
    public static class JGraphEllipseRenderer extends VertexRenderer {
    /**
    * Return a slightly larger preferred size than for a rectangle.
    */
    public Dimension getPreferredSize() {
    Dimension d = super.getPreferredSize();
    d.width += d.width / 8;
    d.height += d.height / 2;
    return d;
    }
    /**
    */
    public void paint(Graphics g) {
    int b = borderWidth;
    Graphics2D g2 = (Graphics2D) g;
    Dimension d = getSize();
    boolean tmp = selected;
    if (super.isOpaque()) {
    g.setColor(super.getBackground());
    if (gradientColor != null && !preview) {
    setOpaque(false);
    g2.setPaint(new GradientPaint(0, 0, getBackground(),
    getWidth(), getHeight(), gradientColor, true));
    }
    g.fillOval(b - 1, b - 1, d.width - b, d.height - b);
    }
    try {
    setBorder(null);
    setOpaque(false);
    selected = false;
    super.paint(g);
    } finally {
    selected = tmp;
    }
    if (bordercolor != null) {
    g.setColor(bordercolor);
    g2.setStroke(new BasicStroke(b));
    g.drawOval(b - 1, b - 1, d.width - b, d.height - b);
    }

    g2.setStroke(GraphConstants.SELECTION_STROKE);
    g.setColor(highlightColor);
    g.setColor(Color.blue);
    g.drawOval(b - 1, b - 1, d.width - b, d.height - b);

    }

    }
    }

Discussions similaires

  1. Réponses: 2
    Dernier message: 11/03/2010, 11h29
  2. création d'une carte avec navigation du type mappy
    Par BernardT dans le forum Interfaces Graphiques en Java
    Réponses: 3
    Dernier message: 30/10/2005, 01h24
  3. Création d'une table avec foreign key
    Par lepierre dans le forum Langage SQL
    Réponses: 5
    Dernier message: 17/09/2004, 14h20
  4. [Débutante] Création d'une image avec un composant
    Par gwendo dans le forum AWT/Swing
    Réponses: 9
    Dernier message: 09/07/2004, 09h58
  5. Création d'une base avec IbConsole
    Par Lucien dans le forum Outils
    Réponses: 3
    Dernier message: 02/03/2004, 18h34

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