Java 中repaint的理解。

学习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);
		}
这是一个绘制一个小矩形跟随鼠标点击移动的小程序。其界面如下

Java的教程上是这样解释的:

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使几个区域重绘!!其上的代码也就是说,使原来的绘图区域和当前的绘图区域发生重绘,以便实现将原来的图像擦去,并显示当前的图像!好吧,这样理解应该是对的吧!欢迎指正。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值