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
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
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; }
Merci de penser au tagquand 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
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?
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 :
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 /** @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; }
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.
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; }
Merci de penser au tagquand 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
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
en fait, je remarque que ma methode
getooltiptext
n'est jamais appelé
pourquoi?
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); } }Dans cet exemple-ci en creant et en appelant une methode appropriee qui appelle tooltipMap.put(myRect, myTooltip)...Cependant, mon problème reste tjrs le meme, je ne sais pas comment associé a mon Rectangle 2D une info bulle.
Aucun idee, tu dois mal t'y prendre qq part...en fait, je remarque que ma methode getooltiptext n'est jamais appelé
pourquoi?
Merci de penser au tagquand 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
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
Partager