Drawing in Java

Source: Internet
Author: User
ArticleDirectory
    • AWT plotting:
    • Swing plot:

Drawing in Java is divided into AWT drawing and swing drawing. Next we will discuss it one by one:

AWT plotting:

The component class provides three plotting-related methods:
Paint (Graphics G): draws the appearance of a widget.
Update (Graphics G): Call the paint method to refresh the component appearance.
Repaint (): Call the update method to refresh the component appearance.
The call relationship between the above three methods is: The repaint () method calls the update () method, and the update () method calls the paint () method.
When the component is displayed on the screen for the first time, the AWT thread automatically calls the paint () method to draw the component.

The Update () method can be divided into container components and common components:
1. Update () method of the container component: first fill the entire component area with the background color of the component, and then call the paint () method to redraw the component. The Default background color of the widget is white, and the default foreground color is black.
2. Update () method of a common component: directly call the paint () method.

ProgramThe paint () and update () Methods of the component should not be actively called. Both methods are called by the AWT system. If the program wants the AWT system to re-draw the component, call the repaint () method of the component. The paint () and update () methods are usually overwritten. In general, the program uses the override paint () method to draw on the AWT component.

When you override the update () or paint () method, this method contains a graphics type parameter. When the AWT system calls these two methods, it will upload a graphics-type object, through which the drawing function can be realized.

AWT provides a canvas class as the drawing canvas. The program can create a canvas subclass and override its painting () method to implement the drawing.

Swing plot:

The jcomponent class of swing covers the painting () method of the component class. The paint () method of the jcomponent class delegates the drawing task to three protected methods.
1. paintcomponent () method: Draw the current component
2. paintborder () method: draw the boundaries of components
3. paintchildren () method: if the component is a container, the component contained in the container is drawn.

To draw a custom swing component, you only need to overwrite the paintcomponent () method. To ensure the normal display of the image, you must first call Super. paintcomponent (g) in this method before doing what you want.

Note: Do not call the paintcomponent () method by yourself. This method is automatically called when the application needs to redraw.

Supplement: For any plotting program, the problem is that the calculation of the drawing position is much more complicated than the calling of the drawing function. You may have a similar experience: most of the time is spent on the decision of the painted content, while the real painting process is very simple.
Although you can draw on any jcomponent, and because of this, you can think of them as canvases. However, if you only want to have a plane that can be directly drawn, the typical practice is to inherit from jpanel. the only method to override is paintcomponent (), you can still call the base class version of the method before doing what you want.

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.