ToolTipDemo.java  
1   /*
2    * @(#)ToolTipDemo.java 1.8 04/07/26
3    * 
4    * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
5    * 
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    * 
9    * -Redistribution of source code must retain the above copyright notice, this
10   *  list of conditions and the following disclaimer.
11   * 
12   * -Redistribution in binary form must reproduce the above copyright notice, 
13   *  this list of conditions and the following disclaimer in the documentation
14   *  and/or other materials provided with the distribution.
15   * 
16   * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
17   * be used to endorse or promote products derived from this software without 
18   * specific prior written permission.
19   * 
20   * This software is provided "AS IS," without a warranty of any kind. ALL 
21   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
27   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
28   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
29   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
30   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31   * 
32   * You acknowledge that this software is not designed, licensed or intended
33   * for use in the design, construction, operation or maintenance of any
34   * nuclear facility.
35   */
36  
37  /*
38   * @(#)ToolTipDemo.java 1.8 04/07/26
39   */
40  
41  
42  import javax.swing.*;
43  import javax.swing.event.*;
44  import javax.swing.text.*;
45  import javax.swing.border.*;
46  import javax.swing.colorchooser.*;
47  import javax.swing.filechooser.*;
48  import javax.accessibility.*;
49  
50  import java.awt.*;
51  import java.awt.event.*;
52  import java.beans.*;
53  import java.util.*;
54  import java.io.*;
55  import java.applet.*;
56  import java.net.*;
57  
58  /**
59   * ToolTip Demo
60   *
61   * @version 1.8 07/26/04
62   * @author Jeff Dinkins
63   */
64  public class ToolTipDemo extends DemoModule {
65  
66      /**
67       * main method allows us to run as a standalone demo.
68       */
69      public static void main(String[] args) {
70      ToolTipDemo demo = new ToolTipDemo(null);
71      demo.mainImpl();
72      }
73  
74      /**
75       * ToolTipDemo Constructor
76       */
77      public ToolTipDemo(SwingSet2 swingset) {
78      // Set the title for this demo, and an icon used to represent this
79      // demo inside the SwingSet2 app.
80      super(swingset, "ToolTipDemo", "toolbar/ToolTip.gif");
81  
82      // Set the layout manager.
83      JPanel p = getDemoPanel();
84      p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
85      p.setBackground(Color.white);
86  
87      // Create a Cow to put in the center of the panel.
88      Cow cow = new Cow();
89      cow.getAccessibleContext().setAccessibleName(getString("ToolTipDemo.accessible_cow"));
90  
91      // Set the tooltip text. Note, for fun, we also set more tooltip text
92      // descriptions for the cow down below in the Cow.contains() method.
93      cow.setToolTipText(getString("ToolTipDemo.cow"));
94  
95      // Add the cow midway down the panel
96      p.add(Box.createRigidArea(new Dimension(1, 150)));
97      p.add(cow);
98      }
99  
100 
101     class Cow extends JLabel {
102     Polygon cowgon = new Polygon();
103     
104     public Cow() {
105         super(createImageIcon("tooltip/cow.gif", getString("ToolTipDemo.bessie")));
106         setAlignmentX(CENTER_ALIGNMENT);
107 
108         // Set polygon points that define the outline of the cow.
109         cowgon.addPoint(3,20);    cowgon.addPoint(44,4);
110         cowgon.addPoint(79,15);   cowgon.addPoint(130,11);
111         cowgon.addPoint(252,5);   cowgon.addPoint(181,17);
112         cowgon.addPoint(301,45);  cowgon.addPoint(292,214);
113         cowgon.addPoint(269,209); cowgon.addPoint(266,142);
114         cowgon.addPoint(250,161); cowgon.addPoint(235,218);
115         cowgon.addPoint(203,206); cowgon.addPoint(215,137);
116         cowgon.addPoint(195,142); cowgon.addPoint(143,132);
117         cowgon.addPoint(133,189); cowgon.addPoint(160,200);
118         cowgon.addPoint(97,196);  cowgon.addPoint(107,182);
119         cowgon.addPoint(118,185); cowgon.addPoint(110,144);
120         cowgon.addPoint(59,77);   cowgon.addPoint(30,82);
121         cowgon.addPoint(30,35);   cowgon.addPoint(15,36);
122     }
123     
124     boolean moo = false;
125     boolean milk = false;
126     boolean tail = false;
127 
128     // Use the contains method to set the tooltip text depending
129     // on where the mouse is over the cow.
130     public boolean contains(int x, int y) {
131         if(!cowgon.contains(new Point(x, y))) {
132         return false;
133         }
134         
135         if((x > 30) && (x < 60) && (y > 60) && (y < 85)) {
136         if(!moo) {
137             setToolTipText("<html><center><font color=blue size=+2>" +
138                    getString("ToolTipDemo.moo") + "</font></center></html>");
139             moo = true;
140             milk = false;
141             tail = false;
142         }
143         } else if((x > 150) && (x < 260) && (y > 90) && (y < 145)) {
144         if(!milk) {
145             setToolTipText("<html><center><font face=AvantGarde size=+1 color=white>" +
146                    getString("ToolTipDemo.got_milk") + "</font></center></html>");
147             milk = true;
148             moo = false;
149             tail = false;
150         }
151         } else if((x > 280) && (x < 300) && (y > 20) && (y < 175)) {
152         if(!tail) {
153             setToolTipText("<html><em><b>" + getString("ToolTipDemo.tail") + "</b></em></html>");
154             tail = true;
155             moo = false;
156             milk = false;
157         }
158         } else if(moo || milk || tail) {
159         setToolTipText(getString("ToolTipDemo.tooltip_features"));
160         moo = false;
161         tail = false;
162         milk = false;
163         }
164 
165         return true;
166     }
167     }
168     
169 }
170