java作业java作业.docx

上传人:b****7 文档编号:26146751 上传时间:2023-06-17 格式:DOCX 页数:18 大小:18.09KB
下载 相关 举报
java作业java作业.docx_第1页
第1页 / 共18页
java作业java作业.docx_第2页
第2页 / 共18页
java作业java作业.docx_第3页
第3页 / 共18页
java作业java作业.docx_第4页
第4页 / 共18页
java作业java作业.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

java作业java作业.docx

《java作业java作业.docx》由会员分享,可在线阅读,更多相关《java作业java作业.docx(18页珍藏版)》请在冰豆网上搜索。

java作业java作业.docx

java作业java作业

java作业(java作业)

软件工程r1141宋洋16java作业

publicclassqingdrawpadextendsjframe{

privateobjectinputstreaminput;

privateobjectoutputstreamoutput;

privatejbuttonchoices[];privatestring[]names

={

"new"

"open".

"save".

"pencilby",

"line",

"rect',

"frect',

"oval",

"foval',

"circle",

"fcircle',

"roundrect',

"frrect',

"rubber".

"color".

"stroke".

"theword"

};

privatestring[]={stylenames

"宋体","隶书","华文彩云","仿宋_gb2312","华文行楷',

"方正舒体","timesnewroman","serif","monospaced',

"sonsserif","garamond."

};

privateiconitems[];

jtoolbarbuttonpanel;

privatejlabelstatusbar;

privatedrawpaneldrawingarea;

privateintwidth=850,height=550;

drawings[]itemlist=newdrawings[5];

privateintcurrentchoice=3;

intindex=0;

privatecolorcolor=color.black;

intr,g,b;

intf1,f2;

thestringstyle1;

privatefloatstroke=1.0f;

jcheckboxbold,italic;

jcomboboxstyles;

publicqingdrawpad()

{

super("卿乐绘图画板(中国风)");

jmenubarbar=newjmenubar();

jmenufilemenu=newjmenu("文件");

//filemenu.setmnemonic("f");

jmenuitemnewitem=newjmenuitem("新建");

newitem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

newfile();}

}

);

filemenu.add(newitem);

jmenuitemsaveitem=newjmenuitem("保存");

saveitem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

savefile();/

}

}

);

filemenu.add(saveitem);

jmenuitemloaditem=newjmenuitem("打开");

loaditem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

loadfile();

}

}

);

filemenu.

add(loaditem);

filemenu.addseparator();

jmenuitemexititem=newjmenuitem("退出");

exititem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

system.exit(0);

}

}

);

filemenu.add(exititem);

bar.add(filemenu);

jmenucolormenu=newjmenu("颜色");

jmenuitemcoloritem=newjmenuitem("选择颜色");

coloritem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

choosecolor();}

}

);

colormenu.add(coloritem);

bar.add(colormenu);

jmenustrokemenu=newjmenu("线条粗细");

jmenuitemstrokeitem=newjmenuitem("设置线条粗细");

strokeitem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

setstroke();

}

}

);

strokemenu.add(strokeitem);

bar.add(strokemenu);

jmenuhelpmenu=newjmenu("帮助");

jmenuitemaboutitem=newjmenuitem("关于画板");

aboutitem.addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

joptionpane.showmessagedialog(null,

"画图板说明"

joptionpane.information_message);

}

}

);

helpmenu.add(aboutitem);

bar.add(helpmenu);

items=newimageicon[names.length];

drawingarea=newdrawpanel();

choices=newjbutton[names.length];

buttonpanel=newjtoolbar(jtoolbar.vertical);

buttonpanel=newjtoolbar(jtoolbar.horizontal);

buttonhandleris=newbuttonhandler();

buttonhandler1handler1=newbuttonhandler1();

for(inti=0;i

{

items[in]=newimageicon(names[in]+".gif");

choices[in]=newjbutton("",items[in]);

choices[in].settooltiptext(tiptext[in]);

buttonpanel.add(choices[in]);

}

for(inti=3;in

{

choices[in].addactionlistener(is);

}

choices[0].addactionlistener(

newactionlistener(){

publicvoidactionperformed(actionevent(e)

{

newfile();

}

}

);

选择[1]。

addactionlistener(

新的actionlistener(){

publicvoidactionPerformed(ActionEvente)

{

loadfile();

}

}

);

选择[2]。

addactionlistener(

新的actionlistener(){

publicvoidactionPerformed(ActionEvente)

{

savefile();

}

}

);

选择[选择],长度为3addactionlistener(handler1);

选择[选择],长度为2addactionlistener(handler1);

选择[选择]。

length-1addactionlistener(handler1);

风格=新的JComboBox(stylenames);

(8)setmaximumrowcount风格;

additemlistener(风格。

新的itemlistener(){

公共无效itemstatechanged(itemeventE)

{

style1=stylenamesgetselectedindex()][风格;

}

}

);

大胆=新的复选框(“粗体”);

斜体=新的复选框(“斜体”);

checkboxhandler钱德勒=新checkboxhandler();

大胆additemlistener(Chandler);

斜体。

additemlistener(钱德勒);

JPanelwordpanel=新jpanel();

buttonpanel添加(粗体);

buttonpanel添加(斜体);

buttonpanel添加(风格);

风格。

setminimumsize(新的维度(50,20));

风格。

setmaximumsize(新的维度(100,20));

容器C=getcontentpane();

超级setjmenubar(杆);

(buttonpanel,总和4.BorderLayout。

北);

(drawingarea,总和4.BorderLayout。

中心);

状态=新jlabel();

总和4.(StatusBarBorderLayout。

南);

状态栏设置(“欢迎使用卿乐绘图画板→O(∩_∩)O”);

createnewitem();

setsize(宽度,高度);

show();

}

公共课buttonhandler实现ActionListener

{

publicvoidactionPerformed(ActionEvente)

{

为(j=3;J<选择。

长度为3;j++)

{

如果(如getsource()==选择[J])

{currentchoice=J;

createnewitem();

repaint();}

}

}

}

公共课buttonhandler1实现ActionListener

{

publicvoidactionPerformed(ActionEvente)

{

如果(如getsource()==选择[选择],长度为3)

{choosecolor();}

如果(如getsource()==选择[选择],长度为2)

{setstroke();}

如果(如getsource()==选择[选择]。

length-1)

{JOptionPane。

ShowMessageDialog(null),

Pleaseclickthedrawingboardtoselectthelocationoftheinputtext",

"Hint",JOptionPane.INFORMATION_MESSAGE);

CurrentChoice=14;

CreateNewItem();

Repaint();

}

}

}

If(e.getSource()==bold)

If(e.getStateChange()==ItemEvent.SELECTED)

F1=Font.BOLD;

Else

F1=Font.PLAIN;

If(e.getSource()==italic)

If(e.getStateChange()==ItemEvent.SELECTED)

F2=Font.ITALIC;

Else

F2=Font.PLAIN;

}

}

ClassDrawPanelextendsJPanel

{

PublicDrawPanel()

{

SetCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

SetBackground(Color.white);

AddMouseListener(NewmouseA());

AddMouseMotionListener(NewmouseB());

}

PublicvoidpaintComponent(Graphicsg)

{

Super.paintComponent(g);

Graphics2Dg2d=(Graphics2D)g;

Intj=0;

While(j<=index)

{

Draw(G2D,itemList[j]);

J++;

}

}

Voiddraw(Graphics2DG2D,drawingsI)

{

I.draw(G2D);}

}

VoidcreateNewItem()

{if(currentChoice==14)

DrawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));

ElsedrawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

Switch(currentChoice)

{

Case3:

ItemList[index]=newPencil();

Break;

Case4:

ItemList[index]=newLine();

Break;

Case5:

ItemList[index]=newRect();

Break;

Case6:

ItemList[index]=newfillRect();

Break;

Case7:

ItemList[index]=newOval();

Break;

Case8:

ItemList[index]=newfillOval();

Break;

Case9:

ItemList[index]=newCircle();

Break;

Case10:

ItemList[index]=newfillCircle();

Break;

Case11:

ItemList[index]=newRoundRect();

Break;

Case12:

ItemList[index]=newfillRoundRect();

Break;

Case13:

ItemList[index]=newRubber();

Break;

Case14:

ItemList[index]=newWord();

Break;

}

ItemList[index].type=currentChoice;

ItemList[index].R=R;

ItemList[index].G=G;

ItemList[index].B=B;

ItemList[index].stroke=stroke;

}

PublicvoidchooseColor()

{

Color=JColorChooser.showDialog(QingDrawPad.this),

"Pleasechooseacolor",color;

R=颜色。

getred();

G=getgreen()颜色;

Bgetblue()颜色;

自行设计[索引]。

r=R;

自行设计[索引]G=G;

[索引]ITEMLIST。

B=B;

}

公共无效setstroke()

{

输入的字符串;

输入=JOptionPane。

showinputdialog(

“请输入一个浮点型线条粗细值(>0)”);

行程=浮parseFloat(输入);

[索引]自行设计行程=行程;

}

公共无效savefile()

{

档案选择程式=新jfilechooser()JFileChooser;

然后在fileChooser.setfileselectionmode(文件对话框。

files_only);

int结果=然后在fileChooser.showsavedialog(本);

如果(结果==文件对话框。

cancel_option)

返回;

文件的文件名,然后在fileChooser.getselectedfile();

canwrite()文件名;

如果(文件名==null||文件名。

getname()。

等于(“”))

JOptionPane。

showmessagedialog(档案选择程式,“文件名无效”,

“文件名无效”,JOptionPaneerror_message);

别的{

尝试{

delete()文件名;

FileOutputStreamFOS=新的输出(文件名);

输出为新的对象(FOS);

图纸的记录;

输出writeint(指数);

对于(int=i0;i<索引;i++)

{

图P=itemlist[我];

输出的writeObject(P);

输出。

flush();}

()输出;

FOS。

();

}

赶上(IOExceptionIOE)

{

printstacktrace()IOE;

}

}

}

公共无效loadfile()

{

档案选择程式=新jfilechooser()JFileChooser;

然后在fileChooser.setfileselectionmode(文件对话框。

files_only);

int结果=然后在fileChooser.showMessageDialog(本);

如果(结果==文件对话框。

cancel_option)

返回;

文件的文件名,然后在fileChooser.getselectedfile();

canread()文件名;

如果(文件名==null||文件名。

getname()。

等于(“”))

JOptionPane。

showmessagedialog(档案选择程式,“文件名无效”,

“文件名无效”,JOptionPaneerror_message);

别的{

尝试{

FileInputStreamFIS=新的输入(文件名);

输入=新对象输入流(FIS);

图inputrecord;

国际countnumber=0;

countnumber=输入。

readint();

为(指数=0;指数小于countnumber;指数++)

{

inputrecord=(图)readobject()输入;

[索引]=inputrecord自行设计;

}

createnewitem();

()输入;

repaint();

}

赶上(个endoffileexception){

JOptionPane。

ShowMessageDialog(this,"there'snomorerecordsinthefile",

"JOptionPane.ERROR_MESSAGE";

}

Catch(ClassNotFoundExceptionclassNotFoundException){

JOptionPane.showMessageDialog(this,""cannotcreateobjects)",

"JOptionPane.ERROR_MESSAGE";

}

Catch(IOExceptionioException){

JOptionPane.showMessageDialog(this)errorreadingdatafromfile",

"JOptionPane.ERROR_MESSAGE";

}

}

}

PublicvoidnewFile()

{

Index=0;

CurrentChoice=3;

Color=Color.black;

Stroke=1.0f;

CreateNewItem();

Repaint();

}

Publicstaticvoidmain(Stringargs[])

{{try

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

Catch(ExceptionE){}

QingDrawPadnewPad=newQingDrawPad();

NewPad.addWindowListener

NewWindowAdapter(){

PublicvoidwindowClosing(WindowEventE)

{System.exit(0);}});

}

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1