问答题

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


import java.awt.*;
import java.applet.*;
/*
<applet code=ex6_7. class width=800 height=400>
</applet>
*/
public class ex6_7 extends Applet
private Button btn;
private boolean bDraw,bClear;
private int upX,upY,downX,downY;
public void int()
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
<HTML>
<HEAD>
<TITLE>ex6_7</TITLE>
</HEAD>
<BODY>
<appletcode=" ex6_7. class" width=800 height=400 >
</applet>
</BODY>
</HTML>

【参考答案】

(1)bClear=false (2)downX,downY,upX,upY
(3)paint(g) (4)bD......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
【程序说明】 定义一个多边形结构:struct polygon实现以下内容:(1)建立该结构的链表:create函数是创建链表,每输入一个结点的数据,就把该结点加入到链表当中,它返回创建的链表的头指针。(2)显示链表的各个结点数据:结点数据包括:多边形顶点数、各顶点的纵横坐标、当多边形顶点数为0时,链表创建结束。(3)编写一个函数disp,删除链表中的所有结点。需要注意的是:要先释放结点数据内存,再删除结点,如果在释放结点数据内存单元之前删除结点,则无法找到结点数据内存单元的地址,也就无法释放数据的内存单元。 【程序】 #include iomanip.h struct polygon int n; int* x; int *y; polygon * next; ; void Push(polygon * & head,int n) polygon * newNOde=newpolygon; newNOde=newpo,Ygon; newNOde->next= (1) ; newNOde->x=new int [n]; newNOde->y=new int[n]; newNOde->n= (2) ; for(int i=0;i<= (3) ;i++) cout<<“请输入多边形各顶点x、y坐标,坐标值之间用空格分隔:”; cin>>newNOde->x[i]>>newNOde->y[i]; (4) =head; 在head前不需要额外的。 head=newNOde; polygon * create() polygon * head=NULL; polygon * tail; int n; cout<<“请输入多边形顶点的个数(顶点个数为0时结束):”; cin>>n; if(n==0)return (5) ; Push(head, (6) ; tail=head; cout<<“请输入多边形顶点的个数(顶点个数为0时结束):”; cin>>n; while(n!=0) Push(tail->next, (7) ; 在tail->next增加结点 tail =tail->next; advance tail to point to last node cout<<“请输入多边形顶点的个数(顶点个数为0时结束):”; cin>>n; return head; void disp(polygon * head) int i,No=1; cout<<setw(10)<< x <<setw(6)<< y <<end1; while(head!=NULL) cout<<“第” <<No<<“结点:” <<end1; for(i=0;i<=head->n-1;i++) cout<<setw(10)<<head- >x[i]<<setw(6)<<head- >y[i]<<endl; (8) ; head= (9) ; Match while statement void del(polygon * head) polygon * p; while(head!=NULL) p= (10) ; head=head->next; delete p->x; delete P->y; deletep; Match while statement void main() polygon * head; head=create(); disp(head); del(head);