●试题六
【说明】
下面是一个Applet程序,其功能是在绘图区域中通过鼠标的移动来绘制直线,并且有清除绘图区域按钮,用来清除已经绘制的图像。
程序运行结果如图5所示。

图5
import javA.awt.*;
import javA.applet.*;
/*
*/
public class ex6_7 extends Applet{
private Button btn;
private boolean bDraw, bClear;
private int upX, upY,downX, downY;
public void init(){
setLayout(null);
bClear = false;
bDraw = false;
btn = new Button("clear");
btn.reshape(250, 150, 70, 30);
add(btn);
}
public void paint(Graphics g){
if(bClear){
g.clearRect(0, 0, getSize().width, getSize().height);
(1) ;
}
if(bDraw){
g.drawLine( (2) );
bDraw = false;
}
}
public void update(Graphics g){
(3) ;
}
public boolean mouseDown(Event event, int x, int y){
downX = x;
downY = y;
return true;
}
public boolean mouseUp(Event event, int x, int y){
upX = x;
upY = y;
(4) ;
repaint();
return true;
}
public boolean action(Event event, Object object){
if ( (5) ){
bClear = true;
repaint();
}
return true;
}
}
ex6_7.html
ex6_7 code=" ex6_7.class" width=800 height=400 >