2022年2022年简单画板程序java .pdf

上传人:C****o 文档编号:33381436 上传时间:2022-08-10 格式:PDF 页数:17 大小:120.55KB
返回 下载 相关 举报
2022年2022年简单画板程序java .pdf_第1页
第1页 / 共17页
2022年2022年简单画板程序java .pdf_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《2022年2022年简单画板程序java .pdf》由会员分享,可在线阅读,更多相关《2022年2022年简单画板程序java .pdf(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、简单画板程序MyPanel.java import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Panel; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.io.Bu

2、fferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.util.Vector; public class MyPanel extends Panel /* * */ private static final long serialVersionUID = 1L; enum DrawType FreeDom, Line, Ellipse, Rectangle 名师资料总结 - - -精品资料欢迎下载 - - - - - - - -

3、 - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 17 页 - - - - - - - - - private VectorVector FreedomDatas = new VectorVector(); /freeDom时使用 private VectorVector lineDatas = new VectorVector(); /Line时使用 private VectorVector ellipseDatas = new VectorVector(); private VectorVector rectangleDatas = new

4、 VectorVector(); private Color lineColor = Color.blue; private int lineWidth = 5; private DrawType drawType = DrawType.FreeDom; private VectorVectorVector allWriteDatas = new VectorVectorVector(); private VectorVectorVector allReadDatas = new VectorVectorVector(); public MyPanel() addMouseListener(n

5、ew MouseAdapter() public void mousePressed(MouseEvent e) switch(drawType) case FreeDom: Point p = new Point(e.getX(),e.getY(); Vector newLine = new Vector(); newLine.add(p); FreedomDatas.add(newLine); break; case Line: /画直线 Point prePoint = new Point(e.getX(),e.getY(); 名师资料总结 - - -精品资料欢迎下载 - - - - -

6、 - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 17 页 - - - - - - - - - Vector newl = new Vector(); newl.add(prePoint); lineDatas.add(newl); break; case Ellipse: /画椭圆 Point newPoint = new Point(e.getX(),e.getY(); Vector newp = new Vector(); newp.add(newPoint); ellipseDatas.add(newp); break; c

7、ase Rectangle: /画矩形 Point nPoint = new Point(e.getX(),e.getY(); Vector newn = new Vector(); newn.add(nPoint); rectangleDatas.add(newn); break; public void mouseReleased(MouseEvent e) switch(drawType) case FreeDom: break; case Line: Point endPoint = new Point(e.getX(),e.getY(); int n = lineDatas.size

8、() - 1; Vector newl = lineDatas.get(n); newl.add(endPoint); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 17 页 - - - - - - - - - break; case Ellipse: Point endPoint2 = new Point(e.getX(),e.getY(); int n2 = ellipseDatas.size() - 1; Vector newl2 = ellipseDatas.g

9、et(n2); newl2.add(endPoint2); break; case Rectangle: Point endPoint3 = new Point(e.getX(),e.getY(); int n3 = rectangleDatas.size() - 1; Vector newl3 = rectangleDatas.get(n3); newl3.add(endPoint3); break; repaint(); /刷新画面 ); addMouseMotionListener(new MouseMotionAdapter() public void mouseDragged(Mou

10、seEvent e) switch(drawType) case FreeDom: Point p = new Point(e.getX(),e.getY(); int n = FreedomDatas.size()-1; /拿到最后一条线的位置 Vector lastLine = FreedomDatas.get(n); lastLine.add(p); break; case Line: /直线 break; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 17 页

11、- - - - - - - - - case Ellipse: /椭圆 break; case Rectangle: /矩形 break; /repaint(); /刷新画面 ); Override public void paint(Graphics g) g.clearRect(0, 0, getWidth(), getHeight(); g.setColor(lineColor); Graphics2D g_2D = (Graphics2D)g; BasicStroke stroke = new BasicStroke(lineWidth,BasicStroke.CAP_ROUND,Ba

12、sicStroke.JOIN_ROUND); g_2D.setStroke(stroke); switch(drawType) case FreeDom: Vector v; Point s,e; int i,j,m; int n = FreedomDatas.size(); for(i=0;in;i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 17 页 - - - - - - - - - v = FreedomDatas.get(i); m = v.size()

13、-1; for(j=0;jm;j+) s = (Point)v.get(j); e = (Point)v.get(j+1); g.drawLine(s.x, s.y, e.x, e.y); break; case Line: /直线 int ls = lineDatas.size(); for(int li=0;lils;li+) Vector twoPoints = lineDatas.get(li); Point startPoint = twoPoints.get(0); Point endPoint = twoPoints.get(1); g.drawLine(startPoint.x

14、,startPoint.y,endPoint.x,endPoint.y); break; case Ellipse: /椭圆 int es = ellipseDatas.size(); for(int ei=0;eies;ei+) Vector twoPoints = ellipseDatas.get(ei); Point startPoint = twoPoints.get(0); Point endPoint = twoPoints.get(1); int width = Math.abs(endPoint.x - startPoint.x); int height = Math.abs(

15、endPoint.y - startPoint.y); g.drawOval(startPoint.x,startPoint.y,width,height); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 17 页 - - - - - - - - - break; case Rectangle: /矩形 int rs = rectangleDatas.size(); for(int ri=0;rirs;ri+) Vector twoPoints = rectangleD

16、atas.get(ri); Point startPoint = twoPoints.get(0); Point endPoint = twoPoints.get(1); int width = Math.abs(endPoint.x - startPoint.x); int height = Math.abs(endPoint.y - startPoint.y); g.drawRect(startPoint.x,startPoint.y,width,height); break; public void setDrawType(int i) switch(i) case 0: drawTyp

17、e = DrawType.FreeDom; break; case 1: drawType = DrawType.Line; break; case 2: drawType = DrawType.Ellipse; break; case 3: drawType = DrawType.Rectangle; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 17 页 - - - - - - - - - break; repaint(); public void setLineW

18、igth(int i) lineWidth = i; repaint(); public void setLineColor(int i) switch(i) case 0: lineColor = Color.red; break; case 1: lineColor = Color.yellow; break; case 2: lineColor = Color.green; break; case 3: lineColor = Color.gray; break; case 4: lineColor = Color.black; break; case 5: lineColor = Co

19、lor.blue; break; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 17 页 - - - - - - - - - repaint(); public void cleanAll() FreedomDatas.clear(); lineDatas.clear(); ellipseDatas.clear(); rectangleDatas.clear(); repaint(); public void saveToFile() allWriteDatas.cle

20、ar(); allWriteDatas.add(FreedomDatas); allWriteDatas.add(lineDatas); allWriteDatas.add(ellipseDatas); allWriteDatas.add(rectangleDatas); try BufferedWriter output = new BufferedWriter(new FileWriter(out.dat); for(int s=0;sallWriteDatas.size();s+) VectorVector datas = allWriteDatas.get(s); int ss = d

21、atas.size(); output.write(new Integer(ss).toString(); output.newLine(); for(int i=0;idatas.size();i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 17 页 - - - - - - - - - Vector ps = datas.get(i); int pp = ps.size(); output.write(new Integer(pp).toString(); ou

22、tput.newLine(); for(int j=0;jps.size();j+) Point p = ps.get(j); int x = (int)p.getX(); int y = (int)p.getY(); output.write(new Integer(x).toString(); output.newLine(); output.write(new Integer(y).toString(); output.newLine(); output.close(); catch(Exception e) e.printStackTrace(); public void readFr

23、omFile() FreedomDatas.clear(); lineDatas.clear(); ellipseDatas.clear(); rectangleDatas.clear(); try 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 17 页 - - - - - - - - - BufferedReader input = new BufferedReader(new FileReader(out.dat); for(int i=0;i4;i+) Vect

24、orVector datas = new VectorVector(); String str = input.readLine(); int size = Integer.parseInt(str); for(int j=0;jsize;j+) Vector ps = new Vector(); String st = input.readLine(); int pp = Integer.parseInt(st); for(int k=0;kpp;k+) String sx = input.readLine(); int x = Integer.parseInt(sx); String sy

25、 = input.readLine(); int y = Integer.parseInt(sy); Point p = new Point(x,y); ps.add(p); datas.add(ps); allReadDatas.add(datas); VectorVector clone1 = (VectorVector) allReadDatas.get(0).clone(); FreedomDatas = clone1; VectorVector clone2 = (VectorVector) allReadDatas.get(1).clone(); lineDatas = clone

26、2; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 17 页 - - - - - - - - - VectorVector clone3 = (VectorVector) allReadDatas.get(2).clone(); ellipseDatas = clone3; VectorVector clone4 = (VectorVector) allReadDatas.get(3).clone(); rectangleDatas = clone4; repaint

27、(); catch (Exception e) e.printStackTrace(); drawFrame.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawFrame extends JFrame /* * */ private static final long serialVersionUID = 1L; public DrawFrame() super(20102100227 王嘉铠 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - -

28、- - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 17 页 - - - - - - - - - /菜单栏 JMenuBar myBar = new JMenuBar(); setJMenuBar(myBar); JMenu m = new JMenu(文件 ), new JMenu(图形 ), new JMenu( 线条宽度 ), new JMenu( 颜色 ); JMenuItem mI = new JMenuItem( 打开 ),new JMenuItem( 新建 ),new JMenuItem(保存 ), new JMenuItem(

29、自由 ),new JMenuItem(直线 ), new JMenuItem(椭圆 ),new JMenuItem(矩形 ), new JMenuItem(5), new JMenuItem(10),new JMenuItem(15),new JMenuItem(20), new JMenuItem( 红色 ),new JMenuItem( 黄色 ),new JMenuItem(绿色 ), new JMenuItem( 灰色 ),new JMenuItem( 黑色 ), new JMenuItem(蓝色 ) ; int i; int j; for(i=0;im.length;i+) myBar

30、.add(mi); for(j=0;jmIi.length;j+) mi.add(mIij); /画板名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 17 页 - - - - - - - - - Container contentPane = getContentPane(); contentPane.setBounds(0, myBar.getHeight(),getWidth(),getHeight() - myBar.getHeight(); final MyPa

31、nel panel = new MyPanel(); contentPane.add(panel); /各种监听器 mI00.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stub panel.readFromFile(); ); mI01.addActionListener(new ActionListener() /新建 Override public void actionPerformed

32、(ActionEvent arg0) / TODO Auto-generated method stub panel.cleanAll(); ); mI02.addActionListener(new ActionListener() /保存 Override 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 17 页 - - - - - - - - - public void actionPerformed(ActionEvent arg0) / TODO Auto-g

33、enerated method stub panel.saveToFile(); ); for(int type=0;type4;type+) /图形 final int t = type; mI1type.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) panel.setDrawType(t); ); for(int type=0;type4;type+) /线宽 mI2type.addActionListener(new ActionListener() O

34、verride public void actionPerformed(ActionEvent e) JMenuItem j = (JMenuItem)e.getSource(); int width = Integer.parseInt(j.getText().toString(); panel.setLineWigth(width); ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 17 页 - - - - - - - - - for(int type=0;t

35、ype6;type+) /颜色 final int t = type; mI3type.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) panel.setLineColor(t); ); mouseDraw.java import javax.swing.JFrame; public class MouseDraw /* * param args */ public static void main(String args) / TODO Auto-genera

36、ted method stub DrawFrame app = new DrawFrame(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setBounds(450, 100, 500, 500); app.setVisible(true); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 17 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 17 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁