aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using glass pane for faster animation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using glass pane for faster animation" Watch "Using glass pane for faster animation" New topic
Author

Using glass pane for faster animation

Scott Cook
Greenhorn

Joined: Aug 01, 2006
Posts: 14
Hi!
I'm writing a survey program that plots 20000+ x/y coordinates and needs to have a "zoom" rectangle that can be drawn by dragging the mouse. If the entire screen is redrawn each time the rectangle updates, it takes too long and the rectangle drags behind the mouse. I'm trying to fix this problem by drawing the zoom rectangle on the glass pane, but I'm having 2 problems:

1) Every time the glass pane updates, the Component Pane updates as well (this negates the whole point of using the glass pane in this case)

2) If I force the Component Pane to not update (using an if statement), the glass pane overpaints the component pane with a white background. I can't seem to force the glass pane to have a clear background.

My understanding is that glass panes are supposed be non-opaque and drawn independently of the component pane. What am I doing wrong here?

Here is some test code I'm using to isolate the problem:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GlassPaneTest extends JFrame {

Point[] points;
final static int numPoints = 20000;
final static int plotSize = 500;
protected Rectangle zoomRectangle; //just testing the draw part, doesn't actually zoom
protected GlassPane glassPane;
protected boolean dragging;

public GlassPaneTest(){
setTitle("Glass Pane Test");
points = new Point[numPoints];
for (int i=0;i<points.length;i++){ //load up a bunch of points to draw
points[i] = new Point((int)(Math.random()*(plotSize-5)),(int)(Math.random()*(plotSize-5)));
}
glassPane = new GlassPane();
glassPane.setOpaque(false);
}

public static void main(String[] args) {
GlassPaneTest gpt = new GlassPaneTest();
gpt.go();
}

public void go() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//make draw panel
DrawPane dp = new DrawPane();
getContentPane().add(dp);

//add mouse listeners to JFrame
getContentPane().addMouseMotionListener( new MouseMotionListener(){
public void mouseMoved(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {
dragging = true;
zoomRectangle.add(e.getX(),e.getY());
glassPane.repaint();
}
});
getContentPane().addMouseListener( new MouseListener(){
public void mouseClicked(MouseEvent e) {
zoomRectangle = null;
glassPane.repaint();
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
dragging = false;
zoomRectangle = null;
glassPane.repaint();
}
public void mousePressed(MouseEvent e) {
zoomRectangle = new Rectangle(e.getX(),e.getY(),1,1);
glassPane.repaint();
}
});

//set glass pane
setGlassPane(glassPane);
((JComponent)getGlassPane()).setOpaque(false);
getGlassPane().setVisible(true);

//make visible
setSize(plotSize,plotSize);
setVisible(true);
}

protected class DrawPane extends JPanel {
public void paintComponent(Graphics g){
if(!dragging) { //don't refresh while drawing zoom rectangle
g.setColor(Color.black);
g.fillRect(0,0,this.getWidth(),this.getHeight());

//draw a bunch of green points
g.setColor(Color.green);
for (int i=0;i<numPoints;i++){
g.fillOval((int)points[i].getX(),(int)points[i].getY(),5,5);
}
}
}
}

protected class GlassPane extends JComponent{
public void paintComponent(Graphics g){
//draw zoom rectangle
g.setColor(Color.magenta);
if (zoomRectangle!=null)
g.drawRect((int)zoomRectangle.getX(),(int)zoomRectangle.getY(),
(int)zoomRectangle.getWidth(),
(int)zoomRectangle.getHeight());

}
}
}


Thanks!
Scott
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using glass pane for faster animation