询问java applet是否能够制作简单的几何图案

导入Java . applet . *;

导入Java . awt . *;

导入Java . awt . event . *;

导入Java . util . *;

导入javax . swing . *;

导入Java . awt . geom . *;

导入Java . io . *;

类点实现可序列化

{

int x,y;

颜色颜色;

int工具;

跨边界;

Point(int x,int y,Color col,int tool,int boarder)

{

this.x = x

this.y = y

this.col = col

this.tool =工具;

this.boarder =寄宿生;

}

}

类paintboard扩展框架实现ActionListener、MouseMotionListener、MouseListener、ItemListener

{

int x = -1,y =-1;

int con = 1;//画笔大小

int Econ = 5;//橡皮大小

int tool flag = 0;//toolFlag:工具标记

//toolFlag工具对应表:

//(0-刷);(1-橡胶);(2-清除);

//(3-直线);(4圈);(5-矩形);

颜色c =新颜色(0,0,0);//画笔颜色

BasicStroke size = new basic stroke(con,basic stroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);//画笔粗细

Point cutflag =新点(-1,-1,c,6,con);//截断标志

Vector paintInfo = null//点信息矢量组

int n = 1;

FileInputStream picIn = null

FileOutputStream picOut = null

ObjectInputStream VIn = null

ObjectOutputStream VOut = null

//*工具面板-画笔、线条、圆形、矩形、多边形、橡皮擦、清除*/

面板工具面板;

按钮橡皮擦,drLine,drCircle,drRect

按钮清晰,笔;

Choice ColChoice,SizeChoice,EraserChoice

按钮colchooser

标签颜色,尺码b,尺码e;

//保存功能

按钮openPic,savePic

FileDialog打开图片,保存图片;

漆板(弦s)

{

超级用户;

addMouseMotionListener(this);

addmouse listener(this);

paint info = new Vector();

/*工具按钮和选项*/

//颜色选择

col Choice = new Choice();

col choice . add(" black ");

col choice . add(" red ");

col choice . add(" blue ");

col choice . add(" green ");

col choice . additem listener(this);

//画笔大小选择

size Choice = new Choice();

size choice . add(" 1 ");

size choice . add(" 3 ");

size choice . add(" 5 ");

size choice . add(" 7 ");

size choice . add(" 9 ");

size choice . additem listener(this);

//橡胶尺寸选择

eraser Choice = new Choice();

eraser choice . add(" 5 ");

eraser choice . add(" 9 ");

eraser choice . add(" 13 ");

eraser choice . add(" 17 ");

eraser choice . additem listener(this);

////////////////////////////////////////////////////

tool Panel = new Panel();

Clear =新建按钮(“clear”);

橡皮擦=新按钮(“橡皮擦”);

Pen =新建按钮(“画笔”);

DrLine =新建按钮("画一条直线");

DrCircle =新建按钮("画圆");

DrRect = new Button("画一个矩形");

OpenPic =新按钮(“打开图片”);

Saveepic = newbutton("保存图片");

Colchooser = new Button("显示调色板");

//每个组件的事件监控

clear . addactionlistener(this);

eraser . addactionlistener(this);

pen . addactionlistener(this);

dr line . addactionlistener(this);

dr circle . addactionlistener(this);

dr rect . addactionlistener(this);

open pic . addactionlistener(this);

save pic . addactionlistener(this);

col chooser . addactionlistener(this);

颜色=新标签(“刷颜色”,标签。中心);

尺寸B =新标签(“刷子尺寸”,标签)。中心);

尺寸E =新标签(“橡胶尺寸”,标签)。中心);

//面板添加组件

tool panel . add(open pic);

tool panel . add(save pic);

tool panel . add(pen);

tool panel . add(dr line);

tool panel . add(dr circle);

tool panel . add(dr rect);

ToolPanel.add(颜色);tool panel . add(col choice);

ToolPanel.add(大小b);tool panel . add(size choice);

tool panel . add(col chooser);

toolPanel.add(橡皮擦);

ToolPanel.add(大小为e);tool panel . add(eraser choice);

toolPanel.add(清除);

//工具面板到小程序面板

添加(toolPanel,BorderLayout。北);

setBounds(60,60,900,600);set visible(true);

validate();

//保存和加载对话框

OpenPicture = new FileDialog(this,“打开图片”,FileDialog。负载);

open picture . set visible(false);

SavePicture = new FileDialog(this,“保存图片”,FileDialog。保存);

save picture . set visible(false);

openPicture.addWindowListener(新WindowAdapter()

{

公共void窗口关闭(WindowEvent e)

{ open picture . set visible(false);}

});

savePicture.addWindowListener(新WindowAdapter()

{

公共void窗口关闭(WindowEvent e)

{ save picture . set visible(false);}

});

addWindowListener(新WindowAdapter()

{

公共void窗口关闭(WindowEvent e)

{ system . exit(0);}

});

}

公共空白油漆(图形g)

{

graphics 2d g2d =(graphics 2d)g;

点p1,p2;

n = paint info . size();

if(工具标志==2)

g.clearRect(0,0,getSize()。width,getSize()。身高);//清除

for(int I = 0;我& ltn;i++){

p 1 =(Point)paint info . element at(I);

p2 =(Point)paint info . element at(I+1);

size = new BasicStroke(p 1 . boarder,basic stroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);

g2d . set color(p 1 . col);

g2d.setStroke(大小);

if(p1.tool==p2.tool)

{

开关(p1.tool)

{

案例0://画笔

Line2D line1 =新Line2D。Double(p1.x,p1.y,p2.x,p2 . y);

g2d . draw(line 1);

打破;

Case 1:// eraser

g.clearRect(p1.x,p1.y,p1.boarder,p 1 . boarder);

打破;

情况3://画一条直线

Line2D line2 =新的Line2D。Double(p1.x,p1.y,p2.x,p2 . y);

g2d . draw(line 2);

打破;

案例4://画一个圆

Ellipse2D椭圆=新Ellipse2D。Double(p1.x,p1.y,Math.abs(p2.x-p1.x),math . ABS(p2 . y-p 1 . y));

g2d.draw(椭圆);

打破;

案例5://画一个矩形

Rectangle2D rect =新Rectangle2D。Double(p1.x,p1.y,Math.abs(p2.x-p1.x),math . ABS(p2 . y-p 1 . y));

g2d . draw(rect);

打破;

情况6://截断,跳过

I = I+1;

打破;

默认值:

}//结束开关

}//结束if

}//结束于

}

公共void itemStateChanged(item event e)

{

If(e.getSource()==ColChoice)//预选颜色。

{

string name = col choice . getselecteditem();

if(name=="black ")

{c =新颜色(0,0,0);}

else if(name=="red ")

{c =新颜色(255,0,0);}

else if(name=="green ")

{c =新颜色(0,255,0);}

else if(name=="blue ")

{c =新颜色(0,0,255);}

}

else if(e . getsource()= = size阳离子)//画笔大小

{

string selected = size choice . getselecteditem();

if(selected=="1 ")

{

con = 1;

size = new BasicStroke(con,BasicStroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);

}

else if(selected=="3 ")

{

con = 3;

size = new BasicStroke(con,BasicStroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);

}

else if(selected=="5 ")

{ con = 5;

size = new BasicStroke(con,BasicStroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);

}

else if(selected=="7 ")

{ con = 7;

size = new BasicStroke(con,BasicStroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);

}

else if(selected=="9 ")

{ con = 9;

size = new BasicStroke(con,BasicStroke。CAP_BUTT,BasicStroke。JOIN _ BEVEL);

}

}

Elseif (e.getsource () = =橡皮擦选择)//橡皮大小

{

string Esize = eraser choice . getselecteditem();

if(Esize=="5 ")

{ Econ = 5 * 2;}

else if(Esize=="9 ")

{ Econ = 9 * 2;}

else if(Esize=="13 ")

{ Econ = 13 * 2;}

else if(Esize=="17 ")

{ Econ = 17 * 3;}

}

}

public void mouse dragged(mouse event e)

{

点p 1;

开关(工具标志){

案例0://画笔

x =(int)e . getx();

y =(int)e . gety();

p1 =新点(x,y,c,toolFlag,con);

paint info . add element(p 1);

repaint();

打破;

Case 1:// eraser

x =(int)e . getx();

y =(int)e . gety();

p1 =新点(x,y,null,toolFlag,Econ);

paint info . add element(p 1);

repaint();

打破;

默认值:

}

}

public void mouse moved(mouse event e){ }

公共void更新(图形g)

{

油漆(克);

}

公共void mousePressed(MouseEvent e)

{

点p2;

开关(工具标志){

情况3://直线

x =(int)e . getx();

y =(int)e . gety();

p2 =新点(x,y,c,toolFlag,con);

paint info . add element(p2);

打破;

案例4: //圈

x =(int)e . getx();

y =(int)e . gety();

p2 =新点(x,y,c,toolFlag,con);

paint info . add element(p2);

打破;

案例5: //矩形

x =(int)e . getx();

y =(int)e . gety();

p2 =新点(x,y,c,toolFlag,con);

paint info . add element(p2);

打破;

默认值:

}

}

public void mouse released(mouse event e)

{

点P3;

开关(工具标志){

案例0://画笔

paint info . add element(cut flag);

打破;

case 1: //eraser

paint info . add element(cut flag);

打破;

情况3://直线

x =(int)e . getx();

y =(int)e . gety();

p3 =新点(x,y,c,toolFlag,con);

paint info . add element(P3);

paint info . add element(cut flag);

repaint();

打破;

案例4: //圈

x =(int)e . getx();

y =(int)e . gety();

p3 =新点(x,y,c,toolFlag,con);

paint info . add element(P3);

paint info . add element(cut flag);

repaint();

打破;

案例5: //矩形

x =(int)e . getx();

y =(int)e . gety();

p3 =新点(x,y,c,toolFlag,con);

paint info . add element(P3);

paint info . add element(cut flag);

repaint();

打破;

默认值:

}

}

public void mouse enter(mouse event e){ }

public void mouse exited(mouse event e){ }

public void mouse clicked(mouse event e){ }

公共无效操作已执行(操作事件e)

{

If(e.getSource()==pen)// brush

{ tool flag = 0;}

if(e . getsource()= = eraser)//eraser

{ tool flag = 1;}

If(e.getSource()==clear)//清除

{

tool flag = 2;

paint info . remove allements();

repaint();

}

If(e.getSource()==drLine)//画一条线。

{ tool flag = 3;}

If(e.getSource()==drCircle)//画一个圆。

{ tool flag = 4;}

If(e.getSource()==drRect)//绘制一个矩形。

{ tool flag = 5;}

if(e . getsource()= = col chooser)//调色板

{

color new color = jcolorchooser . showdialog(this," palette ",c);

c = newColor

}

If(e.getSource()==openPic)//打开图片。

{

open picture . set visible(true);

if(openPicture.getFile()!=空)

{

int tempflag

tempflag = toolFlag

tool flag = 2;

repaint();

尝试{

paint info . remove allements();

File File in = new File(open picture . get directory()、open picture . getfile());

picIn = new file inputstream(filein);

VIn =新的ObjectInputStream(picIn);

paint info =(Vector)vin . readobject();

vin . close();

repaint();

toolFlag = tempflag

}

catch(ClassNotFoundException ioe 2)

{

repaint();

toolFlag = tempflag

System.out.println("无法读取对象");

}

catch(IOException IOe)

{

repaint();

toolFlag = tempflag

System.out.println("无法读取文件");

}

}

}

If(e.getSource()==savePic)//保存图片。

{

save picture . set visible(true);

尝试{

File File out = new File(save picture . get directory()、save picture . getfile());

picOut = new file output stream(file out);

VOut = new object output stream(picOut);

vout . writeobject(paint info);

vout . close();

}

catch(IOException IOe)

{

System.out.println("不能写对象");

}

}

}

}

公共类计算机

{

公共静态void main(String args[])

{新漆板(“绘图程序”);}

}