Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using OpenGL in Java2d seems broken.

12 views
Skip to first unread message

Daniel Pitts

unread,
Apr 27, 2011, 11:47:52 AM4/27/11
to
To: comp.lang.java.gui
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/>

---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Daniele Futtorovic

unread,
Aug 11, 2008, 3:27:39 PM8/11/08
to
On 11/08/2008 18:21, Daniel Pitts allegedly wrote:
> It seems to me that the following (SSCCE [snipped] 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.

Works for me.

$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

Win32 XP - ATI Radeon HD 2600 Pro

--
DF.

RedGrittyBrick

unread,
Aug 11, 2008, 4:17:22 PM8/11/08
to
Daniel Pitts wrote:
> 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.
>

I get the see-through window you describe.
No odd effects when I move the frame around.
If I resize the frame it turns opaque white.
When releasing the resize handle, it turns slightly darker off-white!
It stays that way when dragged or when resized again.

Athlon 64 x2
Windows Vista Home Premium 32-bit. SP1.
Nvidia GeForce 8600 GT.
Nvidia driver 7.15.11.6371

--
RGB

Knute Johnson

unread,
Aug 11, 2008, 5:46:38 PM8/11/08
to

I'm running 1.6.0_10-rc and I see a very slow yellow circle following
the mouse pointer with OpenGL enabled. Disabled it works fine and
refreshes much faster.

Win XP SP 3
1.6.0_10-rc
Nvidia NVS 285 (with the latest drivers)

I know that a lot of work is being done on rendering in _10. I even
found a bug related to a pixel shader that appears on NVidia cards
similar to mine.

Do you actually need OpenGL or are you trying to improve performance?
There are a lot of issues with different video cards, OpenGL and the d3d
pipeline. Below is the program I used to find a bug in the new _10
pixel shader for my video card. I found that running the program on an
older version of _10 worked almost twice as fast as a later version.

Anyway, if performance is what you are trying to get, run the program
below without and command line options and then with
-Dsun.java2d.opengl=True and then with -Dsun.java2d.d3d=False and see
what sort of speeds you get. Mine are 290, 60 and 450 respectively.

The important thing to note here is that this program tests
AffineTranform rotations. Some other 2D effects may run much faster or
slower. And different hardware will affect this in unpredictable ways.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;

public class test2 extends Canvas implements Runnable {
volatile Thread thread;
volatile BufferStrategy bs;
double angle;
long then = System.currentTimeMillis();
int n;
double rate;

public test2() {
setIgnoreRepaint(true);
setPreferredSize(new Dimension(400,300));

addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
if (bs == null) {
createBufferStrategy(2);
bs = getBufferStrategy();
System.out.println(bs);
}
}
});
}

public void start() {
then = System.currentTimeMillis();
thread = new Thread(this);
thread.start();
}

public void stop() {
thread.interrupt();
}

public void run() {
while (!thread.interrupted()) {
render();
}
}

public void render() {
do {
do {
int w = getWidth();
int h = getHeight();

Graphics2D g = (Graphics2D)bs.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);

AffineTransform at = g.getTransform();

angle += 0.001;
g.rotate(angle,w/2,h/2);
g.setColor(Color.BLUE);
g.fillRect(w/2 - 100,h/2 - 100,200,200);

if (++n % 100 == 0) {
long now = System.currentTimeMillis();
long time = now - then;
then = now;
rate = 100000.0 / time;
}

g.setTransform(at);
g.setColor(Color.RED);
g.drawString(String.format("%3.1f",rate),10,10);

g.dispose();
} while (bs.contentsRestored()) ;
bs.show();
} while (bs.contentsLost()) ;
}

public static void main(String[] args) {

final test2 t2 = new test2();
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
t2.start();
}
public void windowClosing(WindowEvent we) {
t2.stop();
f.dispose();
}
});

f.add(t2,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Thomas A. Russ

unread,
Aug 11, 2008, 5:14:47 PM8/11/08
to
Daniel Pitts <newsgroup....@virtualinfinity.net> writes:

> 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.
>

> 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?

Probably not a.

I just tested this on a Mac with Java 1.4, 1.5 and 1.6.
They all worked both with and without enabling OpenGL.

--
Thomas A. Russ, USC/Information Sciences Institute

Daniel Pitts

unread,
Aug 11, 2008, 6:27:59 PM8/11/08
to
Knute Johnson wrote:
> Daniel Pitts wrote:
>> 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?
>>
>
> I'm running 1.6.0_10-rc and I see a very slow yellow circle following
> the mouse pointer with OpenGL enabled. Disabled it works fine and
> refreshes much faster.
>
> Win XP SP 3
> 1.6.0_10-rc
> Nvidia NVS 285 (with the latest drivers)
>
> I know that a lot of work is being done on rendering in _10. I even
> found a bug related to a pixel shader that appears on NVidia cards
> similar to mine.
>
> Do you actually need OpenGL or are you trying to improve performance?
> There are a lot of issues with different video cards, OpenGL and the d3d
> pipeline. Below is the program I used to find a bug in the new _10
> pixel shader for my video card. I found that running the program on an
> older version of _10 worked almost twice as fast as a later version.
Actually, I found that Full Screen Exclusive Mode with Page Flipping
wasn't working without out it, but appeared to be working (somewhat)
with OpenGL enabled. Of course, working is relative (setting up the page
flipping didn't throw an exception with OpenGL, but nothing was rendered).

>
> Anyway, if performance is what you are trying to get, run the program
> below without and command line options and then with
> -Dsun.java2d.opengl=True and then with -Dsun.java2d.d3d=False and see
> what sort of speeds you get. Mine are 290, 60 and 450 respectively.
>
> The important thing to note here is that this program tests
> AffineTranform rotations. Some other 2D effects may run much faster or
> slower. And different hardware will affect this in unpredictable ways.

Well, it should be configurable by the user in the end, I suppose. I
was actually just working on a skeleton for a few "fun apps". and
thought it would be need to enable OpenGL rendering with them. Speed is
a concern, so perhaps I should just accept the standard pipeline as
"Fast Enough", or find some other (preferably platform-independent)
library for graphics rendering.

Thanks for the replies everyone.

Daniele Futtorovic

unread,
Aug 11, 2008, 7:14:44 PM8/11/08
to
On 11/08/2008 23:14, Thomas A. Russ allegedly wrote:
> Daniel Pitts <newsgroup....@virtualinfinity.net> writes:
>
>> 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.
>>
>> 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?
>
> Probably not a.

Nor b).

Would seem like Vista's the culprit. Now who'd have thought? ;)

--
DF.

0 new messages