目录
- 第一卷
- 第二卷
画板能够捕捉到copy;露事件、鼠标事件和其他类似的事件。基本的画板类不处理这copy;事件,但你可以copy;展它来创建有你所需功能的画板类。
6.7.1创建画板
importjava.awt.*;
importjava.applet.Applet;
publicclasssuperGUIextendsApplet{
...myCanvasdoodle;
...public
void init(){
...
//建立我们的画板
doodle=new myCanvas();
doodle.reshape(0,0,100,100);
leftPanel.add(\"Center\",doodle);
...
}
}
class myCanvasextendsCanvas{
public void paint(Graphicsg){
g.drawRect(0,0,99,99);
g.drawString(\"Canvas\",15,40);
}
}
6.7.2画板事件
你可以覆盖一般的事件处理成员函数。下面是一个包含了mouseDown事件处理的例子:importjava.awt.*;importjava.applet.Applet;
public class canvasextendsApplet{
Buttonb1;
publicvoidinit(){
//
SetourlayoutasaBorderstylesetLayout(newBorderLayout(15,15));
b1=newButton(\"Test\");
myCanvasc1=new myCanvas(100,100);
//
addthecanvasandthebuttontotheapplet
add(\"Center\",c1);
add(\"South\",b1);}
publicbooleanaction(Evente,Objectarg){
System.out.println(\"Event:\"+arg);
return true;
}
public booleanmouseDown(Evente,intx,inty){
System.out.println(\"Mouseworks:(\"+x+\",\"+y+\")\");
returntrue;
}
}
class myCanvasextendsCanvas{
privateintwidth;privateintheight;
publicmyCanvas(intw,inth){
width=w;
height=h;
reshape(0,0,w,h);
}
public void paint(Graphicsg){
g.setColor(Color.blue);
g.fillRect(0,0,width,height);
}
publicbooleanmouseDown(Evente,intx,inty){
if((x