关于画图工具的优化
关于画图工具的优化针对于之前的画图的优化有以下几点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在曲线与橡皮擦的重画可以看成无数个线段连成在画是一样在画时与多边形相同不断更新来实现不断画

相关新闻

50元预算实现专业级AI音频降噪方案

50元预算实现专业级AI音频降噪方案

1. 项目概述:低成本AI降噪方案的价值与定位作为一名长期关注音频处理技术的从业者,我深刻理解学生群体在专业学习中对降噪工具的需求与预算限制之间的矛盾。市面上主流AI降噪软件如Adobe Audition、iZotope RX动辄上千元的年费订阅,而专业录音…

2026/8/18 9:27:49 阅读更多 →
中国供应链EMBA横评:复旦科创生态如何赋能东南亚

中国供应链EMBA横评:复旦科创生态如何赋能东南亚

引言 东南亚华裔企业家希望链接中国产业链,泰国高管希望理解中国市场,两类申请人的共同需求是进入稳定的华语商业环境,并获得课程、校友与产业场景的持续支持。中文沟通只是起点,项目能否连接目标行业、长三角供应链、科创资源和亚…

2026/8/18 12:55:12 阅读更多 →
CAD与影像数据高精度套合技术解析

CAD与影像数据高精度套合技术解析

1. 影像与CAD工程文件精确套合的核心挑战在工程测绘与地理信息领域,将遥感影像、航拍图等栅格数据与CAD矢量图纸进行空间对齐是一项基础但极具技术含量的工作。我从业十余年处理过数百个套合项目,发现90%的精度问题都源于坐标系转换、控制点选取和文件格…

2026/8/18 4:27:23 阅读更多 →

最新新闻

2025年主流测试用例集工具深度横评与选型指南

2025年主流测试用例集工具深度横评与选型指南

1. 测试用例集工具:从“记录本”到“效率引擎”的演进 如果你和我一样,在软件测试这行摸爬滚打了几年,一定经历过从Excel、Word文档管理测试用例的“石器时代”。那时候,一个功能迭代,测试用例文档动辄几十页&#xff…

2026/8/19 22:01:26 阅读更多 →
TypeScript工程化AI应用开发:从配置管理到流式响应实战

TypeScript工程化AI应用开发:从配置管理到流式响应实战

1. 从“Hello World”到“Hello AI”:为什么你的第一个AI调用项目需要从工程化开始 如果你和我一样,是个对AI应用开发充满好奇的前端或全栈开发者,当看到ChatGPT API、Claude API或者各种图像生成模型时,第一反应可能就是&#xf…

2026/8/19 22:01:26 阅读更多 →
第131篇 Gazebo进阶——自定义模型、传感器插件和物理参数

第131篇 Gazebo进阶——自定义模型、传感器插件和物理参数

面试翻车现场 面试一家做仓储机器人的公司,面试官问了个很具体的问题:"你在Gazebo里做过自定义模型吗?怎么给机器人加上差速驱动的物理模拟?" 我说用URDF描述模型,加gazebo_ros_diff_drive插件。他摇摇头说…

2026/8/19 22:01:26 阅读更多 →
第132篇 ROS2组件化编程——从Node到Component的性能提升

第132篇 ROS2组件化编程——从Node到Component的性能提升

面试翻车现场 面试一家自动驾驶公司,面试官问:"你了解ROS2的Component吗?为什么需要组件化?" 我说Component就是把Node打包成共享库,可以动态加载。他追问:"组件化解决了什么具体问题&…

2026/8/19 22:01:26 阅读更多 →
CAD绘图效率提升:从练习图29拆解系统绘图流程与高效命令组合

CAD绘图效率提升:从练习图29拆解系统绘图流程与高效命令组合

如果你是一名机械工程师、建筑设计师,或者正在学习CAD制图,那么你一定遇到过这样的困境: 明明软件操作都会,但面对一个复杂的零件图或建筑平面图,却不知从何下手,画得又慢又容易出错。 这背后真正的问题…

2026/8/19 22:01:26 阅读更多 →
具身智能三大关键技术详解:TVA 、World 、VLA(八)

具身智能三大关键技术详解:TVA 、World 、VLA(八)

前沿技术探索:AI智能体视觉(TVA,Transformer-based Vision Agent)是依托Transformer架构与“因式智能体”理论所构建的颠覆性工业视觉技术,是集深度强化学习(DRL)、卷积神经网络(CNN…

2026/8/19 22:00:25 阅读更多 →

日新闻

【单片机课程设计/毕业设计】基于 STM32 与 WiFi 模块的室内通风智能管控系统设计 基于 STM32 的人体存在感知自适应风扇控制系统设计(018503)

【单片机课程设计/毕业设计】基于 STM32 与 WiFi 模块的室内通风智能管控系统设计 基于 STM32 的人体存在感知自适应风扇控制系统设计(018503)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于嵌入式单片机,Java、小程序技术领域和毕业项目实战 ✌️…

2026/8/19 0:00:30 阅读更多 →
AI如何驱动数学猜想生成:从大语言模型到自动化数学发现

AI如何驱动数学猜想生成:从大语言模型到自动化数学发现

1. 项目概述:当AI开始“猜”数学定理 最近在AI研究圈里,一个名为“Moonshine”的项目引起了不小的讨论。这名字本身就挺有意思,直译是“月光”,但在数学史上,它特指一个神秘而美丽的联系——魔群月光猜想,连…

2026/8/19 0:00:30 阅读更多 →
WarcraftHelper 魔兽争霸3优化实战指南

WarcraftHelper 魔兽争霸3优化实战指南

WarcraftHelper 魔兽争霸3优化实战指南 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 一台刚配的新电脑,跑《魔兽争霸3》却卡成 PPT——这…

2026/8/19 0:02:31 阅读更多 →

周新闻

基于阿里云与通义千问(Qwen)构建AI应用:从模型调用到生产部署的完整实践指南

基于阿里云与通义千问(Qwen)构建AI应用:从模型调用到生产部署的完整实践指南

如果你是一名开发者,最近可能已经感受到了AI大模型正在从“玩具”变成“生产力工具”的强烈信号。从代码补全到智能Agent,从本地部署到云端API,我们正处在一个技术栈快速重构的节点。然而,面对层出不穷的模型、框架和工具&#xf…

2026/8/19 11:55:18 阅读更多 →
工业通信系统底层逻辑:04 反射——高频能量撞墙之后会发生什么?

工业通信系统底层逻辑:04 反射——高频能量撞墙之后会发生什么?

第四篇:反射——高频能量撞墙之后会发生什么? —— 你以为信号已经过去了,其实它正在回来打你 老Q的现场笔记 第五季,我们正式进入工业神经系统层。这里不再是单个设备的战斗,而是整个工厂“经脉”层面的秩序之战。从这一篇开始,你将第一次看清:看似简单的信号传播,背…

2026/8/19 9:46:27 阅读更多 →
【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

2026/8/19 11:55:16 阅读更多 →

月新闻

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南 【免费下载链接】BaiduNetdiskPlugin-macOS For macOS.百度网盘 破解SVIP、下载速度限制~ 项目地址: https://gitcode.com/gh_mirrors/ba/BaiduNetdiskPlugin-macOS 还在为百度网盘macOS版的龟速下…

2026/8/19 5:04:55 阅读更多 →
终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 还在为网易云音乐下载的NCM格式文件无法在其他播放器播放而烦恼吗?ncmdump解密工具帮你轻松解决这个困…

2026/8/19 7:42:22 阅读更多 →
HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

AgentCard 智能体卡片:为英语学习 App 打造桌面级学习助手适用平台:HarmonyOS 7.0 (API 26 Beta)一、引言 HarmonyOS 7.0(API 26 Beta)新增了 AgentCard 智能体卡片能力,这是继 HMAF(鸿蒙智能体框架&#x…

2026/8/19 11:55:13 阅读更多 →