How does jframe work? Deep inside, how did he draw the material?

Usually, when I create a class, such as Customer , I give it some data fields, i.e. public int IdNumber; and some methods, i.e. public String getName(){...} . But that is pretty much the case. I cannot go beyond this and start playing with graphics - I can only manipulate and organize the data as far as the class allows.

I can’t understand what is going on inside the JFrame. Whoever wrote the JFrame class, how did they write a class that can make a window appear on the screen? What happens inside, what makes it happen? Is there any way to emulate it?

The same question applies to all graphical Java classes. I am really interested to know how this works, as it bothers me every time I use one of them.

+6
source share
3 answers

Java started with awt (Abstract Windowing Toolkit), and swing was later introduced.

In AWT , the platform event processing loop is connected, and the events are packaged in native Java classes, and one (non-parallel) event processing queue / thread processes them one by one. Swing inherits this.

In AWT, each GUI component, such as a radio button or menu item, has its own β€œpeer-to-peer” control, a component provided by the platform. There is a parallel set of java classes and their counterpart C. The java Graphics class is especially interesting, which allows you to customize the drawing of lines, rectangles, etc. It is viewed under Windows with CDC (Device Context) - presumably.

In Swing, most of the platform components are emulated, that is, recreated themselves: drawing, mouse processing, etc. Thus, the native part is simpler, say, CWnd (Window component) with a custom drawing.

Swing can provide more consistent and feature rich functionality. You might imagine that setting the backgroud color on the AWT radio button might not be possible or using HTML on a label or tooltip. Also Swing can do skinning, themes, LookAndFeels. The system looks and feels like a close imitation of platform components. Especially Swing components are more light weight , since not every component has its own peer control, which must be processed in C.

Now, SWT is a later IBM initiative implemented in eclipse to reload AWT. Not as customizable as Swing, but designed for the platform nearby.

You should forget to use AWT components, and if you do not program for Eclipse RCP as SWT as well.

So: global platform events, such as mouse clicks, redraw requests, are translated into Java events. There is a hierarchy of containers JFrame, JPanels, JScrollPanes, JComponents. The event is dispatched to processing components, for which, for example, is called paintComponent:

 @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // A later introduced class that can do more. g2.draw... } 

With JavaFX , a new player appears that IMHO is not yet fully mature, but can be used in non-production code. It allows you to create effects / animations, rotations, transformations, lights. Thus, 2D-4D rendering based on a similar rendering platform. This property is also based, so the flag does not have to be tied to a logical but Boolean property that observes and notifies changes. I need some more practical experience to imagine the optimal architecture.

+3
source

If you are interested in how Java is implemented, you should take a look at the source code. http://openjdk.java.net/projects/jdk7/ will be the beginning.

Of course, this only gives you an idea of ​​this particular implementation and does not mean that your Java is implemented in the same way.

+3
source

How does a window appear on the screen? This functionality is offered by the JVM operating system (X Window System on Linux).

At the Java level, a JFrame inherits from java.awt.Window, which has native peers provided by the embedded system .

If you really want to understand this, it is better if you try to create multiple windows using only C.

+2
source

Source: https://habr.com/ru/post/944522/


All Articles