I have a program that draws coordinates to a screen. I need to add functionality that displays a tooltip with its location when hovering over each point. However, I can't get it to work properly and I'm not sure why.
Coordinate class:
public class Coordinate extends Ellipse2D.Double {
private Point point;
private String name;
private int radius;
public Coordinate(Point point, String name, int radius) {
super(point.getX(), point.getY(), radius, radius);
this.point = point;
this.name = name;
this.radius = radius;
}
}
Grid class:
protected void paintComponent(Graphics graphics) {
initOrigin();
super.paintComponent(graphics);
graphics.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
// draw Y axis
graphics.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
if (coordinates != null) {
drawCoordinates(graphics);
}
}
protected void drawCoordinates(Graphics graphics) {
System.out.println(coordinates.toString());
for (int i = 0; i < this.coordinates.size(); i++) {
int radius = coordinates.get(i).getRadius();
int x = (int) Math.round(coordinates.get(i).getPoint().x * X_SCALING) + originX;
int y = (int) Math.round(coordinates.get(i).getPoint().y * -1 * Y_SCALING) + originY; // flip bit to get vertical coordinates in proper place
System.out.println(x);
System.out.println(y);
graphics.fillOval(x - radius, y - radius, radius * 2, radius * 2);
setToolTipText(coordinates.get(i).toString());
}
}
@Override
public String getToolTipText(MouseEvent event) {
for (Coordinate coordinate : coordinates) {
if (coordinate.contains(event.getPoint())) {
return coordinate.toString();
}
}
return null;
}
I'm setting the tooltip in the drawCoordinates()
method and overriding the getToolTip()
method as well but nothing is popping up. Obviously I'm missing something but I'm not sure where to start. Can anyone point me in the right direction?