[solved] Enabling hardware acceleration (Java2D)

Hey JGO, I found this page here: http://www.m0interactive.com/archives/2008/12/30/using_hardware_acceleration_for_java2d.html

Which suggests that by enabling the 2 property’s below, I’ll enable hardware acceleration in my game and enhance my rendering.


-Dsun.java2d.opengl
-Dsun.java2d.d3d

I had a few questions to ask, before trying to implement it into my game.

Questions:
Will these, when set by ‘System.setProperty()’ truly enable hardware acceleration?
Can I enable/disable these property’s on the fly (Example JComboBox for high/low quality rendering) and have them take effect instantly?

Thanks for any feedback / info guys :slight_smile:

Look into GLG2D, works like graphics2D but behind the scenes it is hardware accelerated.

I haven’t used it but I’ve heard good things.

To answer your question-
You cannot turn it on and off at runtime, it is a flag. As far as I can see it would cause it to run a bit faster, though I doubt it would be as fast as GLG2D, and it could lead to problems when you distribute your game.

Stop being silly and hard headed and learn something new! :slight_smile:

Swing was not made for games.

I can’t seem to get GLG2D working for anything lol, even overdid myself and imported EVERY .jar and .dll #RAGE

Just go use Libgdx if you want to make a game quickly, or use LWJGL if you need hardcore low level control (I doubt you would need that).

I’d like to get everything I can get out of Java2D before moving onto anything else as Java2D provides everything I need for my game with no flaws.

I’ve installed the GLG2D.jar file and it keeps spamming more dependencies are required, even when I added EVERY SINGLE .jar and .dll it’s still denying me?

I’ll make a thread on how to get GLG2D working, thanks guys.

Besides everything.

Given how utterly simple it is to use LibGDX Java2D literally serves no purpose in game development.

LibGDX takes 5 minutes to setup first time, 7 lines of code and you can draw your first image. A few more lines and you can give it an AABB and a way to move it using keys.

LibGDX the way it is the now is easy street, I would honestly go as far as saying that it is a viable choice for a university lecturer when it comes to teaching first year students.

[quote=“TehJavaDev,post:1,topic:50704”]
just add -Dsun.java2d.opengl=true and -Dsun.java2d.opengl=false or the other way around and see what it does to your game if you use Graphics2D. thats what it is about. either you use this API or you choose another, or what do you mean by “implement” ?

[quote=“TehJavaDev,post:1,topic:50704”]
afaik, switching them at runtime is not supported, also using System.setProperty() is not always working, just use the jvm args and your set.

you can check what implementation is behind Graphics2D at runtime tho by looking into :

graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
primaryDisplay = graphicsEnvironment.getDefaultScreenDevice();
graphicsConfiguration = primaryDisplay.getDefaultConfiguration();
bufferCapabilities = graphicsConfiguration.getBufferCapabilities();

buffer-caps can be a D3DGraphicsConfig or WGLGraphicsConfig or some proxy implementation.

you can pass the caps directly into createBufferStrategy. at least now you know what’s going on and can ask java the right stuff.

edit

just to complete this : after you create the bufferstrategy, you can grab the Graphics2D reference by :

// an overwritten Canvas class around the constructor :

public class Canvas extends java.awt.Canvas
{
  [...]

  public Canvas()
  {
    BufferCapabilities caps = primaryDisplay.getDefaultConfiguration().getBufferCapabilities();
    createBufferStrategy(2,caps);
    buffer = getBufferStrategy();
    g = getDefaultGraphics((Graphics2D)buffer.getDrawGraphics()); // use this to paint your stuff
  }

  [...]
}


the downside of using Graphics2D like that, it’s not possible to access the “data-buffer” of a “buffered-image” cos’ there is simply none. so reading pixels and setting one by one is not gonna fly (too well).

reading pixels works, many to fail tho’ :

private void readBackBuffer()
{
  if(buffer instanceof BltBufferStrategy) // the buffer from above
  {
    BltBufferStrategy _buffer = (BltBufferStrategy)buffer;
    try
    {
      Method m = _buffer.getClass().getSuperclass().getDeclaredMethod("getBackBuffer");
      m.setAccessible(true);
      Object v = m.invoke(_buffer);

      if(v instanceof VolatileImage)
      {
          backBuffer = (VolatileImage)v;  // at least a Image, we get width height etc.
      }
    }
    catch(NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
    {
      // ...
    }
  }
}

“showing” such buffer should be something like :

public void swapBuffers()
{
  if(!buffer.contentsLost()) buffer.show();
}

finally you when you resize/reshape things you might want check if the buffers graphics2D instance changed and update yours in case.

Thank you basil, I’ll try making a test case out of the code you posted and see what happens.

Thanks again :slight_smile:

EDIT:

Now it seems to be working ^_______^


OpenGL pipeline enabled for default config on screen 0

awesome :slight_smile: