畫圖板Java課程設(shè)計(jì)_第1頁
畫圖板Java課程設(shè)計(jì)_第2頁
畫圖板Java課程設(shè)計(jì)_第3頁
畫圖板Java課程設(shè)計(jì)_第4頁
畫圖板Java課程設(shè)計(jì)_第5頁
已閱讀5頁,還剩36頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、 面向?qū)ο蟪绦蛟O(shè)計(jì)課程設(shè)計(jì)報告題 目: 畫圖板設(shè)計(jì)與實(shí)現(xiàn) 院 (系): 信息科學(xué)與工程學(xué)院 專業(yè)班級: 計(jì)算機(jī)科學(xué)與技術(shù)1201班 學(xué)生姓名: 程偉 學(xué) 號: 20121183011 指導(dǎo)教師: 吳奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日華中科技大學(xué)武昌分校制 面向?qū)ο蟪绦蛟O(shè)計(jì) 課程設(shè)計(jì)任務(wù)書一、設(shè)計(jì)(調(diào)查報告/論文)題目畫圖板設(shè)計(jì)與實(shí)現(xiàn)二、設(shè)計(jì)(調(diào)查報告/論文)主要內(nèi)容內(nèi)容:完成一個類似于windows畫圖板的繪畫程序。軟件主要有菜單欄,工具欄,畫圖區(qū)組成,菜單欄具有新建,打開,保存等常見的功能;工具欄可以選擇用戶需要的圖形樣式,設(shè)置參數(shù),插入文本等操作?;竟?/p>

2、能與要求:1 新建一個空的圖形文件:用于繪制新的圖形。2 打開或保存一個圖形文件,便于對圖形文件的管理。3 繪制一些基本的畫圖組件:直線,矩形,實(shí)心矩形,橢圓,實(shí)心橢圓,圓,實(shí)心圓,圓角矩形,實(shí)心圓角矩形等。4 可以用鼠標(biāo)在繪制畫板上單擊拖動進(jìn)行隨筆畫。5 橡皮擦:可以將已繪制好的圖形擦掉。三、原始資料1.面向?qū)ο蟪绦蛟O(shè)計(jì)課程設(shè)計(jì)指導(dǎo)書2. 耿祥義JAVA大學(xué)實(shí)用教程北京:清華大學(xué)出版社,2009四、要求的設(shè)計(jì)(調(diào)查/論文)成果1.課程設(shè)計(jì)報告2.課程設(shè)計(jì)軟件五、進(jìn)程安排布置任務(wù),查找資料、需求分析1天總體設(shè)計(jì)1天詳細(xì)設(shè)計(jì)1.5天編制源程序?qū)崿F(xiàn)3.5天測試與修改1天撰寫課設(shè)報告2天六、主要參考

3、資料1 張廣彬. Java課程設(shè)計(jì)案例精編(第二版).北京:清華大學(xué)出版社, 2011. 2 耿祥義. Java課程設(shè)計(jì)(第二版).北京:清華大學(xué)出版社, 2008.3 耿祥義JAVA大學(xué)實(shí)用教程北京:清華大學(xué)出版社,20094 鄒林達(dá)陳國君 Java2程序設(shè)計(jì)基礎(chǔ)北京:清華大學(xué)出版社,20105 鄒林達(dá)Java2程序設(shè)計(jì)基礎(chǔ)實(shí)驗(yàn)指導(dǎo)北京:清華大學(xué),2010指導(dǎo)教師(簽名): 20 年 月 日目 錄1需求與總體設(shè)計(jì)11.1需求分析11.2總體設(shè)計(jì)12詳細(xì)設(shè)計(jì)32.1詳細(xì)設(shè)計(jì)思路32.2詳細(xì)主代碼43編碼實(shí)現(xiàn)74系統(tǒng)運(yùn)行與測試33總結(jié)371需求與總體設(shè)計(jì)1.1需求分析 平時生活中,我們會需要處理

4、一些基本的圖像,也有可能需要花一些簡單的圖像,平時所使用的圖形處理工具有PhotoShop、Windows 畫圖工具等,其中PhotoShop 是一款非常強(qiáng)大的圖形處理工具,windows 畫圖工具則是一款較為簡單的畫圖工具,功能較為簡單,用Windows畫圖工具,是一種比較簡單與具有代表性的畫圖工具,雖然功能不夠強(qiáng)大,但具有大多圖片處理程序所必需的基本功能:鉛筆畫圖、各種數(shù)學(xué)函數(shù)圖形、填色、取色、橡皮擦等等。這次試驗(yàn)中我做的就是一個仿照windows畫板的程序。 考慮到畫圖板的實(shí)際需要,畫圖板要實(shí)現(xiàn)打開,保存圖片,用于打開本地圖片或保存當(dāng)前圖片至本地;選擇需要繪制的圖形,用于選擇需要繪制的圖

5、形樣式,如直線,矩形,橢圓等;設(shè)置背景色,畫筆色以及畫筆大小,用于設(shè)置畫板背景顏色,畫筆顏色和當(dāng)前畫筆粗細(xì);擦除錯誤操作;顯示幫助信息等。1.2總體設(shè)計(jì)總體功能圖 總體設(shè)計(jì)類圖2詳細(xì)設(shè)計(jì)2.1詳細(xì)設(shè)計(jì)思路系統(tǒng)結(jié)構(gòu)設(shè)計(jì):菜單欄的設(shè)計(jì)及功能實(shí)現(xiàn):菜單欄主要包括文件、編輯、設(shè)置、幫助等常見菜單。其中文件菜單有新建、打開、保存、另存為、退出等功能;編輯菜單有撤銷、恢復(fù)等功能;設(shè)置菜單含有對畫筆顏色、畫筆大小等的設(shè)置功能;幫助菜單提供畫圖板的幫助信息。畫圖區(qū)的設(shè)計(jì):對圖片的編輯處理區(qū)。工具欄的設(shè)計(jì):根據(jù)實(shí)際需要選擇不同的工具完成畫圖操作。其他功能設(shè)計(jì)??傮w流程圖2.2詳細(xì)類設(shè)計(jì)MiniDrawPad :

6、主類,擴(kuò)展了JFrame類,用來生成主界面。Drawings類:是畫圖類,基本圖形的單元。其各個子類用以實(shí)現(xiàn)畫各種圖形包括以下幾個子類: 1.Line類:繪制直線;2. Rect類:繪制矩形;3. fillOval類:繪制實(shí)心橢圓;4. fillRect類:繪制實(shí)心矩形;5. Oval類:繪制橢圓;6. Cicle類:繪制圓;7. fillCicle類:繪制實(shí)心圓;8. RoundRect類:繪制圓角矩形;9. fillRoundRect類:繪制實(shí)心圓角矩形;10. Pecil類:自由筆;ButtonHandler類:按鈕偵聽器ButtonHanler類,內(nèi)部類,用來偵聽基本按鈕的操作Butt

7、onHandler1類:用來偵聽顏色選擇、畫筆粗細(xì)設(shè)置、文字輸入按鈕的操作checkBoxHandler類:選擇字體風(fēng)格時候用到的事件偵聽器類,加入到字體風(fēng)格的選擇框中DrawPanel類:畫圖面板類,用來畫圖mouseA類:鼠標(biāo)事件mouseA類,繼承了MouseAdapter,用來完成鼠標(biāo)相應(yīng)事件操作mouseB類: 鼠標(biāo)事件mouseB類繼承了MouseMotionAdapter,用來完成鼠標(biāo)拖動和鼠標(biāo)移動時的相應(yīng)操作 3編碼實(shí)現(xiàn)源代碼import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import javax

8、.swing.*;import java.io.*;import java.util.*;public class MiniDrawPad extends JFrame private ObjectInputStream input; private ObjectOutputStream output; /定義輸入輸出流,用來調(diào)用和保存圖像文件 private JButton choices; /按鈕數(shù)組,存放以下名稱的功能按鈕 private String names= "New", /新建 "Open", /打開 "Save",

9、/"保存" /*接下來是我們的畫圖板上面有的基本的幾個繪圖單元按鈕*/ "Pencil", /鉛筆畫,也就是用鼠標(biāo)拖動著隨意繪圖 "Line", /繪制直線 "Rect", /繪制空心矩形 "fRect", /繪制以指定顏色填充的實(shí)心矩形 "Oval", /繪制空心橢圓 "fOval", /繪制以指定顏色填充的實(shí)心橢圓 "Circle", /繪制圓形 "fCircle", /繪制以指定顏色填充的實(shí)心圓形 "

10、RoundRect", /繪制空心圓角矩形 "frRect", /繪制以指定顏色填充的實(shí)心圓角矩形 "Rubber", /橡皮擦,可用來擦去已經(jīng)繪制好的圖案 "Color", /選擇顏色按鈕,可用來選擇需要的顏色 "Stroke", /選擇線條粗細(xì)的按鈕,輸入需要的數(shù)值可以實(shí)現(xiàn)繪圖線條粗細(xì)的變化 "Word" /輸入文字按鈕,可以在繪圖板上實(shí)現(xiàn)文字輸入 ; private String styleNames= " 宋體 " , " 隸書 " ,

11、" 華文彩云 " , " 仿宋_GB2312 " , " 華文行楷 " , " 方正舒體 " , " Times New Roman " , " Serif " , " Monospaced " , " SonsSerif " , " Garamond " ; /可供選擇的字體項(xiàng) /當(dāng)然這里的靈活的結(jié)構(gòu)可以讓讀者自己隨意添加系統(tǒng)支持的字體 private Icon items; private String tipT

12、ext= /這里是鼠標(biāo)移動到相應(yīng)按鈕上面上停留時給出的提示說明條 /讀者可以參照上面的按鈕定義對照著理解 "Draw a new picture", "Open a saved picture", "Save current drawing", "Draw at will", "Draw a straight line", "Draw a rectangle", "Fill a ractangle", "Draw an oval", &

13、quot;Fill an oval", "Draw a circle", "Fill a circle", "Draw a round rectangle", "Fill a round rectangle", "Erase at will", "Choose current drawing color", "Set current drawing stroke", "Write down what u want" ; JTo

14、olBar buttonPanel ; /定義按鈕面板 private JLabel statusBar; /顯示鼠標(biāo)狀態(tài)的提示條 private DrawPanel drawingArea; /畫圖區(qū)域 private int width=1000,height=600; /定義畫圖區(qū)域初始大小 drawings itemList=new drawings5000; /用來存放基本圖形的數(shù)組 private int currentChoice=3; /設(shè)置默認(rèn)畫圖狀態(tài)為隨筆畫 int index=0; /當(dāng)前已經(jīng)繪制的圖形數(shù)目 private Color color=Color.black;

15、 /當(dāng)前畫筆顏色 int R,G,B; /用來存放當(dāng)前色彩值 int f1,f2; /用來存放當(dāng)前字體風(fēng)格 String style1; /用來存放當(dāng)前字體 private float stroke=1.0f; /設(shè)置畫筆粗細(xì),默認(rèn)值為1.0f JCheckBox bold,italic; /定義字體風(fēng)格選擇框 /bold為粗體,italic為斜體,二者可以同時使用 JComboBox styles; public MiniDrawPad() /構(gòu)造函數(shù) super("Drawing Pad"); JMenuBar bar=new JMenuBar(); /定義菜單條 JM

16、enu fileMenu=new JMenu("文件"); fileMenu.setMnemonic('F'); /新建文件菜單條 JMenuItem newItem=new JMenuItem("新建"); newItem.setMnemonic('N'); newItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) newFile(); /如果被觸發(fā),則調(diào)用新建文件函數(shù)段 ); fileMenu.a

17、dd(newItem); /保存文件菜單項(xiàng) JMenuItem saveItem=new JMenuItem("保存"); saveItem.setMnemonic('S'); saveItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) saveFile(); /如果被觸發(fā),則調(diào)用保存文件函數(shù)段 ); fileMenu.add(saveItem); /打開文件菜單項(xiàng) JMenuItem loadItem=new JMenuItem(&q

18、uot;打開"); loadItem.setMnemonic('L'); loadItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) loadFile(); /如果被觸發(fā),則調(diào)用打開文件函數(shù)段 ); fileMenu.add(loadItem); fileMenu.addSeparator(); /退出菜單項(xiàng) JMenuItem exitItem=new JMenuItem("退出"); exitItem.setMnemoni

19、c('X'); exitItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) System.exit(0); /如果被觸發(fā),則退出畫圖板程序 ); fileMenu.add(exitItem); bar.add(fileMenu); /設(shè)置顏色菜單條 JMenu colorMenu=new JMenu("顏色"); colorMenu.setMnemonic('C'); /選擇顏色菜單項(xiàng) JMenuItem colorIt

20、em=new JMenuItem("選擇顏色"); colorItem.setMnemonic('O'); colorItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) chooseColor(); /如果被觸發(fā),則調(diào)用選擇顏色函數(shù)段 ); colorMenu.add(colorItem); bar.add(colorMenu); /設(shè)置線條粗細(xì)菜單條 JMenu strokeMenu=new JMenu("像素")

21、; strokeMenu.setMnemonic('S'); /設(shè)置線條粗細(xì)菜單項(xiàng) JMenuItem strokeItem=new JMenuItem("設(shè)置像素"); strokeItem.setMnemonic('K'); strokeItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) setStroke(); ); strokeMenu.add(strokeItem); bar.add(strokeMenu);

22、/設(shè)置提示菜單條 JMenu helpMenu=new JMenu("幫助"); helpMenu.setMnemonic('H'); /設(shè)置提示菜單項(xiàng) JMenuItem aboutItem=new JMenuItem("關(guān)于"); aboutItem.setMnemonic('A'); aboutItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) JOptionPane.showMessageDi

23、alog(null, "這是一個迷你畫圖板!nCopyright (c) 2014 華中科技大學(xué)武昌分校 ", " 畫圖板程序說明 ", JOptionPane.INFORMATION_MESSAGE ); ); helpMenu.add(aboutItem); bar.add(helpMenu); items=new ImageIconnames.length; /創(chuàng)建各種基本圖形的按鈕 drawingArea=new DrawPanel(); choices=new JButtonnames.length; buttonPanel = new JTo

24、olBar( JToolBar.VERTICAL ) ; buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ; ButtonHandler handler=new ButtonHandler(); ButtonHandler1 handler1=new ButtonHandler1(); /導(dǎo)入我們需要的圖形圖標(biāo),這些圖標(biāo)都存放在與源文件相同的目錄下面 for(int i=0;i<choices.length;i+) /itemsi=new ImageIcon( MiniDrawPad.class.getResource(namesi +&

25、quot;.gif"); /如果在jbuilder下運(yùn)行本程序,則應(yīng)該用這條語句導(dǎo)入圖片 itemsi=new ImageIcon(namesi + ".gif"); /默認(rèn)的在jdk或者jcreator下運(yùn)行,用此語句導(dǎo)入圖片 choicesi=new JButton("",itemsi); choicesi.setToolTipText(tipTexti); buttonPanel.add(choicesi); /將動作偵聽器加入按鈕里面 for(int i=3;i<choices.length-3;i+) choicesi.addA

26、ctionListener(handler); choices0.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) newFile(); ); choices1.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) loadFile(); ); choices2.addActionListener( new ActionListener() public void ac

27、tionPerformed(ActionEvent e) saveFile(); ); choiceschoices.length-3.addActionListener(handler1); choiceschoices.length-2.addActionListener(handler1); choiceschoices.length-1.addActionListener(handler1); /字體風(fēng)格選擇 styles=new JComboBox(styleNames); styles.setMaximumRowCount(8); styles.addItemListener( n

28、ew ItemListener() public void itemStateChanged(ItemEvent e) style1=styleNamesstyles.getSelectedIndex(); ); /字體選擇 bold=new JCheckBox("黑體字"); italic=new JCheckBox("斜體字"); checkBoxHandler cHandler=new checkBoxHandler(); bold.addItemListener(cHandler); italic.addItemListener(cHandler

29、); JPanel wordPanel=new JPanel(); buttonPanel.add(bold); buttonPanel.add(italic); buttonPanel.add(styles); styles.setMinimumSize( new Dimension ( 500, 200 ) ); styles.setMaximumSize(new Dimension ( 1000, 200 ) ); Container c=getContentPane(); super.setJMenuBar( bar ); c.add(buttonPanel,BorderLayout.

30、NORTH); c.add(drawingArea,BorderLayout.CENTER); statusBar=new JLabel(); c.add(statusBar,BorderLayout.SOUTH); statusBar.setText(" To The Little Drawing Pad! :)"); createNewItem(); setSize(width,height); show(); /按鈕偵聽器ButtonHanler類,內(nèi)部類,用來偵聽基本按鈕的操作 public class ButtonHandler implements Action

31、Listener public void actionPerformed(ActionEvent e) for(int j=3;j<choices.length-3;j+) if(e.getSource()=choicesj) currentChoice=j; createNewItem(); repaint(); /按鈕偵聽器ButtonHanler1類,用來偵聽顏色選擇、畫筆粗細(xì)設(shè)置、文字輸入按鈕的操作public class ButtonHandler1 implements ActionListener public void actionPerformed(ActionEven

32、t e) if(e.getSource()=choiceschoices.length-3) chooseColor(); if(e.getSource()=choiceschoices.length-2) setStroke(); if(e.getSource()=choiceschoices.length-1) JOptionPane.showMessageDialog(null, "Please hit the drawing pad to choose the word input position", "Hint",JOptionPane.IN

33、FORMATION_MESSAGE ); currentChoice=14; createNewItem(); repaint(); /鼠標(biāo)事件mouseA類,繼承了MouseAdapter,用來完成鼠標(biāo)相應(yīng)事件操作class mouseA extends MouseAdapter public void mousePressed(MouseEvent e) statusBar.setText(" Mouse Pressed :" + e.getX() + ", " + e.getY() + ""); /設(shè)置狀態(tài)提示 itemList

34、index.x1=itemListindex.x2=e.getX(); itemListindex.y1=itemListindex.y2=e.getY(); /如果當(dāng)前選擇的圖形是隨筆畫或者橡皮擦,則進(jìn)行下面的操作 if(currentChoice=3|currentChoice=13) itemListindex.x1=itemListindex.x2=e.getX(); itemListindex.y1=itemListindex.y2=e.getY(); index+; createNewItem(); /如果當(dāng)前選擇的圖形式文字輸入,則進(jìn)行下面操作 if(currentChoice=

35、14) itemListindex.x1=e.getX(); itemListindex.y1=e.getY(); String input; input=JOptionPane.showInputDialog( "Please input the text you want!"); itemListindex.s1=input; itemListindex.x2=f1; itemListindex.y2=f2; itemListindex.s2=style1; index+; currentChoice=14; createNewItem(); drawingArea.r

36、epaint(); public void mouseReleased(MouseEvent e) statusBar.setText(" Mouse Released :" + e.getX() + ", " + e.getY() + ""); if(currentChoice=3|currentChoice=13) itemListindex.x1=e.getX(); itemListindex.y1=e.getY(); itemListindex.x2=e.getX(); itemListindex.y2=e.getY(); r

37、epaint(); index+; createNewItem(); public void mouseEntered(MouseEvent e) statusBar.setText(" Mouse Entered :" + e.getX() + ", " + e.getY() + ""); public void mouseExited(MouseEvent e) statusBar.setText(" Mouse Exited :" + e.getX() + ", " + e.getY()

38、+ ""); /鼠標(biāo)事件mouseB類繼承了MouseMotionAdapter,用來完成鼠標(biāo)拖動和鼠標(biāo)移動時的相應(yīng)操作 class mouseB extends MouseMotionAdapter public void mouseDragged(MouseEvent e) statusBar.setText(" " + " Mouse Dragged :" + e.getX() + ", " + e.getY() + ""); if(currentChoice=3|currentChoic

39、e=13) itemListindex-1.x1=itemListindex.x2=itemListindex.x1=e.getX(); itemListindex-1.y1=itemListindex.y2=itemListindex.y1=e.getY(); index+; createNewItem(); else itemListindex.x2=e.getX(); itemListindex.y2=e.getY(); repaint(); public void mouseMoved(MouseEvent e) statusBar.setText(" Mouse Moved

40、 :" + e.getX() + ", " + e.getY() + ""); /選擇字體風(fēng)格時候用到的事件偵聽器類,加入到字體風(fēng)格的選擇框中private class checkBoxHandler implements ItemListener public void itemStateChanged(ItemEvent e) if(e.getSource()=bold) if(e.getStateChange()=ItemEvent.SELECTED) f1=Font.BOLD; else f1=Font.PLAIN; if(e.getS

41、ource()=italic) if(e.getStateChange()=ItemEvent.SELECTED) f2=Font.ITALIC; else f2=Font.PLAIN; /畫圖面板類,用來畫圖 class DrawPanel extends JPanel public DrawPanel() setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); setBackground(Color.white); addMouseListener(new mouseA(); addMouseMotionListener

42、(new mouseB(); public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; /定義畫筆 int j=0; while (j<=index) draw(g2d,itemListj); j+; void draw(Graphics2D g2d,drawings i) i.draw(g2d); /將畫筆傳入到各個子類中,用來完成各自的繪圖 /新建一個畫圖基本單元對象的程序段 void createNewItem() if(currentChoice=14

43、) /進(jìn)行相應(yīng)的游標(biāo)設(shè) drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR); else drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); switch (currentChoice) case 3: itemListindex=new Pencil(); break; case 4: itemListindex=new Line(); break; case 5: itemListindex=new Rect(); break; case 6: itemListindex=new fillRect(); break; case 7: itemListindex=new Oval(); break; case 8: itemListindex=new fillOval(); break; case 9: itemListindex=new Circle(); break; case 10: itemListindex=new fillCi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論