Skip to content
Advertisement

How can we show the tooltip of a curved line (drawn with piccolo2d) only along the curve and not in the contained area?

In the sample code below (Modified TooltipExample.java from https://github.com/piccolo2d/piccolo2d.java/tree/master/examples/src/main/java/org/piccolo2d/examples), I created a curved line inside a rectangle. It appears that the area/bounds contained by this curve is not along the curve but the highlighted part in GREY. I am trying to find if there is a way we can show the tooltip of curve only along the curve. I tried resetting the underlying PBounds, but no luck. Does anyone know how to achieve this?

import org.piccolo2d.PCamera;
import org.piccolo2d.PCanvas;
import org.piccolo2d.PNode;
import org.piccolo2d.event.PBasicInputEventHandler;
import org.piccolo2d.event.PInputEvent;
import org.piccolo2d.extras.PFrame;
import org.piccolo2d.nodes.PPath;
import org.piccolo2d.nodes.PText;

import java.awt.*;
import java.awt.geom.Point2D;


/**
 * Simple example of one way to add tooltips
 *
 * @author jesse
 */
public class TooltipExample extends PFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public TooltipExample() {
        this(null);
    }

    public TooltipExample(final PCanvas aCanvas) {
        super("TooltipExample", false, aCanvas);
    }

    public void initialize() {
        final PNode n1 = PPath.createEllipse(0, 0, 100, 100);
        final PNode n2 = PPath.createRectangle(300, 200, 100, 100);

        PPath curve = new PPath.Float();
        curve.moveTo(318.0,222.0);
        curve.curveTo(318.0,222.0, 375.0,228.0, 352.0,278.0);
        curve.addAttribute("tooltip","Curve");
        curve.setPaint(Color.GRAY);

        n1.addAttribute("tooltip", "node 1");
        n2.addAttribute("tooltip", "node 2");
        getCanvas().getLayer().addChild(n1);
        getCanvas().getLayer().addChild(n2);
        getCanvas().getLayer().addChild(curve);

        final PCamera camera = getCanvas().getCamera();
        final PText tooltipNode = new PText();

        tooltipNode.setPickable(false);
        camera.addChild(tooltipNode);

        camera.addInputEventListener(new PBasicInputEventHandler() {
            public void mouseMoved(final PInputEvent event) {
                updateToolTip(event);
            }

            public void mouseDragged(final PInputEvent event) {
                updateToolTip(event);
            }

            public void mouseClicked(final PInputEvent event) {
                final Point2D p = event.getCanvasPosition();
                System.out.println(p.getX()+","+p.getY());
            }

            public void updateToolTip(final PInputEvent event) {
                final PNode n = event.getPickedNode();
                final String tooltipString = (String) n.getAttribute("tooltip");
                final Point2D p = event.getCanvasPosition();
                event.getPath().canvasToLocal(p, camera);

                tooltipNode.setText(tooltipString);
                tooltipNode.setOffset(p.getX() + 8, p.getY() - 8);
            }
        });
    }

    public static void main(final String[] argv) {
        new TooltipExample();
    }
}

enter image description here

The bigger picture: Let’s say we have many such overlapping curves passing over one another. If there is another smaller shape placed in between the huge overlapping area of these curves, I am not able to get any events for that shape. Basically I am not able to click that shape. I would appreciate any suggestions to solve this problem. Thanks in advance.

Edit 1: Adding a new picture for problem elaboration:

enter image description here

Advertisement

Answer

If the paint is null and you only have an outline of the curves, then the tooltips should work as you describe. However, If the objects are opaque then only the top object is picked. You could implement the tooltip mechanism the way it suits your scenario. For example, if you are over several objects that obscure each other you can display a combined tooltip of these objects.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement