当前位置: 首页 > news >正文

关于画图工具的优化

关于画图工具的优化

针对于之前的画图的优化有以下几点

1曲线的增加
2 对于橡皮擦的优化
3 关于粗细的调整
4对于移动窗体画图的消失

1曲线的增加

主要用到了新的监听器MouseMotionListener
他有俩个方法分别为mouseDragged和mouseMove
主要用到了mouseDragged具体如下
1:导入接口
2:实现方法
3:具体代码(在

x4=e.getX();
y4=e.getY();
if (“曲线”.equals(name)){
gr.drawLine(x1,y1,x4,y4);

x1=x4; y1=y4;

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); x1=x4; y1=y4;

3 关于粗细的调整

在转画笔后
int width=1;
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 northJpanel=new JPanel(); northJpanel.setBackground(Color.blue); Dimension di=new Dimension(); di.setSize(0,50); jf.add(northJpanel,BorderLayout.NORTH); draw1 listener=new draw1(); String []name={"直线","矩形","三角形","多边形","圆","椭圆","等腰三角形","红色","蓝色","粗","细","曲线","橡皮擦"}; for (String s : name) { JButton jbu = new JButton(s); northJpanel.add(jbu); jbu.addActionListener(listener); } MPanel drawPanel=new 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 color=Color.black; public int width=1; 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)){ width=1; } 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.color=color; shape.Width=width; shape.x1 = x1; shape.y1 = y1; shape.x2 = x2; shape.y2 = y2; shape.x3=x3; shape.y3=y3; shapeArr[index] = shape; index++; } } if ("多边形".equals(name)){ 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); shapes shape=new shapes(); shape.name=name; shape.color=color; shape.Width=width; shape.x1=x1;shape.y1=y1; shape.x2=x2;shape.y2=y2; shapeArr[index] = shape; index++; } if (clickCount>=3){ x3=e.getX(); y3=e.getY(); gr.drawLine(x2,y2,x3,y3); shapes shape=new shapes(); shape.name=name; shape.color=color; shape.Width=width; shape.x1=x2;shape.y1=y2; shape.x2=x3;shape.y2=y3; shapeArr[index] = shape; index++; x2=x3; y2=y3; if (e.getClickCount()==2){ shapes closeshape=new shapes(); closeshape.name=name; closeshape.color=color; closeshape.Width=width; closeshape.x1=x1;closeshape.y1=y1; closeshape.x2=x3;closeshape.y2=y3; shapeArr[index]=closeshape; index++; gr.drawLine(x1,y1,x3,y3); clickCount=0; } } } } @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.color=color; shape.Width=width; 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.color=color; shape.Width=width; 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 shape=new shapes(); shape.name = name; shape.color=color; shape.Width=width; 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 shape=new shapes(); shape.name = name; shape.color=color; shape.Width=width; 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 shape=new shapes(); shape.name = name; shape.color=color; shape.Width=width; 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) { x4=e.getX(); y4=e.getY(); if ("曲线".equals(name)){ gr.drawLine(x1,y1,x4,y4); shapes shape=new shapes(); shape.name = name; shape.color=color; shape.Width=width; shape.x1 = x1; shape.y1 = y1; shape.x2 = x4; shape.y2 = y4; shapeArr[index] = shape; index++; x1=x4; y1=y4; } if ("橡皮擦".equals(name)){ g2.setStroke(new BasicStroke(8)); gr.setColor(Color.white); gr.drawLine(x1,y1,x4,y4); shapes shape=new shapes(); shape.name = name; shape.color=color; shape.Width=8; shape.x1 = x1; shape.y1 = y1; shape.x2 = x4; shape.y2 = y4; shapeArr[index] = shape; index++; x1=x4; y1=y4; } } @Override public void mouseMoved(MouseEvent e) {

}
}

3继承(重中之重)在之前提过super

public 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 color=Color.black; public String name; public void drawshape(Graphics g){ if (g==null)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讲解

1:shape为一个数组来计录画图的点与按键的名称画图的粗细与颜色
来实现重画
2:继承了Jpanel的方法(用了super)加了一个方法通过数组来计录方便重画
3:4中的case与if相同及""中的相同就运行:后的,再运行后要加break停止运行
4:在多边形的重画中建了一个新数组来计录一开始的点避免找不到一开始的点,就可以闭合
5:在曲线与橡皮擦的重画可以看成无数个线段连成在画是一样(在画时与多边形相同不断更新来实现不断画)

http://www.jsqmd.com/news/1401897/

相关文章:

  • Lua环境搭建全攻略:从零配置独立开发环境到包管理
  • 双列瀑布流之美:ArkUI 触底加载动画让鸿蒙商品页刷到手软
  • 知网AIGC检测前,先用哪个免费检测自查
  • 2026 年至今,广东专业的化工设备拆除回收服务商找哪家,你家闲置的旧厂“大块头”,藏着不为人知的变现窍门? - 行业推荐官【认证】
  • 美团“红灯停表”悄悄按下了骑手计时器的暂停键
  • 2026年试试这几家车间精益安灯系统整体解决方案提供商,让管理效率翻倍 - 品牌排行榜
  • 小米8E5解锁工具更新啦,又叫8 Elite Gen5解锁脚本
  • 数据的身家性命:ArkTS 为鸿蒙备份导出设计目标库结构
  • 桌面快捷方式图标变白最保险解决方式
  • Eclipse环境下的PVZ模组开发:实现昼夜交替与日食机制
  • 温度与湿度双重作用下的肌肤护理策略
  • 懒加载的秘密:ArkTS 实现鸿蒙商品分页与总条数统计
  • OpenClaw新手必备:10个高效技能包快速搭建AI助手
  • 论文尾巴降AI按字怎么弄,助研君最省心
  • Prism框架区域与导航机制:构建模块化WPF/Xamarin.Forms应用的核心
  • Windows安全中心页面不可用?从服务检查到注册表修复的完整解决方案
  • 2026 年新消息:浦北口碑好的公路防撞护栏生产厂家哪家靠谱,被忽略的公路保命装置,居然还有这么多不为人知的细节?-煜翎丝网 - 行业推荐官[官方】--
  • 工业软件授权分发技术解析与实践
  • 黑客攻防实战:从渗透测试到APT攻击防御
  • Mac 上跑通 minazapper 狗叫分类器:从“假 100%“到真实可用的踩坑指南
  • 构建多智能体协作系统:从协议设计到工程实践
  • 【C++】CSP-J复赛模拟赛2
  • Keil MDK 安装配置全攻略:从零搭建 ARM Cortex-M 开发环境
  • Photoshop抠图实战:快速选择、钢笔与通道三大核心技法详解
  • 广东东莞玻璃收纳盒铜槽生产厂家实力测评,本地采购避坑指南 - myqiye
  • 2026 年至今,磐安靠谱的液化气罐水切割加工厂怎么联系,原来能用来切割的东西,不只见过的那几样 - 企业信息推荐-2
  • GS8552-SR精密运放芯片选型、应用与实测指南
  • 深度 | DeepSeek V4 跑上国产芯片:推理闭环成立,训练侧还在用英伟达?
  • x32dbg/x64dbg逆向之反汇编while分析
  • 2026年8月山东省日照市联通单宽带怎么选_新手避坑指南 - 找卡家园