當前位置:編程學習大全網 - 編程語言 - 詢問java applet是否能夠處理簡單的幾何案例

詢問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[])

{新漆板(“繪圖程序”);}

}

  • 上一篇:地雷智能采用哪些技術獲得智能的?
  • 下一篇:關於VC如何操作word2003
  • copyright 2024編程學習大全網