这是教材上的一个applet程序,这个程序是运行成功的!!
当按下鼠标时记录鼠标位置,松开鼠标时画出矩形,我奇怪的是:
程序里定义了paint()函数,却没有调用它,调用的是repaint()函数,为什么?repaint函数哪里定义的?
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class App15_1 extends Applet implements MouseListener
{
int x1,x2,y1,y2,width,height;
boolean flag=false;
public void init()
{
this.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
flag=true;
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
repaint();
}
public void paint(Graphics g)
{
if(flag)
{
width=Math.abs(x2-x1);
height=Math.abs(y2-y1);
if(x1>x2 && y1>y2)
{x1=x2;y1=y2;}
else if(x1>x2 && y1<y2)x1=x2;
else if(x1<x2 && y1>y2)y1=y2;
g.drawRect(x1,y1,width,height);
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}