Java what does it mean to call repaint () instead of calling paint (?) directly in Applet?

We override this paint method

public void paint(Graphics g)
{
   g.drawString(msg,xpos,ypos);
}

If we have another method, we can say a mouse down event method

public void mousePressed(MouseEvent me)    
    {
        xpos=me.getX();  // msg,xpos and ypos are variables of class
        ypos= me.getY();
        msg="You pressed mouse";
        repaint();
    }

Why can't we call it paint (graphics g) instead of repeat ()?

Solution

You should try You'll notice

>You must get the graphics object in some way. > It is not best practice to invoke drawing in the body of the event handler, because it causes the principal of the method to execute immediately on the GUI thread, so no event can be processed before paint returns. Otoh, repaint plans to draw events at a convenient point in the future and will not make the GUI look suspended Of course, in your case, drawstring is not very slow, but in general

Here is the classic article on painting

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>