repaint()

Helix333 2010-03-29 09:45:41
java.awt.Component中 repaint(int x, int y, int width, int height) 方法为 重绘组件的指定矩形区域。
请各位指点下它的具体作用是什么?如果是图像重绘的话,重绘的图像是哪个?在哪添加待显示的图像?

请高手最好留个简单代码,学习下,谢谢!!!
...全文
275 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jaychang2009 2011-01-06
  • 打赏
  • 举报
回复
楼上代码有误。。。
chihonglu1111 2010-03-29
  • 打赏
  • 举报
回复
你把f.repaint()注释和不注释掉的区别看一下就知道了,
实际上repaint()方法就是调用了两个方法,调用repaint方法的同时,也调用了update方法,最后又调用了paint方法,相当于一个刷新的作用
chihonglu1111 2010-03-29
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestMouseMotion {
public static void main(String args[]) {
new MyFrame88("drawing...");
}
}

class MyFrame88 extends Frame {
ArrayList<Point> points = null;
MyFrame88(String s) {
super(s);
points = new ArrayList<Point>();
setLayout(null);
setBounds(300,300,400,300);
this.setBackground(new Color(204,204,255));
setVisible(true);
System.out.println("good");
this.addMouseListener(new Monitor());
System.out.println("beter");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void paint(Graphics g) {
Iterator<Point> i = points.iterator();
while(i.hasNext()){
Point p = (Point)i.next();
g.setColor(Color.BLUE);
g.fillOval(p.x,p.y,10,10);
}
}
public void addPoint(Point p){
points.add(p);
}
}
class Monitor extends MouseAdapter {
// private int num = 0;
public void mouseClicked(MouseEvent e) {
MyFrame88 f = (MyFrame88)e.getSource();
f.addPoint(new Point(e.getX(),e.getY()));
// if(num++ >= 5){
// f.repaint();
// num=0;
// }
}
}
ZRBRZB 2010-03-29
  • 打赏
  • 举报
回复
比如你通过拖动鼠标画一条直线,那就要在鼠标移动事件里调用repaint()
sun1950 2010-03-29
  • 打赏
  • 举报
回复
动画的时候就要每隔固定时间repaint(),不然怎么动....
Helix333 2010-03-29
  • 打赏
  • 举报
回复
paint()不就显示好了嘛?repaint()有什么用?我感觉像多此一举啊。。。有没有repaint()的例子。。。麻烦各位贴出一个来。。。个人资料有限,谢谢
Helix333 2010-03-29
  • 打赏
  • 举报
回复
都已经paint()好了,为什么还要repaint()。。。如果修改了,或者是当前区域想替换一幅新图,不能使用repaint(int x, int y, int width, int height)???谢谢
nj_dobetter 2010-03-29
  • 打赏
  • 举报
回复
repaint(int x, int y, int width, int height)只是告诉重绘管理器需要重绘一个矩形区域,在这个区域里面绘制的是什么,要看paint()里面的代码。实际上在调repaint(int x, int y, int width, int height)的时候,就说明已经知道里面需要重绘什么。
wslzxql 2010-03-29
  • 打赏
  • 举报
回复
我不是高手,我只知道重绘就是绘你的paint()的那个图像,其它的书上都有啊,你看一个例子就知道了

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧