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 :

Mettre une info bulle Tooltiptext sur un rectangle?


Sujet :

Graphisme Java

  1. #1
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 88
    Points : 49
    Points
    49
    Par défaut Mettre une info bulle Tooltiptext sur un rectangle?
    J'aimerai savoir s'il est possible de mettre une infobulle sur un rectangle2D.


    Car je ne vois aucune méthode pour le faire.

    Voir meme sur toute autre objet.

    Merci

    [ Sujet déplacé depuis le forum java par Viena ]
    Les Règles du Forum

  2. #2
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 852
    Points : 22 869
    Points
    22 869
    Billets dans le blog
    51
    Par défaut
    Pour la methode la plus simple il faut coupler ca a la methode String getToolTipText(MouseEvent event) du composant dans lequel tu affiches tes objets vectoriels.

    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
     
    Public class MyCanvas extends JPanel {
     
    /** Creates a new instance.
    */
    public MyCanvas {
      super();
      // By default panels do not emmit tooltips.
      ToolTipManager.sharedInstance().registerComponent(this);
      ...
    }
     
    /** @inheritDoc
    */
    @Override public String getToolTipText(MouseEvent event) {
      String result = null;
      if (myRect.contains(event.getX(), event.getY()) {
        result = ....
      }
      return result;
    }

  3. #3
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 88
    Points : 49
    Points
    49
    Par défaut
    merci,

    mais alors maintenant, comment je fait pour mettre une infobulle au rectangle?

    La, j'ai tout pour capturer l'info bulle s'il y en a, mais pour le mettre?

  4. #4
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 852
    Points : 22 869
    Points
    22 869
    Billets dans le blog
    51
    Par défaut
    Euh je ne saisisie pas trop là.... Tu peux mettre n'importe quel texte/ce que tu veux une fois que la méthode getToolTipText(MouseEvent event) est appelée. Genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    /** @inheritDoc 
    */ 
    @Override public String getToolTipText(MouseEvent event) { 
      String result = null; 
      if (myRect.contains(event.getX(), event.getY()) { 
        result = "Je suis dans mon rectangle (" + rect.x + ", " + rect.y + ")  " + rect.width + " x " + rect.height + ".");
        // Utiliser un StringBuilder/StringBuffer ou un MessageFormat pour construire la chaine.
        // Ca evitera de creer -potentiellement- tout un tas d'objet temporaires.
      } 
      return result; 
    }
    Après pour associder une infobulle à un rectangle, tu as plusieurs solutions. Il t'es possible d'utiliser une Map<Rectangle, String> par exemple ou alors de te créer une sous-classe étendant Rectangle qui contient un membre description.

    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
     
    public class SelfDescritiveRectangle extends Rectangle {
      private String description;
     
      /** Gets a description of this rectangle.
       * @return a <code>String</code>, can be <code>null</code>.
       * @see #setDescription
       */
      public String getDescription() {
        return description;
      }
     
     ....
    }
     
    ...
     
     
     
    /** @inheritDoc 
    */ 
    @Override public String getToolTipText(MouseEvent event) { 
      String result = null; 
      if (myRect.contains(event.getX(), event.getY()) { 
        if (myRect instanceof SelfDescritiveRectangle) {
          result = ((SelfDescritiveRectangle)myRect).getDescription();
        }
        else {
           ...
         }
      } 
      return result; 
    }
    Ou de te créer une classe Sprite délégant sa forme à une Shape (pas d"héritage direct de Rectangle donc) et contenant une description. ........... etc.

  5. #5
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 88
    Points : 49
    Points
    49
    Par défaut
    Je ne comprend pas ce que tu veux dire.

    J'ai un constructeur dans lequel j'ai rajouté la ligne

    ToolTipManager.sharedInstance().registerComponent(this);

    Et j'ai implémenté la fonction

    public String getToolTipText(MouseEvent event) {
    String result = null;
    if (myRect.contains(event.getX(), event.getY()) {
    result = ....
    }
    return result;
    }


    Cependant, mon problème reste tjrs le meme, je ne sais pas comment associé a mon Rectangle 2D une info bulle.

    je cré un rectangle:

    Rectangle2D rectangle2D = new Rectangle2D.Double(20, 20, 40, 40);

    mais maintenant, je ne trouve aucune methode me permettant d'associer à ce rectangle une infobulle comme settooltiptext....

    As tu un exemple sous la main que je pourrai exécuté?

    Merci

  6. #6
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 88
    Points : 49
    Points
    49
    Par défaut
    en fait, je remarque que ma methode
    getooltiptext
    n'est jamais appelé

    pourquoi?

  7. #7
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 852
    Points : 22 869
    Points
    22 869
    Billets dans le blog
    51
    Par défaut
    As tu un exemple sous la main que je pourrai exécuté?

    Mais tres certainement. Voici un exemple utilisant une map pour stocker les infobulles associes a chacun des rectangles :

    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
    114
    115
    116
    117
    118
     
    package test;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    /**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class TestPanel extends JPanel {
      public static final int MAX_RECTANGLE_NUMBER = 10;
      public static final int MIN_RECTANGLE_NUMBER = 1;
      public static final int DEFAULT_WIDTH = 400;
      public static final int DEFAULT_HEIGHT = 400;
      private static final Color[] COLORS = {
                                            Color.BLACK, Color.BLUE, Color.CYAN,
                                            Color.DARK_GRAY,
                                            Color.GRAY, Color.GREEN,
                                            Color.LIGHT_GRAY,
                                            Color.MAGENTA, Color.ORANGE, Color.PINK,
                                            Color.RED,
                                            Color.WHITE, Color.YELLOW
      };
      private static Stroke DEFAULT_STROKE = new BasicStroke(1.0f);
     
      private java.util.List<Rectangle> rectangleList = new LinkedList<Rectangle>();
      private Map<Rectangle, String> tooltipMap = new HashMap<Rectangle, String>();
      private Map<Rectangle, Color> foregroundMap = new HashMap<Rectangle, Color>();
      private Map<Rectangle, Color> backgroundMap = new HashMap<Rectangle, Color>();
     
      /** Creates a new instance.
       */
      public TestPanel() {
        super();
        setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
        Random random = new Random();
        // Create rectangles.
        int rectangleNumber = Math.max(MIN_RECTANGLE_NUMBER,
                                       random.nextInt(MAX_RECTANGLE_NUMBER));
        System.out.println("There are " + rectangleNumber + " rectangle");
        for (int i = 0; i < rectangleNumber; i++) {
          int x = random.nextInt(DEFAULT_WIDTH);
          int y = random.nextInt(DEFAULT_HEIGHT);
          int width = random.nextInt(DEFAULT_WIDTH - x);
          int height = random.nextInt(DEFAULT_HEIGHT - y);
          Rectangle rectangle = new Rectangle(x, y, width, height);
          rectangleList.add(rectangle);
          String tooltip = "This is the tooltip for rectangle " + i;
          tooltipMap.put(rectangle, tooltip);
          int foregroundIndex = random.nextInt(COLORS.length);
          foregroundMap.put(rectangle, COLORS[foregroundIndex]);
          int backgroundIndex = random.nextInt(COLORS.length);
          backgroundMap.put(rectangle, COLORS[backgroundIndex]);
        }
        // Allow this panel to display tooltips.
        ToolTipManager.sharedInstance().registerComponent(this);
      }
     
      /** {@inheritDoc}
       */
      @Override protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        for (Rectangle rectangle : rectangleList) {
          // Paint inside rectangle.
          g2.setColor(backgroundMap.get(rectangle));
          g2.fill(rectangle);
          // Paint outside.
          g2.setStroke(DEFAULT_STROKE);
          g2.setColor(foregroundMap.get(rectangle));
          g2.draw(rectangle);
        }
      }
     
      /** {@inheritDoc}
       */
      @Override
      public String getToolTipText(MouseEvent event) {
        String result = null;
        // Search from last (topmost) to first (undermost) rectangle.
        for (ListIterator<Rectangle> it = rectangleList.listIterator(rectangleList.
            size()); it.hasPrevious(); ) {
          Rectangle rectangle = it.previous();
          if (rectangle.contains(event.getX(), event.getY())) {
            result = tooltipMap.get(rectangle);
            break;
          }
        }
        return result;
      }
     
      /** Self-test main.
       * @param args Arguments from the command line.
       */
      public static void main(String ...args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new TestPanel(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
      }
    }
    Cependant, mon problème reste tjrs le meme, je ne sais pas comment associé a mon Rectangle 2D une info bulle.
    Dans cet exemple-ci en creant et en appelant une methode appropriee qui appelle tooltipMap.put(myRect, myTooltip)...

    en fait, je remarque que ma methode getooltiptext n'est jamais appelé
    pourquoi?
    Aucun idee, tu dois mal t'y prendre qq part...

  8. #8
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 88
    Points : 49
    Points
    49
    Par défaut
    Merci pour cet exemple instructif pour moi.

    En fait j'ai réussi maintenant, ma methode getooltiptext etait pas appelé car j'avais oublié des truc dans mon constructeur :p

    Merci

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

Discussions similaires

  1. Mettre une info-bulle en couleur
    Par Gilles BILLARD dans le forum VB.NET
    Réponses: 4
    Dernier message: 25/10/2012, 19h10
  2. plugin msDropDown, comment mettre une info bulle
    Par philou8 dans le forum jQuery
    Réponses: 1
    Dernier message: 18/05/2011, 23h33
  3. [JavaScript]Délais sur une info bulle
    Par YAMKI dans le forum Général JavaScript
    Réponses: 10
    Dernier message: 05/02/2007, 15h13
  4. [MySQL] reprendre les infos d'une bd pour mettre dans une info bulle
    Par luciedoudou dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 19/01/2007, 13h55
  5. probleme sur une info bulle avec onMouseOver
    Par pouss dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 08/02/2006, 09h40

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