网友您好, 请在下方输入框内输入要搜索的题目:
下面是一个Applet程序,实现实心或者空心矩形的绘制。要求鼠标在Applet窗口中拖动实现矩形的绘制,可以选择填充或者空心,有按钮用来清空窗口中的图像。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.applet.*;
/*
<applet code="ex13_3.class" width=800 height=400 >
</applet>
*/
public class ex13_3 extends Applet{
private Button btnClear;
private CheckboxGroup cbg;
private Checkbox chk1, chk2;
private int upX, upY;
private int downX, downY;
private boolean bDraw, bClear, bStyle;
public void init(){
bDraw = false;
bClear = false;
bStyle. = false;
setLayout(null);
cbg = new CheckboxGroup();
chk1 = new Checkbox("Hollow", cbg, true);
chk2 = new Checkbox("Filled", cbg, false);
chk1.reshape(80,getSize().height-80,80,25);
chk2.reshape(160,getSize().height-80,80,25);
add(chk1);
add(chk2);
btnClear = new Button("clear");
btnClear.reshape(240,getSize().height-80,80,25);
add(btnClear);
}
public void paint(Graphics g){
if(bDraw){
if(upX > downX && upY > downY){
if (bStyle)
g. fillRect (downX, downY, upX - downX, upY - downY);
else
g.drawRect(downX, downY, upX - downX, upY - downY);
}
else if(upX > downX && upY < downY){
if(bStyle)
g.fillRect(downX, upY, upX - downX, downy - upY);
else
g.drawRect(downX, upY, upX - downX, downy - upY);
}
else if(upX < downX && upY > downY){
if(bStyle)
g.fillRect(upX, downY, downX - upX, upY - downY);
else
g.drawRect(upX, downY, downX - upX, upY - downY);
}
else if(upX < downX && upY < downY){
if(bStyle)
g.fillRect(upX, upY, downX - upX, downy - upY);
else
g.drawRect(upX, upY, downX - upX, downy - upY);
}
bDraw = false;
}
if(bClear)(
g.clearRect(0,0, getSize().height, getSize().width);
b
参考答案