zoukankan      html  css  js  c++  java
  • java程序实现鼠标绘图

     1 import java.awt.*;
     2 
     3 import javax.swing.*;
     4 class Gstudy extends JFrame{
     5     
     6     private int x1,y1,x2,y2;
     7     private newPanel panel=new newPanel();
     8     private JButton btn=new JButton("清除");
     9     public Gstudy()
    10     {
    11         setTitle("交互式绘图");
    12         setBounds(10,10,750,700);
    13         btn.addActionListener(new ClearList());
    14         panel.add(btn,BorderLayout.SOUTH);
    15         add(panel);
    16     }
    17     public static void main(String[] args)
    18     {
    19         Gstudy frm=new Gstudy();
    20         frm.setVisible(true);
    21         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    22     }
    23     private class ClearList  implements ActionListener
    24     {
    25         @Override
    26         public void actionPerformed(ActionEvent e) {
    27             
    28             panel.paintComponent(panel.getGraphics());
    29         }
    30     }
    31     private class MyMouseList extends MouseAdapter
    32     {
    33         public void mousePressed(MouseEvent e)
    34         {
    35             x1=e.getX();
    36             y1=e.getY();
    37         }
    38     }
    39     private class MyMouseMotionList extends MouseMotionAdapter
    40     {
    41         public void mouseDragged(MouseEvent e)
    42         {
    43             x2=e.getX();
    44             y2=e.getY();
    45             Graphics g=panel.getGraphics();
    46             g.drawLine(x1, y1, x2, y2);
    47             x1=x2;
    48             y1=y2;
    49         }
    50     }
    51     private class newPanel extends JPanel
    52     {
    53         
    54         public newPanel()
    55         {
    56             this.addMouseMotionListener(new MyMouseMotionList());
    57             this.addMouseListener(new MyMouseList());
    58         }
    59         @Override
    60         protected void paintComponent(Graphics g) {
    61             // TODO Auto-generated method stub
    62             super.paintComponent(g);
    63         }
    64         
    65     }
  • 相关阅读:
    AVWS安装
    Windows 组策略运用
    Windows系统盘清理
    windows计划任务遇到的坑
    mstsc 复制粘贴,遇到的坑~以及输入法无法切换问题
    pyinstaller打包后运行报错-No module named 'pymssql._mssql'
    Word英文如何优雅对齐显示
    键盘除了fn键都失效了,键盘失灵怎么办?
    Ignatius's puzzle
    Train Problem II
  • 原文地址:https://www.cnblogs.com/sytu/p/4531736.html
Copyright © 2011-2022 走看看