《JAVA实验报告简单绘图程序精编.pdf》由会员分享,可在线阅读,更多相关《JAVA实验报告简单绘图程序精编.pdf(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、JAVA 实验报告简单绘图程序精编 Lele was written in 2021实验三实验三 绘制图形绘制图形一、实验目的一、实验目的学会 JButton 的使用,能进行 JButton 的基本操作,能使用 JButton 构造一个图形界面框架通过实验熟悉 swing图形用户界面程序设计熟悉 java 中的事件处理模型,每个事件动作代表的意义进一步提高编程能力,学会独立思考,学会上网查找资料决问题。二、实验内容二、实验内容设计一个程序,该程序能够绘制基本的图形(包括圆形、矩形、直线等);要求有一个简单易操作的用户界面(包括菜单、按钮等组件)。编写的程序要求能够实现保存和读取的功能。三、实验
2、代码三、实验代码importimport ;importimport;publicpublic classclass Main/*paramparam args*/publicpublic staticstatic voidvoid main(String args)ewInstance();catch(Exception e)();demo2=new Rect().getClass();demo3=;类名称 +();类名称 +();类名称 +();packagepackage base;importimportabstractabstract classclass GrapicsElem P
3、oint start;Point end;privateprivate Color color;publicpublic Color getColor()p);returnreturn color;publicpublic voidvoid setColor(Color color)publicpublic voidvoid setStart(Point start)publicpublic voidvoid setEnd(Point end)=color;=start;=end;publicpublic abstractabstract voidvoid doMousePress(Point
4、 p);publicpublic abstractabstract voidvoid doMouseDrag(Point p);publicpublic abstractabstract voidvoid doMouseRelease(Pointpublicpublic abstractabstract voidvoid draw(Graphics g);packagepackage base;importimportclassclass Line extendsextends GrapicsElem ArrayList track;publicpublic Line()track=newne
5、w ArrayList();Overridepublicpublic voidvoid draw(Graphics g)privateprivate voidvoid AddPoint(Point p)Overridepublicpublic voidvoid doMousePress(Point p)Override/TODOTODO Auto-generated method stubAddPoint(p);(p);forfor(intint i=0;i ()-1;i+)Point cur=(i);Point next=(i+1);,;publicpublic voidvoid doMou
6、seDrag(Point p)Overridepublicpublic voidvoid doMouseRelease(Point p)/TODOTODO Auto-generated method stubAddPoint(p);/TODOTODO Auto-generated method stubAddPoint(p);packagepackage base;importimportclassclass Rect extendsextends GrapicsElem intint x,y,width,height;Overridepublicpublic voidvoid draw(Gr
7、aphics g)/TODOTODO Auto-generated method stub(getX(),getY(),getWidth(),getHeight();Overridepublicpublic voidvoid doMousePress(Point p)(p);(p);Overridepublicpublic voidvoid doMouseDrag(Point p)(p);Overridepublicpublic voidvoid doMouseRelease(Point p)(p);publicpublic intint getX()returnreturn,;publicp
8、ublic intint getY()returnreturn,;publicpublic intint getWidth()returnreturnpublicpublic intint getHeight()returnreturnpackagepackage base;importimportclassclass Circle extendsextends Rect Overridepublicpublic voidvoid draw(Graphics g)/TODOTODO Auto-generated method stub(getX(),getY(),getWidth(),getH
9、eight();package winpaint;package winpaint;import ;import ;import;import;import;import;import;import;import class windowsPaint extends JPanelimport class windowsPaint extends JPanelimplements MouseListener,implements MouseListener,MouseMotionListenerMouseMotionListener String command=String command=矩
10、形矩形;List Lg=new ArrayList();List Lg=new ArrayList();GrapicsElem cur;GrapicsElem cur;JLabel label;JLabel label;public windowsPaint(String title)public windowsPaint(String title)private void init()private void init()init();init();addMouseListener(this);addMouseListener(this);addMouseMotionListener(thi
11、s);addMouseMotionListener(this);/*/*可以使用反射去掉判断语句、可以使用反射去掉判断语句、*Class g=();*Class g=();*();*();*return*return*/*/private GrapicsElem newInstance()private GrapicsElem newInstance()command=command=if(if(矩形矩形)return new Rect();return new Rect();else if(else if(圆形圆形)return new Circle();return new Circle(
12、);else if(else if(线线)return null;return null;return new Line();return new Line();OverrideOverridepublic void paint(Graphics g)public void paint(Graphics g)/TODO Auto-generated method stub/TODO Auto-generated method stub(g);(g);Graphics2D g2d=(Graphics2D)g;Graphics2D g2d=(Graphics2D)g;/();/();/(new B
13、asicStroke);(new BasicStroke);for(int i=0;i ();i+)for(int i=0;i ();i+)OverrideOverridepublic void repaint()public void repaint()/TODO Auto-generated method stub/TODO Auto-generated method stub/TODO Auto-generated method stub/TODO Auto-generated method stub/GrapicsElem ge=(i);GrapicsElem ge=(i);(g);(
14、g);/();();Thread thread=new Thread(new Thread thread=new Thread(newRunnable()Runnable()/OverrideOverridepublic void run()public void run()/TODO Auto-generated/TODO Auto-generatedmethod stubmethod stub/TODO Auto-/TODO Auto-try try SaveToFile();SaveToFile();catch(IOException e)catch(IOException e)gene
15、rated catch blockgenerated catch block/););();();();();ImageIcon bg=new ImageIcon();ImageIcon bg=new ImageIcon();if(bg!=null)if(bg!=null)if(label!=null)if(label!=null)/(label);(label);label=new JLabel(bg);label=new JLabel(bg);(0,0,(),();(0,0,(),();(label);(label);();();/private void SaveToFile()thro
16、wsprivate void SaveToFile()throwsIOException IOException/TODO Auto-generated method stub/TODO Auto-generated method stubBufferedImage bimg=newBufferedImage bimg=newBufferedImage(),(),;BufferedImage(),(),;OverrideOverridepublic void mousePressed(MouseEvent e)public void mousePressed(MouseEvent e)Poin
17、t p=new Point(),();Point p=new Point(),();cur=newInstance();cur=newInstance();(p);(p);Graphics2D g2d=();Graphics2D g2d=();(g2d);(g2d);(bimg,jpg,new File();(bimg,jpg,new File();(cur);(cur);OverrideOverridepublic void mouseDragged(MouseEvent e)public void mouseDragged(MouseEvent e)OverrideOverridepubl
18、ic void mouseReleased(MouseEvent e)public void mouseReleased(MouseEvent e)Point p=new Point(),();Point p=new Point(),();(p);(p);repaint();repaint();Point p=new Point(),();Point p=new Point(),();(p);(p);repaint();repaint();OverrideOverridepublic void mouseMoved(MouseEvent e)public void mouseMoved(Mou
19、seEvent e)OverrideOverridepublic void mouseClicked(MouseEvent e)public void mouseClicked(MouseEvent e)/TODO Auto-generated method stub/TODO Auto-generated method stub/TODO Auto-generated method stub/TODO Auto-generated method stub OverrideOverridepublic void mouseEntered(MouseEvent e)public void mou
20、seEntered(MouseEvent e)OverrideOverridepublic void mouseExited(MouseEvent e)public void mouseExited(MouseEvent e)/TODO Auto-generated method stub/TODO Auto-generated method stub/TODO Auto-generated method stub/TODO Auto-generated method stubpackage winpaint;package winpaint;import class WinToolBar e
21、xtends JToolBarimport class WinToolBar extends JToolBarimplements ActionListenerimplements ActionListenerpublic static String Command=public static String Command=矩形矩形;JButton btn1,btn2,btn3;JButton btn1,btn2,btn3;/windowsPaint winp;/windowsPaint winp;public WinToolBar(windowsPaint winP)public WinTo
22、olBar(windowsPaint winP)btn1=new JButton(btn1=new JButton(矩形矩形););btn2=new JButton(btn2=new JButton(圆形圆形););btn3=new JButton(btn3=new JButton(线线););/e)e)stubstub/(btn1);(btn1);(btn2);(btn2);(btn3);(btn3);(ActionListener)this);(ActionListener)this);(ActionListener)this);(ActionListener)this);(ActionL
23、istener)this);(ActionListener)this);=winP;=winP;JComboBox com=new JComboBox();JComboBox com=new JComboBox();(红色红色););(蓝色蓝色););(黑色黑色););(new ItemListener()(new ItemListener()OverrideOverridepublic void itemStateChanged(ItemEventpublic void itemStateChanged(ItemEvent/TODO Auto-generated method/TODO Au
24、to-generated method););(com);(com);OverrideOverridepublic void actionPerformed(ActionEvent e)public void actionPerformed(ActionEvent e)/TODO Auto-generated method stub/TODO Auto-generated method stubControl);Control);Control);Control);Control);Control);JButton btn=(JButton)();JButton btn=(JButton)();Command=();Command=();三、三、实验结果实验结果