How to display floating tool tip text on a polygon
Asked Answered
B

1

2

I have written a Java code to draw a polygon on an image. When I put my cursor inside the polygon it prints "Inside" otherwise "Outside". So the detection of the points inside the polygon is working fine.

But I want to implement the effect of setToolTipText inside the polygon i.e. at the time of mouse hover inside the polygon, it will show the floating text "Inside".

Similar to the effect in this image:

http://www.java2s.com/Code/Java/Swing-JFC/WorkingwithTooltipText.htm

What are the minimal changes to be made in the following code to get the desired effect?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.awt.Graphics.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

class page1 extends JFrame implements MouseListener,MouseMotionListener ,ActionListener
{
    JFrame f;
    JLabel l;
    JPanel p1;
    ImageIcon ii;
    Image img;
    int height;
    int width;
    Container c;
    int pixels[];
    PixelGrabber pg;
    JPanel panel;
    Graphics2D gg;
    Polygon pp1=new Polygon();
    boolean startHovercurrent,startHoverprev=false;


    page1()
    {
        f=new JFrame("Sample Page");
        ii=new ImageIcon("sample.jpg");
        img=ii.getImage();
        height=ii.getIconHeight();
        width=ii.getIconWidth();
        pixels=new int[ii.getIconWidth()*ii.getIconHeight()];
        pg=new PixelGrabber(img,0,0,ii.getIconWidth(),ii.getIconHeight(),pixels,0,ii.getIconWidth());
        try
        {
            pg.grabPixels();
        }
        catch(InterruptedException k)
        {
        }

        //add points of polygon
        pp1.addPoint(300,300);
        pp1.addPoint(380,300);
        pp1.addPoint(380,220);
        pp1.addPoint(300,220);


        l=new JLabel(ii,JLabel.CENTER);
        c=f.getContentPane();
        JDesktopPane desk = new JDesktopPane();
        JInternalFrame p = new JInternalFrame("Image Frame",false, false, true, false);
        JScrollPane scroll = new JScrollPane(l);
        p.setContentPane(scroll);
        p.setBounds(0, 0, 740, 600);
        desk.add(p);
        p.setVisible(true);
        l.addMouseListener(this);
        l.addMouseMotionListener(this);

        c.add(desk, BorderLayout.CENTER);
        f.setSize(1024,738);
        f.setVisible(true);

    }


    public static void main(String args[])
    {
        new page1();    
    }
    public void mouseClicked(MouseEvent me)
    {
    }
    public void mouseEntered(MouseEvent me)
    {
    }
    public void mouseExited(MouseEvent me)
    {
    }
    public void mousePressed(MouseEvent me)
    {     
    }
    public void mouseReleased(MouseEvent me)
    {   
    }
    public void mouseMoved(MouseEvent me)
    {
        boolean contain1;
        int mx,my;
        gg=(Graphics2D)l.getGraphics();         
        gg.setColor(new Color(255,0,0) );
        gg.fillPolygon(pp1);

        mx = me.getX();
        my = me.getY();

        //check if mouse cursor is inside polygon or not
        // do not print anything if next cursor position is in same state
        contain1=pp1.contains(mx,my);
        if (contain1) {
            startHovercurrent = true;
            if(startHovercurrent!=startHoverprev)
                System.out.println("Inside");

            startHoverprev=startHovercurrent;
         } 
         else {
            startHovercurrent = false;
            if(startHovercurrent!=startHoverprev)
                System.out.println("Outside");

            startHoverprev=startHovercurrent;
         }
    }
    public void mouseDragged(MouseEvent me)
    {   
    }
    public void actionPerformed(ActionEvent ae)
    {  
    }   
}
Blender answered 2/12, 2018 at 17:23 Comment(0)
W
2

For this usage, How to Use Tool Tips suggests overriding the getToolTipText() method of the enclosing JComponent. This answer outlines the approach for JMapViewer and ChartPanel. In the example below, getToolTipText() simply returns the name of any Shape that contains() the triggering mouse event. For comparison, the JLabel at window's bottom gets a conventional too tip via setToolTipText().

image

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ToolTipManager;

/**
 * @see https://mcmap.net/q/1330581/-how-to-display-floating-tool-tip-text-on-a-polygon
 * @see https://mcmap.net/q/1329279/-how-to-add-a-tooltip-to-mapmarker-in-jmapviewer
 */
public class ShapeToolTip {

    private static class ShapePanel extends JPanel {

        private final List<Shape> list = new ArrayList<>();

        public ShapePanel() {
            Polygon p = new Polygon();
            p.addPoint(500, 100);
            p.addPoint(500, 400);
            p.addPoint(200, 400);
            list.add(p);
            list.add(new Ellipse2D.Double(100, 100, 200, 200));
            ToolTipManager.sharedInstance().registerComponent(this);
        }

        @Override
        public String getToolTipText(MouseEvent e) {
            for (Shape shape : list) {
                if (shape.contains(e.getX(), e.getY())) {
                    return shape.getClass().getName();
                }
            }
            return "Outside";
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(new BasicStroke(2));
            for (Shape shape : list) {
                g2d.draw(shape);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(640, 480);
        }
    }

    private void display() {
        JFrame f = new JFrame("ShapeToolTip");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ShapePanel());
        JLabel title = new JLabel("Shape Tool Tip", JLabel.CENTER);
        title.setToolTipText("Title");
        title.setFont(title.getFont().deriveFont(Font.BOLD, 24));
        f.add(title, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ShapeToolTip()::display);
    }
}
Wrac answered 4/12, 2018 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.