Difference between AWT and Swing in component rendering

8

What are the main differences between the Swing and AWT interface building libraries, regarding the way components are rendered and performance rendered?

    
asked by anonymous 09.01.2017 / 21:16

2 answers

7

There are two types of components involved, the heavyweight and lightweight components. The heavyweight components depend heavily on details implemented in native code to work (ie C and C ++) and because of this, their performance tends to be (but is not necessarily) a little better, but making them much more difficult to use, as well as sometimes depending on specific operating system details, sacrificing compatibility. The lightweight components are made 100% in Java, without requiring native code.

The heavyweight components are from AWT (for example, java.awt.Button ), whereas are Swing components (for example javax.swing.JButton ). Direct use of heavyweight components has been discouraged for a long time.

All components of AWT are subclasses of java.awt.Component . One of these subclasses, java.awt.Container is special because it corresponds to a component that may contain subcomponents. A special subclass of Container is the javax.swing.JComponent class which is the superclass of all Swing components. This hierarchy also means that any Swing components may contain subcomponents.

Drawing all of them is based on the paint(Graphics) of class Component ". However, it is not recommended to overwrite or invoke this method directly.

A redesign of a component can be enforced by using the repaint() .

A heavyweight component that has its dirty screen layout can be redrawn by AWT using the update() . The default implementation only clears the drawing area of the entire component and calls paint() to redraw it, but more specialized implementations can do incremental updates or determine which areas are dirty and redraw only those areas. Again, the update() method should not be called directly.

However, the concept of incremental design and the use of update() has proved to be confusing and often unnecessary, so it is not used for lightweight components. In the lightweight components, a call to update() is in practice the same as a call to paint() . In the lightweight components, the paint() method implemented in the JComponent class only calls the paintComponent(Graphics) , paintBorder(Graphics) , and paintChildren(Graphics) methods in this order ( font ). As the names suggest, they respectively draw the component, draw the edge of the component, and draw the subcomponents. Although you can override any of these methods, you will usually want to overwrite only paintComponent(Graphics) .

In the end, in both cases, the person doing the drawing is the class Graphics . However, AWT will always use an instance of the subclass Graphics2D . , then you can cast it securely if you need to have access to a larger and better set of available methods.

And remembering that at the end of the day, whoever controls everything is the AWT. Swing is just a layer above the AWT, not a complete replacement. Swing is only an AWT substitute for component replacement for lightweight components.

There's one more important thing to note: JFrame , JDialog and JWindow are not subclasses of JComponent , so they do not have the paintComponent(Graphics) method, but have the paint(Graphics) method. In this case, you may then prefer to overwrite paint(Graphics) by invoking a super.paint(g) within it. However, in the case of JFrame s and JDialog s, it might be best to add JPanel in and then overwrite paintComponent(Graphics) of JPanel .

TL; DR: In other words, to implement the design of your subclass of JComponent , you should overwrite the paintComponent(Graphics) method. For subclasses of JFrame , JDialog and JWindow , you override paint(Graphics) itself.

More details on link

    
09.01.2017 / 21:47
2

In the implementation, the basic difference is that AWT uses the operating system visual component APIs where the VM is running to create whole components with virtually all behavior being controlled by the OS.

Swing, draws the components from scratch using basic OS graphing APIs, managing most of the components' functionality on their own.

    
09.01.2017 / 21:46