1、java作业java作业java作业(java作业)软件工程 r1141 宋洋 16 java 作业public class qingdrawpad extends jframe private objectinputstream input;private objectoutputstream output;private jbutton choices ; private string names= newopen.save.pencil by,line,rect ,frect ,oval,foval ,circle,fcircle ,roundrect ,frrect ,rubber.c
2、olor.stroke.the word;private string = stylenames宋体, 隶书, 华文彩云, 仿宋 _ gb2312, 华文行楷 ,方正舒体, times new roman, serif, monospaced ,sonsserif, garamond.;private icon items ;jtoolbar buttonpanel;private jlabel statusbar;private drawpanel drawingarea;private int width = 850, height = 550;drawings itemlist = ne
3、w drawings 5;private int currentchoice = 3;int index = 0;private color color = color.black;int r, g, b;int f1, f2;the string style1;private float stroke = 1.0f;jcheckbox bold, italic;jcombobox styles;public qingdrawpad ()super (卿乐绘图画板 (中国风);jmenubar bar = new jmenubar ();jmenu filemenu = new jmenu (
4、文件);/ / filemenu.setmnemonic (f);jmenuitem newitem = new jmenuitem (新建);newitem.addactionlistener (new actionlistener () public void actionperformed (actionevent (e)newfile (););filemenu.add (newitem);jmenuitem saveitem = new jmenuitem (保存);saveitem.addactionlistener (new actionlistener () public vo
5、id actionperformed (actionevent (e)savefile (); /);filemenu.add (saveitem);jmenuitem loaditem = new jmenuitem (打开);loaditem.addactionlistener (new actionlistener () public void actionperformed (actionevent (e)loadfile (););filemenu.add (loaditem);filemenu.addseparator ();jmenuitem exititem = new jme
6、nuitem (退出);exititem.addactionlistener (new actionlistener () public void actionperformed (actionevent (e)system.exit (0););filemenu.add (exititem);bar.add (filemenu);jmenu colormenu = new jmenu (颜色);jmenuitem coloritem = new jmenuitem (选择颜色);coloritem.addactionlistener (new actionlistener () public
7、 void actionperformed (actionevent (e)choosecolor (););colormenu.add (coloritem);bar.add (colormenu);jmenu strokemenu = new jmenu (线条粗细);jmenuitem strokeitem = new jmenuitem (设置线条粗细);strokeitem.addactionlistener (new actionlistener () public void actionperformed (actionevent (e)setstroke (););stroke
8、menu.add (strokeitem);bar.add (strokemenu);jmenu helpmenu = new jmenu (帮助);jmenuitem aboutitem = new jmenuitem (关于画板);aboutitem.addactionlistener (new actionlistener () public void actionperformed (actionevent (e)joptionpane.showmessagedialog (null,画图板说明joptionpane.information _ message););helpmenu.
9、add (aboutitem);bar.add (helpmenu);items = new imageicon names.length;drawingarea = new drawpanel ();choices = new jbutton names.length;buttonpanel = new jtoolbar (jtoolbar.vertical);buttonpanel = new jtoolbar (jtoolbar.horizontal);buttonhandler is = new buttonhandler ();buttonhandler1 handler1 = ne
10、w buttonhandler1 ();for (int i = 0; i choices.length; i + +)items in = new imageicon (names in + .gif);choices in = new jbutton (, items in);choices in.settooltiptext (tiptext in);buttonpanel.add (choices in);for (int i = 3; in choices.length-3; i + +)choices in.addactionlistener (is);choices 0.adda
11、ctionlistener (new actionlistener () public void actionperformed (actionevent (e)newfile(););选择 1 。addactionlistener(新的actionlistener() public void actionPerformed(ActionEvent e)loadfile(););选择 2 。addactionlistener(新的actionlistener() public void actionPerformed(ActionEvent e)savefile(););选择选择,长度为3 a
12、ddactionlistener(handler1);选择选择,长度为2 addactionlistener(handler1);选择选择。length-1 addactionlistener(handler1);风格=新的JComboBox(stylenames);(8)setmaximumrowcount风格;additemlistener(风格。新的itemlistener() 公共无效itemstatechanged(itemevent E)style1 = stylenames getselectedindex() 风格;);大胆=新的复选框(“粗体”);斜体=新的复选框(“斜体”)
13、;checkboxhandler钱德勒=新checkboxhandler();大胆additemlistener(Chandler);斜体。additemlistener(钱德勒);JPanel wordpanel =新jpanel();buttonpanel添加(粗体);buttonpanel添加(斜体);buttonpanel添加(风格);风格。setminimumsize(新的维度(50, 20);风格。setmaximumsize(新的维度(100, 20);容器C = getcontentpane();超级setjmenubar(杆);(buttonpanel,总和4. Border
14、Layout。北);(drawingarea,总和4. BorderLayout。中心);状态=新jlabel();总和4.(StatusBar BorderLayout。南);状态栏设置(“欢迎使用卿乐绘图画板O(_)O”);createnewitem();setsize(宽度,高度);show();公共课buttonhandler实现ActionListenerpublic void actionPerformed(ActionEvent e)为(j = 3;J 选择。长度为3;j+)如果(如getsource() = =选择 J ) currentchoice = J;createnew
15、item();repaint();公共课buttonhandler1实现ActionListenerpublic void actionPerformed(ActionEvent e)如果(如getsource() = =选择选择,长度为3) choosecolor();如果(如getsource() = =选择选择,长度为2) setstroke();如果(如getsource() = =选择选择。length-1) JOptionPane。ShowMessageDialog (null),Please click the drawing board to select the locati
16、on of the input text,Hint, JOptionPane.INFORMATION_MESSAGE);CurrentChoice=14;CreateNewItem ();Repaint ();If (e.getSource () =bold)If (e.getStateChange () =ItemEvent.SELECTED)F1=Font.BOLD;ElseF1=Font.PLAIN;If (e.getSource () =italic)If (e.getStateChange () =ItemEvent.SELECTED)F2=Font.ITALIC;ElseF2=Fo
17、nt.PLAIN;Class DrawPanel extends JPanelPublic DrawPanel ()SetCursor (Cursor.getPredefinedCursor (Cursor.CROSSHAIR_CURSOR);SetBackground (Color.white);AddMouseListener (New mouseA ();AddMouseMotionListener (New mouseB ();Public void paintComponent (Graphics g)Super.paintComponent (g);Graphics2D g2d=
18、(Graphics2D) g;Int j=0;While (j 0)”);行程=浮parseFloat(输入);索引自行设计行程=行程;公共无效savefile()档案选择程式=新jfilechooser() JFileChooser;然后在fileChooser. setfileselectionmode(文件对话框。files_only);int结果=然后在fileChooser. showsavedialog(本);如果(结果= =文件对话框。cancel_option)返回;文件的文件名,然后在fileChooser. getselectedfile();canwrite()文件名;如
19、果(文件名= = null | |文件名。getname()。等于(“”)JOptionPane。showmessagedialog(档案选择程式,“文件名无效”,“文件名无效”,JOptionPane error_message);别的尝试delete()文件名;FileOutputStream FOS =新的输出(文件名);输出为新的对象(FOS);图纸的记录;输出writeint(指数);对于(int = i 0;i 索引;i + +)图P = itemlist 我;输出的writeObject(P);输出。flush();()输出;FOS。();赶上(IOException IOE)p
20、rintstacktrace() IOE;公共无效loadfile()档案选择程式=新jfilechooser() JFileChooser;然后在fileChooser. setfileselectionmode(文件对话框。files_only);int结果=然后在fileChooser. showMessageDialog(本);如果(结果= =文件对话框。cancel_option)返回;文件的文件名,然后在fileChooser. getselectedfile();canread()文件名;如果(文件名= = null | |文件名。getname()。等于(“”)JOptionP
21、ane。showmessagedialog(档案选择程式,“文件名无效”,“文件名无效”,JOptionPane error_message);别的尝试FileInputStream FIS =新的输入(文件名);输入=新对象输入流(FIS);图inputrecord;国际countnumber = 0;countnumber =输入。readint();为(指数= 0;指数小于countnumber;指数+ +)inputrecord =(图)readobject()输入;索引 = inputrecord自行设计;createnewitem();()输入;repaint();赶上(个endo
22、ffileexception)JOptionPane。ShowMessageDialog (this, theres no more records in the file,JOptionPane.ERROR_MESSAGE;Catch (ClassNotFoundException classNotFoundException) JOptionPane.showMessageDialog (this, cannot create objects),JOptionPane.ERROR_MESSAGE;Catch (IOException ioException) JOptionPane.sho
23、wMessageDialog (this) error reading data from file,JOptionPane.ERROR_MESSAGE;Public void newFile ()Index=0;CurrentChoice=3;Color=Color.black;Stroke=1.0f;CreateNewItem ();Repaint ();Public static void main (String args)tryUIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ();Catch (Exception E) QingDrawPad newPad=new QingDrawPad ();NewPad.addWindowListenerNew WindowAdapter () Public void windowClosing (WindowEvent E)System.exit (0););
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1