Let's say I have a JPanel
called content with its paintComponent(g)
overridden. This panel usually paints very quickly, but sometimes (Clarification: the user generates a graphics object that can have thousands of points, which slows down the rendering. It needs to be this way and it's not part of my question) it may take a whole second to repaint due to all of the graphics stuff going on in it.
I want to paint a dot near the location of the mouse, but I want this to update quickly and not have to repaint content every time the mouse is moved.
What I tried to do was paint it on a glass pane, like this:
class LabelGlassPane extends JComponent {
public LabelGlassPane(JApplet frame) {
this.frame = frame;
}
public JApplet frame;
public void paintComponent(Graphics g) {
if(ell != null){
g2.setColor(Vars.overDot);
g2.fill(ell); //this is an ellipse field that was created in the mouesmoved() listener
}
}
}
My issue is that even though I now only update this glass pane when the mouse is moved, it's repainting all of the other components in the applet when I call repaint()
on it.
How can I paint to this glass pane (or have another way of painting) without it painting the components below?
Thanks.
EDIT: Here is a simple example. jp should not update when the mouse is moved, but it is.
public class Glasspanetest extends JApplet implements Runnable{
public void init() {
setLayout(new BorderLayout());
GraphicsPanel jp = new GraphicsPanel();
add(jp, BorderLayout.CENTER);
glass = new LabelGlassPane(this);
this.setGlassPane(glass);
glass.setVisible(true);
}
class GraphicsPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(50, 50, 50, 50);
System.err.println("Painting bottom panel");
}
}
public LabelGlassPane glass = null;
public Ellipse2D.Double ell = null;
class LabelGlassPane extends JComponent {
public LabelGlassPane(JApplet frame) {
this.frame = frame;
this.addMouseMotionListener(new MoveInfoListener());
}
public JApplet frame;
public void paintComponent(Graphics g) {
//g.setColor(Color.red);
//Container root = frame.getRootPane();
// g.setColor(new Color(255,100,100,100));
// g.fillRect(100, 100, 500, 500);
if(ell != null){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.black);
g2.fill(ell);
System.out.println("Painted glass pane");
}
//rPaint(root,g);
}
}
public class MoveInfoListener implements MouseMotionListener{
public void mouseMoved(MouseEvent e) {
ell = new Ellipse2D.Double(e.getX()-3, e.getY()-3, 6, 6);
glass.repaint();
}
public void mouseDragged(MouseEvent arg0) {}
}
public void run() {}
}