How does paint work java




















The repaint method is an asynchronous method of applet class. When call to repaint method is made, it performs a request to erase and perform redraw of the component after a small delay in time. Whenever the repaint method is invoked from a component, a request is sent to the graphical user interface, which communicates to the graphical user interface to perform some action at a future instance of time. The whole idea behind the repaint method is to avoid the call to paint method directly.

After the mouse click event is performed, the following shapes will be visible with a black color border. Note that this is done through the repaint method, which calls the update and then paints method, due to which we are able to see visible shapes immediately after click event is performed. Applet; import java. In the above example, we have an applet and a variable called test is declared inside it.

Therefore, we are using the repaint method that ensures to call update method before the paint method. The output of the above program. In this window value of the test, the variable is always incrementing, and the updated value is visible on GUI.

The above example provides a clear understanding of the repaint method and its function. There are versions of these last two that take a Polygon object as a parameter. JPanel and painting: self adjusting circles. Color; import java. Font; import java.

Graphics; import java. Polygon; import javax. BOLD, 14 ; g. The component behind the button its container, most likely would then be visible, showing through at the corners of the button's bounds. The button would have to include special hit detection code to avoid acting pressed if the user happens to click its corners.

It's one of three methods that JComponent objects use to paint themselves. By default, it first paints the background if the component is opaque. Then it performs any custom painting. Do not invoke or override this method. Note: We recommend that you don't override or invoke the method that calls the paint Xxx methods: the paint method.

Although overriding paint is legitimate in non-Swing components, it's generally not a good thing to do in components that descend from JComponent. Overriding paint can confuse the painting system, which relies on the JComponent implementation of the paint method for correct painting, performance enhancements, and features such as double buffering.

The following figure illustrates the order in which each component that inherits from JComponent paints itself. Step 3 is performed by paintBorder , and step 4 is performed by paintChildren 1. The standard Swing components delegate their look-and-feel-specific painting to an object called a UI delegate.

When such a component's paintComponent method is called, the method asks the UI delegate to paint the component. Generally, the UI delegate first checks whether the component is opaque and, if so, paints the entire background of the component. Then the UI delegate performs any look-and-feel-specific painting.

This means that if you extend JComponent , your component needs to paint its own background if it's opaque. It's an article in The Swing Connection that discusses in depth the intricacies of painting. Here is its GUI:. Here is its containment hierarchy:. The content pane first paints its background, which is a solid gray rectangle. It then tells the JPanel to paint itself.



0コメント

  • 1000 / 1000