Using OpenGL in Java2d seems broken.
It seems to me that the following (SSCCE provided below) should draw a
little yellow circle where the user has moved the mouse. It works fine
if I don't enable opengl, but it doesn't work when I do enable it.
What happens for me: A window appears with decoration, but its contents
are "see through". E.g. it looks like only the decoration was drawn, not
any of the contents. If I move the frame around, it appears that some
sort of double buffer is used, but it never gets updated with the
contents that I provide.
Is this a bug with:
a) my code
b) the JDK
c) The latest GeForce Go 6150 Vista 32-bit drivers from HP (downloaded
and installed yesterday).
d) Something else gone wrong?
<SSCCE filename="OpenGLTest" please-snip-in-reply="true">
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class OpenGLTest {
// To see "correct" behavior, set to false.
private static final boolean USE_OPENGL = true;
public static void main(String[] args) {
if (USE_OPENGL) {
System.out.println("Using OpenGL");
System.setProperty("sun.java2d.opengl", "True");
} else {
System.out.println("Not using OpenGL");
}
EventQueue.invokeLater(new Initter());
}
private static void draw(Graphics2D g2d, Point mousePosition) {
g2d.setPaint(Color.blue);
g2d.fill(new Rectangle(0, 0, 640, 480));
Ellipse2D.Double circle = new Ellipse2D.Double();
if (mousePosition != null) {
g2d.setPaint(Color.yellow);
circle.setFrame(mousePosition, new Dimension(10, 10));
g2d.fill(circle);
}
}
private static class Initter implements Runnable {
public void run() {
System.out.println("Constructing frame");
JFrame frame = new JFrame("Hello");
final JPanel pane = new PaintlessPanel();
pane.setPreferredSize(new Dimension(640, 480));
pane.addMouseMotionListener(new MouseUpdater(pane));
frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("Done constructing frame.");
}
private static class MouseUpdater extends MouseMotionAdapter {
private final JPanel pane;
public MouseUpdater(JPanel pane) {
this.pane = pane;
}
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse moved to: " + e.getPoint());
Graphics graphics = pane.getGraphics();
draw((Graphics2D) graphics, e.getPoint());
graphics.dispose();
}
}
private static class PaintlessPanel extends JPanel {
public void paint(Graphics g) {
System.out.println("Paint even called and ignored.");
// draw((Graphics2D) g, getMousePosition(true));
}
}
}
}
</SSCCE>
Thanks,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>