`
msn877763580
  • 浏览: 84857 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Java中三个与绘图有关的方法

阅读更多

Component 类下的三个与绘图有关的方法

public void paint(Graphics g)
Paints this component.
This method is called when the contents of the component should be painted; such as when the
component is first being shown or is damaged and in need of repair. The clip rectangle in the
Graphics parameter is set to the area which needs to be painted. Subclasses of Component that
override this method need not call super.paint(g).

public void update(Graphics g)
Updates this component.
If this component is not a lightweight component, the AWT calls the update method in response to
a call to repaint. You can assume that the background is not cleared.
The update method of Component calls this component's paint method to redraw this component.
This method is commonly overridden by subclasses which need to do additional work in response to a
call to repaint. Subclasses of Component that override this method should either call super.update(g),
or call paint(g) directly from their update method.

public void repaint()
Repaints this component.
If this component is a lightweight component, this method causes a call to this component's paint method as soon as
possible. Otherwise, this method causes a call to this component's update method as soon as possible.


http://hi.baidu.com/hxzon/item/56d27346ce148dd3c0a59264 写道

 

repaint()这个方法是一个具有刷新页面效果的方法,如果你要页面进行重画就可以调用.

从上面的流程图可以看出,在绘制动画图形时候如果没有调用repaint()方法的时候直接就是由线程调用paint()方法进行绘制,用 repaint()进行刷新显示.但是这样的动画会有个缺点(这样的效果绘制出来会有闪烁).想想做出来的动画总是隔一段时间就闪烁,有人会看吗?那么应 该怎么去除闪烁呢?我再下面的文章中会讲到.这里主要的是说明repaint()这个方法.

在调用了repaint()的时候我门可以看 出,它并不是直接就去绘制动画(调用paint()),而是通过调用AWT线程在由线程去调用另一个方法update()再由update()调用画笔 paint()方法进行绘制. 那么这里为什么要多做一步呢?这样是不是为我门多增加代码的书写量呢?回答是当然不会,如果你不调用repaint()那么 就不能实现每一次的刷新显示,就只会绘制重叠的图形,不能一张一张的绘制出来.那么其中调用的update()到底是起到什么样的作用呢?

update():清除当前显示并调用paint()方法.当然这个update()方法是可以被修改的.

综合上面的介绍可以总结出repaint()的工作原理:repaint()通过调用线程再由线程去调用update()方法清除当前显示并再调用paint()方法进行绘制下一个需要显示的内容.这样就起到了一种图片的交替显示从而在视角上形成了动画.

package cn.sisy.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleDraw {
	private final String RECT_SHAPE = "rect";
	private final String OVAL_SHAPE = "oval";
	private Frame f;
	private Button rect;
	private Button oval;
	private MyCanvas drawArea;
	private String shape = "";
	public void init(){
		f = new Frame("绘图测试");
		rect = new Button("矩形");
		oval = new Button("椭圆");
		drawArea = new MyCanvas();
		drawArea.setPreferredSize(new Dimension(250, 180));
		
		rect.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent arg0) {
				shape = RECT_SHAPE;
				drawArea.repaint();
			}
		});
		oval.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent arg0) {
				shape = OVAL_SHAPE;
				drawArea.repaint();
			}
		});
		
		Panel p =new Panel();
		p.add(rect);
		p.add(oval);
		
		f.add(drawArea);
		f.add(p , BorderLayout.SOUTH);
		f.pack();
		f.setVisible(true);
	}
	public static void main(String[] args) {
		new SimpleDraw().init();
	}
	class MyCanvas extends Canvas{
		@Override
		public void paint(Graphics g) {
			if(shape.equals(RECT_SHAPE)){
				Color c = new Color(220, 100, 80);
				g.setColor(c);
				g.drawRect(40, 60, 80, 100);
			}
			if(shape.equals(OVAL_SHAPE)){
				Color c = new Color(80, 100, 200);
				g.setColor(c);
				g.fillOval(70, 90, 50, 40);
			}
		}
	}
}

 

程序不应该主动调用组件的paint和update方法,这两个方法都由AWT系统负责调用,如果程序希望AWT系统重新绘制该组件,调用该组件的repaint方法即可。

 

 

  • 大小: 3.9 KB
  • 大小: 10.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics