Java, paint () repaint () paintComponent () update () What are the differences between these methods?

Source: Internet
Author: User

In fact, setting properties in java will lead to re-painting, but because this re-painting event is also placed in the event dispatching thread, it causes the event dispatching thread to be Idle, to avoid this situation, place the blocking action in another thread.
Repaint is the starting re-painting action. When repaint is called, The repaintManager will be notified to add a re-painting area. repaintManager will merge some re-painting areas under certain conditions, then, a draw action is dispatched to the event dispatching thread (EventQueue ).

When the event dispatching thread executes this painting event, it will call the paint of the component. In the paint method, update is called to clear the re-painting area (white by default ), call paintComponent to draw itself, and call paintChildren to draw all the sub-nodes. For detailed procedures, refer to the painting method in JComponent.

Public void update (Graphics g) Calls paint. Instead of clearing the background, check ComponentUI. update, which is called by paintComponent.
JComponent: paint (Graphic g) is called by Swing to draw components. The application should not directly call the paint, but use the repaint method to arrange the re-painting component.

This method actually delegates the rendering work to three protected Methods: paintComponent, paintBorder, and paintChildren. Call these methods in the listed order to ensure that the child components appear at the top of the component itself. Generally, components and their child components should not be drawn in the insets area allocated to the border. Subclass can always override this method only. Only subclass of the paint method delegated by the UI (appearance) should override paintComponent.

Protected void paintComponent (Graphics g) if the UI delegate is not null, the painting method of the UI delegate is called. Pass a copy of the Graphics object to this delegate to protect the rest of the paint code from unrecoverable changes (such as Graphics. translate ).
If you override this method in a subclass, you should not permanently change the content passed in to Graphics. For example, you should not change the cropping rectangle or modify the conversion. If you need to perform these operations, you will find it easier to create and operate a new Graphics based on the input Graphics. In addition, if the super class implementation is not called, The opacity attribute must be observed. That is, if the component is not transparent, the background must be completely filled with transparent colors. If you do not observe the opacity attribute, you are likely to see the visualized human content.

The input Graphics object may have a different conversion from the ID conversion installed on the object. In this case, if other conversions are applied multiple times, unexpected results may be obtained.

Protected void paintChildren (Graphics g) draws the child components of this component. If shouldUseBuffer is true, no buffer is available for all component ancestors, and the buffer can be used (if any) at the child level of the component ). Otherwise, the ancestor has a buffer currently in use and the child component should not be drawn using a buffer.
Public void print (Graphics g) calls this method to print the component. This method will call printComponent, printBorder, and printChildren. We recommend that you not override this method, but rewrite one of the methods mentioned above. This method sets the component status so that the dual-buffer is not used. For example, the drawing is completed directly on the input Graphics.
Protected void printComponent (Graphics g)

This method is called during the print operation. Implement this method to call this component

PaintComponent

. You can override this method if you want to add special painting behavior during printing.

Protected void printChildren (Graphics g) prints the child components of this component. Implement this method to call paintChildren for this component. Override this method if you want to print child components in a different way than drawing
Public void updateUI () resets the UI property to the current appearance value. The subclass of JComponent must override this method as follows:
Public void updateUI (){
SetUI (SliderUI) UIManager. getUI (this );
}

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.