学习Java的绘图的时候,总是对其demo上的几行代码不理解。
if((CURR_X != x) || (CURR_Y != y))
{
// The square is moving, repaint background
// over the old square location.
repaint(CURR_X, CURR_Y, CURR_W + OFFSET, CURR_H + OFFSET);
// Update coordinates.
redSquare.setX(x);
redSquare.setY(y);
// Repaint the square at the new location.
repaint(redSquare.getX(), redSquare.getY(), redSquare.getWidth()
+ OFFSET, redSquare.getHeight() + OFFSET);
}
这是一个绘制一个小矩形跟随鼠标点击移动的小程序。其界面如下
Because we are manually setting the clip, our moveSquare
method invokes the repaint method not once, but twice. The first invocation tells Swing to repaint the area of the component where the square previously was (the inherited behavior uses the UI Delegate to fill that area with the current background color.) The second invocation paints the area of the component where the square currently is. An important point worth noting is that although we have invoked repaint twice in a row in the same event handler, Swing is smart enough to take that information and repaint those sections of the screen all in one single paint operation. In other words, Swing will not repaint the component twice in a row, even if that is what the code appears to be doing.
它是说,这两个repaint其实是一个!就是说:连续调用repaint使几个区域重绘!!其上的代码也就是说,使原来的绘图区域和当前的绘图区域发生重绘,以便实现将原来的图像擦去,并显示当前的图像!好吧,这样理解应该是对的吧!欢迎指正。