绘制画图工具

发布时间:2026/8/3 20:23:29
绘制画图工具 画图工具创建窗体JFrame jfnew JFrame(); jf.setSize(900,900); jf.setTitle(画图工具); jf.setLocationRelativeTo(null);//居中显示 jf.setDefaultCloseOperation(3);//退出进程流式布局的设置(JFrame中包含流式布局FlowLayout flow new FlowLayout(); jf.setLayout(flow);设置窗体背景色jf.getContentPane().setBackground(Color.WHITE);面板对象—JPanel(默认流式布局流式布局的设置FlowLayout flow new FlowLayout(); jf.setLayout(flow);JPanelJPanel northPanel new JPanel(); northPanel.setBackground(Color.GREEN); Dimension dm new Dimension(0,40); northPanel.setPreferredSize(dm); jf.add(northPanel,BorderLayout.NORTH);功能按钮一个一个输入JButton jbunew JButton(直线); northPanel.add(jbu); JButton jbu1new JButton(矩形); northPanel.add(jbu1); JButton jbu2new JButton(三角形); northPanel.add(jbu2); JButton jbu3new JButton(椭圆); northPanel.add(jbu3); JButton jbu4new JButton(多三角形); northPanel.add(jbu4); JButton jbu5new JButton(多边形); northPanel.add(jbu5); JButton jbu6new JButton(曲线); northPanel.add(jbu6); JButton jbu7new JButton(橡皮); northPanel.add(jbu7); JButton blue new JButton(蓝色); northPanel.add(blue); JButton rednew JButton(红色); northPanel.add(red);运用数组解决String[] name {直线,矩形,三角形,椭圆,多三角形,多边形,曲线,橡皮,蓝色,红色};//定义字符串数组保存按钮上的内容 for (int i0;iname.length;i){ JButton jbu new JButton(name[i]); //功能按钮 northPanel.add(jbu); jbu.addActionListener(listener); //给按钮添加动作监听器方法 }画图面板JPanel drawPanel new JPanel(); drawPanel.setBackground(Color.WHITE); jf.add(drawPanel,BorderLayout.CENTER);显示可见jf.setVisible(true);//按钮等需要在前面写定义画笔图形内容显示在哪个组件上画笔就从该组件上获取从窗体上获取画笔对象一定要在窗体显示可见之后Graphics g drawPanel.getGraphics();添加监听器drawPanel.addMouseListener(listener);鼠标拖动监听器drawPanel.addMouseMotionListener(listener);添加动作监听器一个一个输入jbu.addActionListener((ActionListener)listener); jbu1.addActionListener((ActionListener)listener); jbu2.addActionListener((ActionListener)listener); jbu3.addActionListener((ActionListener)listener); jbu4.addActionListener((ActionListener)listener); jbu5.addActionListener((ActionListener)listener); jbu6.addActionListener((ActionListener)listener); jbu7.addActionListener((ActionListener)listener); blue.addActionListener((ActionListener)listener); red.addActionListener((ActionListener)listener);运用数组解决String[] name {直线,矩形,三角形,椭圆,多三角形,多边形,曲线,橡皮,蓝色,红色};//定义字符串数组保存按钮上的内容 for (int i0;iname.length;i){ JButton jbu new JButton(name[i]); //功能按钮 northPanel.add(jbu); jbu.addActionListener(listener); //给按钮添加动作监听器方法 }传递listener.grg;主函数public static void main(String[] args){ lianxi1201 ui new lianxi1201(); ui.initUI(); }继承接口:public class 子类 implements 接口接口{}继承接口一定要重写接口中所有的抽象对象public class DrawListener implements MouseListener, ActionListener, MouseMotionListener {}引用传递public Graphics gr;//保存传递过来的画笔全局变量属性public int x1, y1, x2, y2, x3, y3, x4, y4; public String name;设置标志位用来控制代码执行顺序public int flag 1;画笔的宽度public int w1;画笔当前的颜色public Color colorColor.BLACK;动作监听器的处理方法public void actionPerformed(ActionEvent e) { name e.getActionCommand();//获取内容 System.out.println(点击按钮 name name); 设置颜色 if (蓝色.equals(name)) { color Color.BLUE; } if (红色.equals(name)) { color Color.RED; } gr.setColor(color); System.out.println(点击按钮 name name);鼠标监听器处理方法public void mouseClicked(MouseEvent e) { x3 e.getX();//获取x坐标 y3 e.getY();//获取y坐标 画多三角形 if (多三角形.equals(name)) { gr.drawLine(x1, y1, x3, y3); gr.drawLine(x2, y2, x3, y3); flag 1; System.out.println(x3 x3 y3 y3); } 画多变形 if (多边形.equals(name)) { gr.drawLine(x2, y2, x3, y3); x2 x3; y2 y3; 判断是否双击,如果双击最后一个点与第一个点相连 if (e.getClickCount() 2) { gr.drawLine(x1, y1, x3, y3); flag 1; } } 画曲线 if(曲线.equals(name)){ flag1 } }获取按下时坐标值public void mousePressed(MouseEvent e){ System.out.println(按下); 局部变量1.在当前方法(作用域)中定义的变量 2.方法的参数 if(flag 1) { x1 e.getX(); y1 e.getY(); } System.out.println(x1 x1 y1 y1); }绘制图形public void mouseReleased(MouseEvent e) { System.out.println(松开); if (flag 1) { x2 e.getX(); y2 e.getY(); } System.out.println(x2 x2 y2 y2); //绝对值 int w Math.abs(x2 - x1); int h Math.abs(y2 - y1); //取最小值 int mx Math.min(x1, x2); int my Math.min(y1, y2); //矩形 if (矩形.equals(name)) { gr.drawRect(mx, my, w, h); } //三角形 if (三角形.equals(name)) { int[] xPoints new int[]{(x1 x2) / 2, x1, x2}; int[] yPoints {y1, y2, y2}; int nPoints 3; gr.drawPolygon(xPoints, yPoints, nPoints); } //椭圆 if (椭圆.equals(name)) { gr.drawOval(mx, my, w, h); } //锐三角形 if (多三角形.equals(name) || 多边形.equals(name) flag 1) { gr.drawLine(x1, y1, x2, y2); flag; } //橡皮 if (橡皮.equals(name)) { Graphics2D g2 (Graphics2D) gr; g2.setStroke(new BasicStroke(w)); } }绘制图形public void mouseDragged (MouseEvent e){ //绘制可变直线 if (直线.equals(name)) { gr.setColor(Color.WHITE);//设置没有确定前的直线的颜色 gr.drawLine(x1, y1, x4, y4); x4 e.getX(); y4 e.getY(); gr.setColor(Color.BLACK);//设置确认后的直线 gr.drawLine(x1, y1, x4, y4); } //曲线 if (曲线.equals(name)) { x4e.getX(); y4e.getY(); gr.drawLine(x1,y1,x4,y4); x1x4; y1y4; } //橡皮类似画曲线 if(直线.equals(name)) { Graphics2D g2 (Graphics2D) gr;//设置线段粗细 g2.setStroke(new BasicStroke(8)); x4 e.getX(); y4 e.getY(); gr.setColor(Color.WHITE); gr.drawLine(x1, y1, x4, y4); x1 x4; y1 y4; }public void mouseMoved (MouseEvent e){} public void mouseEntered (MouseEvent e){} public void mouseExited (MouseEvent e){}