Path: blob/master/src/demo/share/jfc/SwingSet2/ToolTipDemo.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* ToolTip Demo51*52* @author Jeff Dinkins53*/54public class ToolTipDemo extends DemoModule {5556/**57* main method allows us to run as a standalone demo.58*/59public static void main(String[] args) {60ToolTipDemo demo = new ToolTipDemo(null);61demo.mainImpl();62}6364/**65* ToolTipDemo Constructor66*/67public ToolTipDemo(SwingSet2 swingset) {68// Set the title for this demo, and an icon used to represent this69// demo inside the SwingSet2 app.70super(swingset, "ToolTipDemo", "toolbar/ToolTip.gif");7172// Set the layout manager.73JPanel p = getDemoPanel();74p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));75p.setBackground(Color.white);7677// Create a Cow to put in the center of the panel.78Cow cow = new Cow();79cow.getAccessibleContext().setAccessibleName(getString("ToolTipDemo.accessible_cow"));8081// Set the tooltip text. Note, for fun, we also set more tooltip text82// descriptions for the cow down below in the Cow.contains() method.83cow.setToolTipText(getString("ToolTipDemo.cow"));8485// Add the cow midway down the panel86p.add(Box.createRigidArea(new Dimension(1, 150)));87p.add(cow);88}899091class Cow extends JLabel {92Polygon cowgon = new Polygon();9394public Cow() {95super(createImageIcon("tooltip/cow.gif", getString("ToolTipDemo.bessie")));96setAlignmentX(CENTER_ALIGNMENT);9798// Set polygon points that define the outline of the cow.99cowgon.addPoint(3,20); cowgon.addPoint(44,4);100cowgon.addPoint(79,15); cowgon.addPoint(130,11);101cowgon.addPoint(252,5); cowgon.addPoint(181,17);102cowgon.addPoint(301,45); cowgon.addPoint(292,214);103cowgon.addPoint(269,209); cowgon.addPoint(266,142);104cowgon.addPoint(250,161); cowgon.addPoint(235,218);105cowgon.addPoint(203,206); cowgon.addPoint(215,137);106cowgon.addPoint(195,142); cowgon.addPoint(143,132);107cowgon.addPoint(133,189); cowgon.addPoint(160,200);108cowgon.addPoint(97,196); cowgon.addPoint(107,182);109cowgon.addPoint(118,185); cowgon.addPoint(110,144);110cowgon.addPoint(59,77); cowgon.addPoint(30,82);111cowgon.addPoint(30,35); cowgon.addPoint(15,36);112}113114boolean moo = false;115boolean milk = false;116boolean tail = false;117118// Use the contains method to set the tooltip text depending119// on where the mouse is over the cow.120public boolean contains(int x, int y) {121if(!cowgon.contains(new Point(x, y))) {122return false;123}124125if((x > 30) && (x < 60) && (y > 60) && (y < 85)) {126if(!moo) {127setToolTipText("<html><center><font color=blue size=+2>" +128getString("ToolTipDemo.moo") + "</font></center></html>");129moo = true;130milk = false;131tail = false;132}133} else if((x > 150) && (x < 260) && (y > 90) && (y < 145)) {134if(!milk) {135setToolTipText("<html><center><font face=AvantGarde size=+1 color=#D2691E>" +136getString("ToolTipDemo.got_milk") + "</font></center></html>");137milk = true;138moo = false;139tail = false;140}141} else if((x > 280) && (x < 300) && (y > 20) && (y < 175)) {142if(!tail) {143setToolTipText("<html><em><b>" + getString("ToolTipDemo.tail") + "</b></em></html>");144tail = true;145moo = false;146milk = false;147}148} else if(moo || milk || tail) {149setToolTipText(getString("ToolTipDemo.tooltip_features"));150moo = false;151tail = false;152milk = false;153}154155return true;156}157}158159}160161162