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

Graphisme Java Discussion :

extremité de segment


Sujet :

Graphisme Java

Vue hybride

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

    Informations forums :
    Inscription : Octobre 2007
    Messages : 35
    Par défaut extremité de segment
    bonjour

    J'aimerai savoir s'il existe une méthode pour avoir avoir des lignes, plutot segment avec au bout une fleche ou lansange plein ou vide, triangle plein ou vide.
    Je sais tracé un segment mais j'aimerai pouvoir choisir ses extrémités de début et de fin, un peu comme ce que fais word et visio.

    crdl

  2. #2
    Membre éclairé Avatar de TrYde
    Homme Profil pro
    Responsable de l'industrialisation logiciel
    Inscrit en
    Juillet 2005
    Messages
    55
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Responsable de l'industrialisation logiciel
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 55
    Par défaut
    Bonjour,

    De base il n'y a pas de méthode pour faire une flèche, mais ce n'est pas si difficile. Il suffit de créer une flèche à l'horizontale sous forme de polygone, puis d'appliquer une rotation et une translation pour se placer où on veut.

    Ensuite un drawPolygon ou fillPolygon fera le reste.

    Un exemple :

    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
     
        public Polygon creerFleche() {
            int[] y = {
                    -(arrowWidth-1)/2, 
                    (arrowWidth-1)/2, 
                    (arrowWidth-1)/2, 
                    (int) (arrowWidth*1.5),
                    0,
                    (int) (-arrowWidth*1.5),
                    -(arrowWidth-1)/2
                    };
            int[] x = {
                    (int) distanceFromCenter,
                    (int) distanceFromCenter,
                    arrowWidth*3,
                    arrowWidth*3,
                    0, 
                    arrowWidth*3,
                    arrowWidth*3
                    };
            // Rotate around (0, 0)
            for (int i = 0; i < x.length; i++) {
                int x1 = x[i];
                int y1 = y[i];
                x[i] = (int) (x1 * Math.cos(-angle) - y1 * Math.sin(-angle));
                y[i] = (int) (y1 * Math.cos(-angle) + x1 * Math.sin(-angle));
            }
     
            Polygon poly = new Polygon(x, y, x.length);
            poly.translate(xcoord, ycoord);
            return poly;
        }
    Dans cet exemple, distanceFromCenter désigne la longueur de la flèche, angle désigne l'angle par rapport à l'horizontale, xcoord et ycoord les coordonnées de la tête de la flèche et arrowWidth ainsi que les expressions associées déterminent l'épaisseur de la flèche.

    A+

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

    Informations forums :
    Inscription : Octobre 2007
    Messages : 35
    Par défaut
    donc j'ai déja une méthode pour tracer des lignes
    comment pourais je faire pour avoir une box où je pourai choisir le type de lignes que je veux dessiner.
    flèche, ect...

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 901
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 901
    Billets dans le blog
    54
    Par défaut
    Une JComboBox contenant des segments "type", un DefaultListCellRenderer dont la methode paintComponent() (un DefaultListCellRenderer est un JLabel en fait) a ete surchargee pour dessiner le segment actuel (cad le segment "actif" au moment ou le renderer est appelle - le parametre value de la methode getListCellRendererComponent()).

    Quand un segment est choisi dans la combo au choix :
    - on le clone et on utilise son clone dans le dessin.
    - on cree un nouveau segment reprenant les proprietes du segment choisi (eventuellement prevoir un constructeur de copie dans la classe) et on utilise le nouveau segment dans le dessin.

    J'avais fait deux combo similaires pour le choix du Stroke (le type et l'epaisseur du trait) et cela fonctionnait tres bien. Par exemple dans mon cas :

    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
     
      /**
         * {@inheritDoc}
         */
      @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        selected = isSelected;
        if (value instanceof Stroke) {
          stroke = (Stroke) value;
        }
        else {
          stroke = null;
        }
        return this;
      }
     
      /**
         * {@inheritDoc}
         */
      @Override protected void paintComponent(Graphics graphics) {
         // Dans mon cas le rendu est delegue a une classe utilitaire car j'utilise la meme methode de rendu pour un ListCellRenderer, un TreeCellRenderer et un TableCellRenderer.
         helper.paintComponent((Graphics2D) graphics, this, stroke, selected, getStrokePaint());
       }
     
    [...]
     
      public void paintComponent(Graphics2D graphics, JComponent component, Stroke stroke, boolean selected, Paint strokePaint) {
        Dimension size = component.getSize();
        Insets insets = component.getInsets();
        int width = size.width - (insets.left + insets.right);
        int height = size.height - (insets.top + insets.bottom);
        Graphics2D g2 = (Graphics2D) graphics.create(insets.left, insets.top, width, height);
        try {
          drawComponent(g2, component, width, height, stroke, strokePaint);
          // Draw the selection border (if any).
          if (selected) {
            g2.setColor(component.getBackground());
            g2.setStroke(BORDER_STROKE);
            g2.drawRect(0, 0, width - 1, height - 1);
          }
        }
        finally {
          g2.dispose();
        }
      }
     
      /**
         * Really draw the component.
         * @param graphics The graphics context in which to paint.
         * @param width The width of the drawing area.
         * @param height The height of the drawing area.
         */
      private void drawComponent(Graphics2D graphics, JComponent component, double width, double height, Stroke stroke, Paint strokePaint) {
        if (stroke != null) {
          drawStroke(graphics, component, width, height, stroke, strokePaint);
        }
      }
     
      /**
         * Paints the stroke.
         * @param graphics The graphics context in which to draw.
         * @param width The width of the drawing area.
         * @param height The height of the drawing area.
         */
      private void drawStroke(Graphics2D graphics, JComponent component, double width, double height, Stroke stroke, Paint strokePaint) {
        Stroke oldStroke = graphics.getStroke();
        RenderingHints oldHints = graphics.getRenderingHints();
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        double margin = width / 10.0;
        line.x1 = margin;
        line.x2 = width - margin;
        line.y1 = height / 2.0;
        line.y2 = line.y1;
        graphics.setPaint(strokePaint);
        graphics.setStroke(stroke);
        graphics.draw(line);
        graphics.setStroke(oldStroke);
        graphics.setRenderingHints(oldHints);
      }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

Discussions similaires

  1. Comment contrer la "segmentation fault" ?
    Par guillaume_pfr dans le forum C
    Réponses: 15
    Dernier message: 08/08/2003, 13h43
  2. Problème de déclaration de segment avec use32
    Par le mage tophinus dans le forum Assembleur
    Réponses: 2
    Dernier message: 10/01/2003, 10h17
  3. [VB6] [Interface] Horloge 7 segments
    Par selenay dans le forum VB 6 et antérieur
    Réponses: 11
    Dernier message: 07/10/2002, 16h15
  4. [TASM] Déclarer le segment de pile
    Par cipher dans le forum x86 16-bits
    Réponses: 2
    Dernier message: 01/10/2002, 03h58
  5. angle entre 2 segments
    Par tane dans le forum Mathématiques
    Réponses: 4
    Dernier message: 25/09/2002, 16h47

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