1

While java programs are platform independent, the JVM itself is platform dependent. I am interested in knowing how Java draws the application GUI (buttons and text) on the screen.

Under Windows, control objects such as buttons are usually created using a window(from user32.dll) or a rectangular region(from gdi32.dll), text is drawn later to the corresponding window/region handle using the provided user32/gdi32 text draw functions.

I tried running a simple two button Java gui application using swing and hooking the majority of create region/window and text draw functions from gdi32.dll and user32.dll, but so far, it seems the Java program is just using these native dlls for drawing the main window frame only.

Does Java.exe uses some other dlls to draw buttons and other controls on the screen ? And if so, why is this case when there are already available native dlls for drawing ?

Ibrahim
  • 203
  • 2
  • 11
  • Please read this http://stackoverflow.com/questions/2306190/java-desktop-application-swt-vs-swing and http://stackoverflow.com/questions/7358775/java-gui-frameworks-what-to-choose-swing-swt-awt-swingx-jgoodies-javafx – neohope Sep 06 '16 at 05:12
  • I read both but still cannot find an answer, I am not interested on the high level implementation of the controls, I know that swing is not using the provided high level windows controls(buttons,menus..etc), I am interested on the basic geometric building blocks of these controls, for instance if we have a rectangular button on the screen, the pixels of the rectangle corresponding to the button boundary has to be drawn on the screen at some point. gdi32 and user32 are core windows components, and my thought is that they have to be used at some point for drawing the pixels on the screen. – Ibrahim Sep 06 '16 at 05:32
  • http://openjdk.java.net/ – neohope Sep 06 '16 at 06:01
  • `java.exe` doesn’t draw anything; it’s just a launcher for the JVM, which also doesn’t draw anything, but provides an infrastructure for executing Java code, which may use native libraries, so you have to address the particular code and libraries for your question. Since Swing uses pluggable look & feels, there isn’t a single answer for Swing—it depends on the Look&Feel… – Holger Sep 06 '16 at 15:05

2 Answers2

4

On Windows Java2D uses Direct3D renderer to draw primitives by default. You may disable it by specifying -Dsun.java2d.d3d=false. In this case GDI renderer will be used.

Add -Dsun.java2d.trace=log,verbose option to trace which Java2D primitives are called in your Swing application.

apangin
  • 92,924
  • 10
  • 193
  • 247
  • But what about swing ? is it using Java2D to draw the high level GUI components such as buttons ? I tried running a swing button example gui with -Dsun.java2d.d3d=false but couldn't trap any GDI calls to draw the button rectangle. – Ibrahim Sep 07 '16 at 20:32
  • 1
    @Ibrahim You probably trap the wrong function. Try [`SetDIBitsToDevice`](https://msdn.microsoft.com/library/windows/desktop/dd162974(v=vs.85).aspx). – apangin Sep 07 '16 at 21:49
0

From the Wikipedia Article:

Unlike AWT components, Swing components are not implemented by platform-specific code. Instead, they are written entirely in Java and therefore are platform-independent. The term "lightweight" is used to describe such an element.

J Earls
  • 1,792
  • 8
  • 12
  • I still don't get it, Java code is converted to Java byte code, which in turn is given to the JVM, but the JVM is platform dependent and needs to use the OS to draw the components on the screen – Ibrahim Sep 06 '16 at 05:11
  • Yes. But the only native code that Swing uses is basic drawing code. widgets and all higher level visual abstractions are created in java, not using native code. – J Earls Sep 06 '16 at 05:12
  • Yeah, I am interested in this basic native code, so far it doesn't seem that Java is using any basic geometric native code for drawing button rectangles and other controls. – Ibrahim Sep 06 '16 at 05:19
  • I can't answer that. Swing uses AWT `Container` objects as its base drawing canvas, and then _(I assume)_ does basic `Graphics` operations on top of that. So it will use whatever native code is used by the drawing primitives in the AWT `Graphics` implementation. – J Earls Sep 06 '16 at 05:27