0

I'm working on a project using Java in an embedded system. I have drivers for the screen and touch input, and a virtual keyboard for text input. My screen driver has a Graphics2D object you can draw into and a repaint(Rectangle) method for updating. Similarly, the touch driver is capable of generating MouseEvents for listeners.

Right now I'm working with drawing directly to the screen. This is fine, but what I really want is to allow the user of my library to create a JFrame and use normal Swing elements. However, the default GraphicsEnvironment is headless, since Java doesn't see my EInkDriver class or my TouchDriver class.

I'm aware that there are ways to call a component's paint method to get the image; however this has problems when you're painting a Container, and I'd also like to present the user with a GraphicsConfiguration so they can write something like JFrame frame = new JFrame(getEInkGraphicsConfiguration()); and carry on as if they were writing a desktop Swing app.

Given these capabilities that I already have, how would I go about creating a GraphicsDevice or GraphicsEnvironment that would allow me to use Swing in my project?

Ryan Kennedy
  • 3,275
  • 4
  • 30
  • 47

1 Answers1

1

I took a very short look at the abstract class GraphicsEnvironment, and guess, one may derive ones own class, and use the "java.awt.graphicsenv" key of line 64 to register it.

Fortunately there is an abstract class, and the code is little.


Thereafter

The following code from GraphicsEnvironment shows:

  1. System properties (you may set) that are used for headless or not.
  2. The environment setting DISPLAY.

Maybe you can use this info.

                        String osName = System.getProperty("os.name");
                        if (osName.contains("OS X") && "sun.awt.HToolkit".equals(
                                System.getProperty("awt.toolkit")))
                        {
                            headless = defaultHeadless = Boolean.TRUE;
                        } else {
                            headless = defaultHeadless =
                                Boolean.valueOf(("Linux".equals(osName) ||
                                                 "SunOS".equals(osName) ||
                                                 "FreeBSD".equals(osName) ||
                                                 "NetBSD".equals(osName) ||
                                                 "OpenBSD".equals(osName)) &&
                                                 (System.getenv("DISPLAY") == null));
                        }
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks for the quick response, but I'm more interested in the specifics of "deriving one's own class." If I can derive the class then I can use it by calling `JFrame(GraphicsConfiguration)`. However I'm stuck as to how to actually derive that class. I was thinking of overwriting `GraphicsDevice.getFullScreenWindow()` but I can't create a `Window` without a `GraphicsEnvironment` leaving me with a chicken-and-the-egg problem. – Ryan Kennedy Dec 15 '12 at 20:39
  • I did not really look into it, no guarantee, but I thought of the setting `java.awt.graphicsenv=my.packge.MyGraphicsEnvironmentChild`. – Joop Eggen Dec 15 '12 at 21:28
  • I understood that part... it doesn't explain how to actually implement `my.packge.MyGraphicsEnvironmentChild` though. If I could implement that class then I wouldn't need to set that property since I could just call `JFrame(new MyGraphicsEnvironmentChild())`. Your answer assumes I can implement this class, which I can't--that's what my question is asking. So unfortunately your answer doesn't help. – Ryan Kennedy Dec 15 '12 at 21:31