Address: http://developer.51cto.com/art/200907/137237.htm
When I went back last night, I thought the component object was too simple. I thought it was too much content. It was necessary to add two additional articles to explain other component concepts. Today, we will introduce the processing process of the swing component painting method. This process can help us understand many swing mechanisms.
The painting method of the swing component is an internal interface method. Generally, you do not directly call this method. It is always called in the event scheduling thread. In general, in addition to the system refresh event to trigger this method, component's repaint also triggers this method call. The repaint method is often used to refresh the interface when the component status changes. The repaint method is a few thread-safe methods in swing that can be called in any thread. The principle is to post a paint event to the event queue. Because events in the event queue are synchronously executed by the event scheduling thread, this method is always thread-safe. The event scheduling thread obtains the event source component from the paint event, and calls the update method of the component after the system applies for graphical device resources. Update is a product left behind by the AWT era. It is intended that the AWT component draws the background of the component and then calls the paint method to draw the foreground of the component. After swing appears, this method is discarded, and all logic is transferred to the painting method. Update simply calls the paint method to complete component rendering. In the old Java textbooks, we often see that the so-called repaint Scheduling update method, update then calls the paint method, and the custom component needs to overload the paint method and other words, because of this history.
Now, the implementation of jcomponent has transformed the painting method into multiple embedded mechanisms, including layered rendering, border, transparent background, dual buffering, and skin. These mechanisms provide convenience for different components.
Components on the graphic user interface can be divided into container components and leaf components based on their roles on the component tree. The swing model regards leaf components as a special container component without sub-components, but jcomponent inherits the iner class, And all swing components inherit jcomponent.
In the painting method, jcomponent first encapsulates the image device object based on whether the component needs to use dual buffering. After some processing, it calls the paintcomponent method to draw itself and then calls the paintborder method to draw the border, finally, call paintchildren to complete the rendering of child components.
Paintcomponent is used to draw the component itself, excluding the child component. Therefore, the mybutton method in the previous article can overwrite the paintcomponent method to reproduce the mybutton. In jcomponent implementation, the paintcomponent code of JDK 6 is:
- protectedvoidpaintComponent(Graphicsg){
- if(ui!=null){
- GraphicsscratchGraphics=(g==null)?null:g.create();
- try{
- ui.update(scratchGraphics,this);
- }
- finally{
- scratchGraphics.dispose();
- }
- }
- }
We can see that the background transparency mechanism is implemented here. First, the UI delegate object determines whether the component background is transparent. If not, the background color is used to fill the entire component area, and then the painting (G, C) is called) to complete the rendering of components in this lookandfeel type. After learning about this, we almost understood how swing achieves background transparency and how to switch skin. As the subsequent articles will describe UI delegate and skin mechanism in detail, this is the end of this article.
Currently, skin is not required. In this case, you only need to reload the paintcomponent method. If you need a background transparency mechanism, you can imitate the above Code. The paintcomponent of mybutton can be written as follows:
- Publicvoidpaintcomponent (graphicsg ){
- If (isopaque ()){
- G. setcolor (getbackground ());
- G. fillrect (0, 0, getwidth (), getheight ());
- }
- If (pressed) {// The button is pressed
- // Draw the following picture
- } Else {
- // Draw the desired image
- }
- }
Paintchildren finishes rendering the child components of the container Class component. By default, JDK calls the painting method of each self-component. In general, this method does not need to be reloaded. If you want to change the occlusion sequence of components such as Z-order, you can override this method and call the painting method of the component in reverse order.
Here we have a deeper understanding of the swing structure, and the UI delegate mechanism has also begun to reveal the terminal. There are several important concepts or mechanisms of swing component which will be further explained in the subsequent articles tomorrow.