恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

关于画图工具的优化

  • 首页
  • 资讯中心
  • /
  • 关于画图工具的优化

相关资讯

半空间数据空间化:GIS接口设计与工程实践全解析 2026/8/16 2:28:41
OpenClaw新手必备:10个高效技能包快速搭建AI助手 2026/8/16 2:23:40
Qt开发中私有头文件缺失错误的深度解析与解决方案 2026/8/16 2:23:40

最新资讯

DataGrip数据库开发实战:从SQL编辑器到效率倍增器的进阶指南
VSCode开发Java与SpringBoot:从环境配置到疑难排错实战指南
Claude Code:轻量化代码协作工具的核心功能与实践
Mac效率神器:鼠须管输入法深度配置与皮肤定制指南
从Word转PDF到40+格式通吃:WorkBuddy技能封装架构实战
AI模型部署实战:从推理优化到生产级运维的工程指南

今日推荐

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码
【双层规划,节点出清价,绿证交易,CVaR方法】两级电力市场环境下计及风险的省间交易商最优购电模型附Matlab代码
隐式mpc+自适应mpc+时变mpc,线性时变模型预测控制附Simulink仿真

本周热门

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码
【双层规划,节点出清价,绿证交易,CVaR方法】两级电力市场环境下计及风险的省间交易商最优购电模型附Matlab代码
隐式mpc+自适应mpc+时变mpc,线性时变模型预测控制附Simulink仿真

本月精选

如何用DamaiHelper实现演唱会门票的智能自动化抢购:完整技术解决方案指南
第4篇:59 倍性能差距的索引瓶颈定位——一次教科书级的全表扫描调优
终极歌词批量下载神器:5分钟解决离线音乐库歌词同步难题

关于画图工具的优化

发布时间:2026/8/16 2:28:41
关于画图工具的优化 关于画图工具的优化针对于之前的画图的优化有以下几点1曲线的增加2 对于橡皮擦的优化3 关于粗细的调整4对于移动窗体画图的消失1曲线的增加主要用到了新的监听器MouseMotionListener他有俩个方法分别为mouseDragged和mouseMove主要用到了mouseDragged具体如下1导入接口2实现方法3具体代码在x4e.getX();y4e.getY();if (“曲线”.equals(name)){gr.drawLine(x1,y1,x4,y4);x1x4; y1y4;2对于橡皮擦的优化与曲线相同注意有转画笔 后会讲g2 (Graphics2D) gr;g2.setStroke(new BasicStroke(width));gr.setColor(color);上为转画笔g2.setStroke(new BasicStroke(8));gr.setColor(Color.white); gr.drawLine(x1,y1,x4,y4); x1x4; y1y4;3 关于粗细的调整在转画笔后int width1;g2 (Graphics2D) gr;g2.setStroke(new BasicStroke(width));//上面的width后填数来控制粗细4 对于移动窗体画图的消失这是本篇的大头首先再建俩个类一个继承Jpanel一个创建对象思路为通过继承来重画进而解决移动窗体画图的消失具体代码如下1主体建立窗体,传递数据public class drawtooi { public void initUI() { JFrame jf new JFrame(); jf.setSize(1000, 1000); jf.setTitle(drawtool); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel northJpanelnew JPanel(); northJpanel.setBackground(Color.blue); Dimension dinew Dimension(); di.setSize(0,50); jf.add(northJpanel,BorderLayout.NORTH); draw1 listenernew draw1(); String []name{直线,矩形,三角形,多边形,圆,椭圆,等腰三角形,红色,蓝色,粗,细,曲线,橡皮擦}; for (String s : name) { JButton jbu new JButton(s); northJpanel.add(jbu); jbu.addActionListener(listener); } MPanel drawPanelnew MPanel();// 这为与之前的不同 drawPanel.setBackground(Color.white); jf.add(drawPanel,BorderLayout.CENTER); jf.setVisible(true); Graphics g drawPanel.getGraphics(); drawPanel.addMouseListener(listener); drawPanel.addMouseMotionListener(listener); listener.gr g; drawPanel.shapeArr listener.shapeArr;// 其次是这传递数据 } public static void main(String[] args) { drawtooi ui new drawtooi(); ui.initUI(); }}2画图工具先别管shape他就是传数据注曲线和橡皮擦都有public class draw1 implements MouseListener, ActionListener, MouseMotionListener { public Graphics gr; public int x1, y1, x2, y2, x3, y3,x4,y4; private String name; private int clickCount 0; private boolean triangleReady false; public Color colorColor.black; public int width1; public Graphics2D g2; public shapes[] shapeArr new shapes[10000]; //操作数组的下标 public int index 0; Override public void actionPerformed(ActionEvent e) { String str e.getActionCommand(); if (红色.equals(str)) { color Color.RED; } else if (蓝色.equals(str)) { color Color.BLUE; } else if (粗.equals(str)) { width 5; }else if (细.equals(str)){ width1; } else{ //表示点击图形按钮 //获取按钮上的内容 name e.getActionCommand(); } g2 (Graphics2D) gr; g2.setStroke(new BasicStroke(width)); gr.setColor(color); } Override public void mouseClicked(MouseEvent e) { if (三角形.equals(name)) { if (triangleReady) { clickCount 0; triangleReady false; return; } clickCount; if (clickCount 1) { x1 e.getX(); y1 e.getY(); } if (clickCount 2) { x2 e.getX(); y2 e.getY(); gr.drawLine(x1, y1, x2, y2); // 第一条边 System.out.println(x1 x1 y1 y1); System.out.println(x2 x2 y2 y2); } if (clickCount 3) { x3 e.getX(); y3 e.getY(); triangleReady true; clickCount 0; // ---- 补全缺失的两条边 ---- gr.drawLine(x1, y1, x3, y3); gr.drawLine(x2, y2, x3, y3); System.out.println(x1 x1 y1 y1); System.out.println(x2 x2 y2 y2); System.out.println(x3 x3 y3 y3); clickCount 0; shapes shape new shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x2; shape.y2 y2; shape.x3x3; shape.y3y3; shapeArr[index] shape; index; } } if (多边形.equals(name)){ clickCount; if(clickCount1){ x1e.getX(); y1e.getY(); } if (clickCount2){ x2e.getX(); y2e.getY(); gr.drawLine(x1,y1,x2,y2); shapes shapenew shapes(); shape.namename; shape.colorcolor; shape.Widthwidth; shape.x1x1;shape.y1y1; shape.x2x2;shape.y2y2; shapeArr[index] shape; index; } if (clickCount3){ x3e.getX(); y3e.getY(); gr.drawLine(x2,y2,x3,y3); shapes shapenew shapes(); shape.namename; shape.colorcolor; shape.Widthwidth; shape.x1x2;shape.y1y2; shape.x2x3;shape.y2y3; shapeArr[index] shape; index; x2x3; y2y3; if (e.getClickCount()2){ shapes closeshapenew shapes(); closeshape.namename; closeshape.colorcolor; closeshape.Widthwidth; closeshape.x1x1;closeshape.y1y1; closeshape.x2x3;closeshape.y2y3; shapeArr[index]closeshape; index; gr.drawLine(x1,y1,x3,y3); clickCount0; } } } } Override public void mousePressed(MouseEvent e) { if (三角形.equals(name)) { return; } if (多边形.equals(name)) { return; } x1 e.getX(); y1 e.getY(); } Override public void mouseReleased(MouseEvent e) { if (多边形.equals(name)) { return; } if (三角形.equals(name)) { return; } x2 e.getX(); y2 e.getY(); //绝对值 int w Math.abs(x2 - x1); int q Math.abs(y2 - y1); //取最小值 int mx Math.min(x1, x2); int my Math.min(y1, y2); int top (x1 x2) / 2; if (直线.equals(name)) { gr.drawLine(x1, y1, x2, y2); shapes shape new shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x2; shape.y2 y2; shapeArr[index] shape; index; } if (矩形.equals(name)) { gr.drawRect(mx, my, w, q); shapes shape new shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x2; shape.y2 y2; shapeArr[index] shape; index; } if (等腰三角形.equals(name)) { gr.drawLine(top, y1, x2, y2); gr.drawLine(x1, y2, x2, y2); gr.drawLine(top, y1, x1, y2); shapes shapenew shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x2; shape.y2 y2; shapeArr[index] shape; index; } if (圆.equals(name)){ gr.drawOval(x1,y1,w,w); shapes shapenew shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x2; shape.y2 y2; shapeArr[index] shape; index; } if (椭圆.equals(name)){ gr.drawOval(x1,y1,w,q); shapes shapenew shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x2; shape.y2 y2; shapeArr[index] shape; index; } } Override public void mouseEntered(MouseEvent e) { } Override public void mouseExited(MouseEvent e) { } Override public void mouseDragged(MouseEvent e) { x4e.getX(); y4e.getY(); if (曲线.equals(name)){ gr.drawLine(x1,y1,x4,y4); shapes shapenew shapes(); shape.name name; shape.colorcolor; shape.Widthwidth; shape.x1 x1; shape.y1 y1; shape.x2 x4; shape.y2 y4; shapeArr[index] shape; index; x1x4; y1y4; } if (橡皮擦.equals(name)){ g2.setStroke(new BasicStroke(8)); gr.setColor(Color.white); gr.drawLine(x1,y1,x4,y4); shapes shapenew shapes(); shape.name name; shape.colorcolor; shape.Width8; shape.x1 x1; shape.y1 y1; shape.x2 x4; shape.y2 y4; shapeArr[index] shape; index; x1x4; y1y4; } } Override public void mouseMoved(MouseEvent e) {}}3继承重中之重在之前提过superpublic class MPanel extends JPanel {public shapes[] shapeArr; //保存图形的数组//重写paint方法 public void paint(Graphics g) { //保留绘制组件的功能 super.paint(g); System.out.println(paint...); //取出所有的图形对象实现重绘 for (shapes shapes : shapeArr) { if (shapes null) continue; shapes.drawshape(g); } }}4重画public class shapes {public int x1,y1,x2,y2,x3,y3; public int Width; public Color colorColor.black; public String name; public void drawshape(Graphics g){ if (gnull)return; Graphics2D g2d(Graphics2D) g; g2d.setColor(this.color); g2d.setStroke(new BasicStroke(Width)); if (name !null) { switch (name) { case 直线: g2d.drawLine(x1, y1, x2, y2); break; case 矩形: int x Math.min(x1, x2); int y Math.min(y1, y2); int w Math.abs(x1 - x2); int h Math.abs(y2 - y1); g2d.drawRect(x, y, w, h); break; case 等腰三角形: int top (x1 x2) / 2; g2d.drawLine(top, y1, x2, y2); g2d.drawLine(x1, y2, x2, y2); g2d.drawLine(top, y1, x1, y2); break; case 圆: int m Math.abs(x1 - x2); g2d.drawOval(x1, y1, m, m); case 椭圆: int width Math.abs(x1 - x2); int height Math.abs(y1 - y2); g2d.drawOval(x1, y2, width, height); case 三角形: g2d.drawLine(x1, y1, x2, y2); g2d.drawLine(x1, y1, x3, y3); g2d.drawLine(x3, y3, x2, y2); break; case 多边形: g2d.drawLine(x1,y1,x2,y2); break; case 曲线: g2d.drawLine(x1,y1,x2,y2); break; case 橡皮擦: g2d.setColor(Color.white); g2d.drawLine(x1,y1,x2,y2); } } }}5讲解1shape为一个数组来计录画图的点与按键的名称画图的粗细与颜色来实现重画2继承了Jpanel的方法用了super加了一个方法通过数组来计录方便重画34中的case与if相同及中的相同就运行后的再运行后要加break停止运行4在多边形的重画中建了一个新数组来计录一开始的点避免找不到一开始的点就可以闭合5在曲线与橡皮擦的重画可以看成无数个线段连成在画是一样在画时与多边形相同不断更新来实现不断画

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号