Paint, paintcomponent, and repaint better understand the JComponent running process

Source: Internet
Author: User

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:

 
 
  1. protectedvoidpaintComponent(Graphicsg){  
  2. if(ui!=null){  
  3. GraphicsscratchGraphics=(g==null)?null:g.create();  
  4. try{  
  5. ui.update(scratchGraphics,this);  
  6. }  
  7. finally{  
  8. scratchGraphics.dispose();  
  9. }  
  10. }  

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:

 
 
  1. Publicvoidpaintcomponent (graphicsg ){
  2. If (isopaque ()){
  3. G. setcolor (getbackground ());
  4. G. fillrect (0, 0, getwidth (), getheight ());
  5. }
  6. If (pressed) {// The button is pressed
  7. // Draw the following picture
  8. } Else {
  9. // Draw the desired image
  10. }
  11. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.