I have an algorithm that displays pretty fractals on a JPanel. I want to be able to show gridline 'crosshairs' on the fractal panel whenever a user hovers his mouse on the panel.
I have achieved this with a quick, simple method for drawing on stuff which goes like this (fractal
is the name of the fractal JPanel):
@Override
public void mouseMoved(MouseEvent e) {
Graphics g = fractal.getGraphics();
// <problematic part>
// This is computationally expensive to do. It takes a good 0.2 seconds.
// This must be done to clear 'old' crosshairs
fractal.repaint();
// </problematic part>
Color c = fractal.getColourScheme().getGridlineColour();
g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 50));
// Draw crosshairs around the mouse's current point
g.drawLine(e.getX(), 0, e.getX(), fractal.getHeight());
g.drawLine(0, e.getY(), fractal.getWidth(), e.getY());
}
As illustrated, I have to repaint() in order to clear any old crosshairs.
I have a feeling I need to paint my crosshairs on something like a Glass Pane to go on top of my JPanel - however, JPanel doesn't provide a Glass Pane. JRootPane does though - can I use this instead?
P.S.
Currently I'm painting the fractal pixel by pixel with lots of little Line2D
s. I will eventually change this to producing a BufferedImage
- in which case, would this problem even matter?