用java写的一个画板demo 是Java课程的一个作业,比较简单 直接贴代码,有不完善的地方,但是因为对swing编程没什么兴趣 所以暂时不想改了

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.BufferedReader;
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}private Vector<Vector<Point>> FreedomDatas = new Vector<Vector<Point>>(); //freeDom时使用private Vector<Vector<Point>> lineDatas = new Vector<Vector<Point>>(); //Line时使用private Vector<Vector<Point>> ellipseDatas = new Vector<Vector<Point>>();private Vector<Vector<Point>> rectangleDatas = new Vector<Vector<Point>>();private Color lineColor = Color.blue;private int lineWidth = 5;private DrawType drawType = DrawType.FreeDom;private Vector<Vector<Vector<Point>>> allWriteDatas = new Vector<Vector<Vector<Point>>>();private Vector<Vector<Vector<Point>>> allReadDatas = new Vector<Vector<Vector<Point>>>();public MyPanel(){addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e){switch(drawType){case FreeDom:Point p = new Point(e.getX(),e.getY());Vector<Point> newLine = new Vector<Point>();newLine.add(p);FreedomDatas.add(newLine); break;case Line://画直线Point prePoint = new Point(e.getX(),e.getY());Vector<Point> newl = new Vector<Point>();newl.add(prePoint);lineDatas.add(newl);break;case Ellipse://画椭圆Point newPoint = new Point(e.getX(),e.getY());Vector<Point> newp = new Vector<Point>();newp.add(newPoint);ellipseDatas.add(newp);break;case Rectangle://画矩形Point nPoint = new Point(e.getX(),e.getY());Vector<Point> newn = new Vector<Point>();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() - 1;Vector<Point> newl = lineDatas.get(n);newl.add(endPoint);break;case Ellipse:Point endPoint2 = new Point(e.getX(),e.getY());int n2 = ellipseDatas.size() - 1;Vector<Point> newl2 = ellipseDatas.get(n2);newl2.add(endPoint2);break;case Rectangle:Point endPoint3 = new Point(e.getX(),e.getY());int n3 = rectangleDatas.size() - 1;Vector<Point> newl3 = rectangleDatas.get(n3);newl3.add(endPoint3);break;}repaint(); //刷新画面}});addMouseMotionListener(new MouseMotionAdapter(){public void mouseDragged(MouseEvent e){switch(drawType){case FreeDom:Point p = new Point(e.getX(),e.getY());int n = FreedomDatas.size()-1; //拿到最后一条线的位置Vector<Point> lastLine = FreedomDatas.get(n);lastLine.add(p);break;case Line://直线break;case Ellipse://椭圆break;case Rectangle://矩形break;}//repaint(); //刷新画面}});}@Overridepublic 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,BasicStroke.JOIN_ROUND);g_2D.setStroke(stroke);switch(drawType){case FreeDom:Vector<Point> v;Point s,e;int i,j,m;int n = FreedomDatas.size();for(i=0;i<n;i++){v = FreedomDatas.get(i);m = v.size()-1;for(j=0;j<m;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;li<ls;li++){Vector<Point> twoPoints = lineDatas.get(li);Point startPoint = twoPoints.get(0);Point endPoint = twoPoints.get(1);g.drawLine(startPoint.x,startPoint.y,endPoint.x,endPoint.y);}break;case Ellipse://椭圆int es = ellipseDatas.size();for(int ei=0;ei<es;ei++){Vector<Point> 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(endPoint.y - startPoint.y);g.drawOval(startPoint.x,startPoint.y,width,height);}break;case Rectangle://矩形int rs = rectangleDatas.size();for(int ri=0;ri<rs;ri++){Vector<Point> twoPoints = rectangleDatas.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:drawType = DrawType.FreeDom;break;case 1:drawType = DrawType.Line;break;case 2:drawType = DrawType.Ellipse;break;case 3:drawType = DrawType.Rectangle;break;}repaint();}public void setLineWigth(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 = Color.blue;break;}repaint();}public void cleanAll(){FreedomDatas.clear();lineDatas.clear();ellipseDatas.clear();rectangleDatas.clear();repaint();}public void saveToFile(){allWriteDatas.clear();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;s<allWriteDatas.size();s++){Vector<Vector<Point>> datas = allWriteDatas.get(s);int ss = datas.size();output.write(new Integer(ss).toString());output.newLine();for(int i=0;i<datas.size();i++){Vector<Point> ps = datas.get(i);int pp = ps.size();output.write(new Integer(pp).toString());output.newLine();for(int j=0;j<ps.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 readFromFile(){FreedomDatas.clear();lineDatas.clear();ellipseDatas.clear();rectangleDatas.clear();try{BufferedReader input = new BufferedReader(new FileReader("out.dat"));for(int i=0;i<4;i++){Vector<Vector<Point>> datas = new Vector<Vector<Point>>();String str = input.readLine();int size = Integer.parseInt(str);for(int j=0;j<size;j++){Vector<Point> ps = new Vector<Point>();String st = input.readLine();int pp = Integer.parseInt(st);for(int k=0;k<pp;k++){String sx = input.readLine();int x = Integer.parseInt(sx);String sy = input.readLine();int y = Integer.parseInt(sy);Point p = new Point(x,y);ps.add(p);}datas.add(ps);}allReadDatas.add(datas);}Vector<Vector<Point>> clone1 = (Vector<Vector<Point>>) allReadDatas.get(0).clone();FreedomDatas = clone1;Vector<Vector<Point>> clone2 = (Vector<Vector<Point>>) allReadDatas.get(1).clone();lineDatas = clone2;Vector<Vector<Point>> clone3 = (Vector<Vector<Point>>) allReadDatas.get(2).clone();ellipseDatas = clone3;Vector<Vector<Point>> clone4 = (Vector<Vector<Point>>) allReadDatas.get(3).clone();rectangleDatas = clone4;repaint();}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 王嘉铠");//菜单栏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("自由"),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;i<m.length;i++){myBar.add(m[i]);for(j=0;j<mI[i].length;j++){m[i].add(mI[i][j]);}}//画板Container contentPane = getContentPane();contentPane.setBounds(0, myBar.getHeight(),getWidth(),getHeight() - myBar.getHeight());final MyPanel panel = new MyPanel();contentPane.add(panel);//各种监听器mI[0][0].addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubpanel.readFromFile();}});mI[0][1].addActionListener(new ActionListener(){//新建@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubpanel.cleanAll();}});mI[0][2].addActionListener(new ActionListener(){//保存@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubpanel.saveToFile();}});for(int type=0;type<4;type++){  //图形final int t = type;mI[1][type].addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {panel.setDrawType(t);} });}for(int type=0;type<4;type++){//线宽mI[2][type].addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {JMenuItem j = (JMenuItem)e.getSource();int width = Integer.parseInt(j.getText().toString());panel.setLineWigth(width);}});}for(int type=0;type<6;type++){//颜色final int t = type;mI[3][type].addActionListener(new ActionListener(){@Overridepublic 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-generated method stubDrawFrame app = new DrawFrame();app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);app.setBounds(450, 100, 500, 500);app.setVisible(true);}}

运行效果

源码下载:JAVA画板DEMO

java画板小demo相关推荐

  1. java爬虫黑马百度云,Java爬虫小Demo java爬取百度风云榜数据

    Java爬虫小Demo java爬取百度风云榜数据 很简单的一个小例子,使用到了java的爬虫框架 jsoup ,一起啦看看实现的方法吧! 相关推荐:Python爬虫实战 python爬虫爬取百度风云 ...

  2. Java card 小 demo -- 电子钱包应用

    个人 JavaCard 系列博文涉及到的 demo 代码:Java card 小 demo – 电子钱包应用 两年多以前的代码,不知道找出来的还是不是以前的工程,我上传到我的 github 上了:ht ...

  3. 接收udp数据_聊聊UDP、TCP和实现一个简单的JAVA UDP小Demo

    最近真的比较忙,很久就想写了,可是一直苦于写点什么,今天脑袋灵光一闪,觉得自己再UDP方面还有些不了解的地方,所以要给自己扫盲. 好了,咱们进入今天的主题,先列一下提纲: 1. UDP是什么,UDP适 ...

  4. java线程间通信:一个小Demo完全搞懂

    版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...

  5. spark集群配置以及java操作spark小demo

    spark 安装 配置 使用java来操作spark spark 安装 tar -zxvf spark-2.4.0-bin-hadoop2.7.tgz rm spark-2.4.0-bin-hadoo ...

  6. 初识NIO之Java小Demo

    Java中的IO.NIO.AIO: BIO:在Java1.4之前,我们建立网络连接均使用BIO,属于同步阻塞IO.默认情况下,当有一条请求接入就有一条线程专门接待.所以,在客户端向服务端请求时,会询问 ...

  7. python桌面开发吐血_想用java写个桌面小demo,就布局都差点写吐血了,学艺不精...

    demo简略需求 项目背景 很多文件重复存放,除了管理混乱,还会对患有强迫症用户的身心造成10000点的伤害...其实就是360云盘当时上传了有上传,造成很多重复的图片+视频,前阵子360个人云盘&q ...

  8. java小demo文件分割器

    一个小demo,给朋友传大文件的时候传不过去的话,可以玩玩 //过滤文件目录 import java.io.File; import java.io.FilenameFilter; public cl ...

  9. 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo

    简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...

最新文章

  1. python小项目-python 的一些小项目
  2. java内部错误2755_内部错误2755.(安装软件出问题啦)
  3. php网页加查询框,Twentytwelve头部添加搜索框及网站名称与描述同行显示的简单方法 | 科研动力...
  4. Go-Web框架-Beego架构(二)
  5. 应用于CDN的GSLB系统
  6. 10分钟学会用Python轻松玩转Excel
  7. php myadmin怎么用,关于apachemysqlphpmyadmin的安装与配置
  8. 虚无鸿蒙哪个厉害,【图说鸿蒙】鸿蒙设定之七柱神(五)
  9. python 获取进程池 sleeping_Python 进程操作之进程池--Pool
  10. java中的垃圾收集器_Java中的垃圾收集
  11. 伪数据科学家 VS 真数据科学家
  12. spring mvc实现ajax 分页
  13. 车载以太网网络中的时间同步
  14. 为什么抖音网红城市都在西部?
  15. ACOPTools:一步步帮你快速、高效构建多基因联合系统发育树
  16. 科技论文计算机排版格式,科技论文排版参考格式
  17. wordpress 安装插件
  18. 定时任务:springboot集成Quartz实现多任务多触发的动态管理
  19. 从GIS地图生成生成建筑模型
  20. git checkout tag

热门文章

  1. 索引及其背后的数据结构(顺带介绍了一下子查询和合并查询)
  2. java android 读取微信文件,Android 实现微信,QQ文件在其他应用打开并获取到路径
  3. 小米妙享更新的安装包下载到了电脑的哪里
  4. 教你这么理解 『假脱机打印机系统』
  5. Zabbix - 微信报警
  6. 【Matplotlib】 移动spines
  7. 推荐系统中的常用算法——基于Session的推荐
  8. 【渝粤题库】广东开放大学 网络风险评估 形成性考核
  9. 计算机二级必过知识点大全,计算机二级ms-office办公软件必过知识点.doc
  10. CloudSim介绍与使用 云计算的建模与仿真