Paint. repaint. Update in Java

Source: Internet
Author: User
Tags gety

Paint, repaint, update, all three methods related to the drawing.

Although they have been used before, they are not fully understood.

The following paragraph is reproduced (I feel better ):

Repaint () is a method that refreshes the page. You can call this method if you need to redraw the page.
From the preceding flowchart, we can see that when the repaint () method is not called, the thread calls the paint () method to draw the animation, and refresh the display with repaint. however, such an animation has a disadvantage (such an effect will flash ). think about the animation. It will always flash every other time. Will anyone watch it? So how should we remove flashes? I will talk about it in the next article. Here we mainly describe the repaint () method.
When we call repaint (), we can see that it does not directly draw an animation (call paint ()), instead, the AWT thread is called to call another method Update () in the thread, and then update () calls the paint () method for painting. so why do we need to take another step here? Does this increase the amount of code writing for me? The answer is no. If you do not call repaint (), you will not be able to refresh each time. Instead, you will only draw overlapping images, but not one by one. in this case, what role does the update () call play?
Update (): Clear the current display and call the paint () method. Of course, this update () method can be modified.
Based on the above introduction, we can summarize the working principle of repaint (): repaint () clears the current display by calling the thread and then calling the update () method by the thread () method to draw the next content to be displayed. in this way, an animation is formed from the perspective of the alternate display of an image.

The following code is an example I have written. It is only for your reference. If you have any questions, please kindly advise and learn together,

The function of this Code is to achieve dynamic draw line:

 

// Main class import javax. swing. *; import Java. AWT. event. *; public class framedraw extends jframe // implements mouselistener {private drawpanel = NULL; Public framedraw () {Panel = new drawpanel (); this. add (panel); this. setvisible (true); this. setsize (300,300);} public static void main (string [] ARGs) {New framedraw ();}}

Called class:

 

 

Import javax. swing. *; import Java. AWT. *; import Java. AWT. event. *; public class drawpanel extends jpanel implements mouselistener {static int x = 0, X1 = 0, y = 0, Y1 = 0; Public drawpanel () {This. addmouselistener (this);} public void paint (Graphics g) {super. paint (g); image = toolkit. getdefatooltoolkit (). getimage ("images/psu.jpg"); G. drawimage (image, 0, 0, 300,300, this); G. drawline (X, Y, X1, Y1);} public void mouseclicked (M Ouseevent e) {// obtain the starting point coordinate if (E. getbutton () = mouseevent. button3) {x = E. getx (); y = E. gety (); // label. settext ("START coordinate of the mouse position:" + "" + "X .... "+ x +" y .... "+ Y); system. out. println ("X... "+ x + ".... "+ Y);} // obtain the end coordinate if (E. getbutton () = mouseevent. button1) {x1 = E. getx (); Y1 = E. gety (); system. out. println ("coordinates of the end... "+ X1 + "... "+ Y1); // If (Y> = 0 | x> = 0) {This. repaint (); this. validate ();} elsesystem. out. println ("data error... ") ;}} Public void mouseentered (mouseevent e) {} public void mouseexited (mouseevent e) {} public void mousepressed (mouseevent e) {} public void mousereleased (mouseevent E) {} public static void main (string [] ARGs) {New drawpanel ();}}

 

 

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.