zoukankan      html  css  js  c++  java
  • 绘图小程序

    点击鼠标左键时,可以进行绘图操作,当点击鼠标右键时,清空屏幕。

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class MouseEventDemo extends JFrame{
        private JPanel p;
        //鼠标上一次的坐标
        int pre_x = -1,pre_y = -1;
        //鼠标当前坐标
        int x, y;
        public MouseEventDemo(){
            super("画板");
            p=new JPanel();
            //注册鼠标监听
            p.addMouseMotionListener(new PaintListener());
            p.addMouseListener(new ResetListener());
            //将面板添加到窗体中
            this.add(p);
            //设定窗口大小
            this.setSize(400,300);
            //设定窗口左上角坐标
            this.setLocation(200, 100);
            //设定窗口默认关闭方式为退出应用程序
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //设置窗口可见
            this.setVisible(true);
        }
        //重写JFrame的paint()方法,此方法用于在窗体中画图
        public void paint(Graphics g){
            //设置画笔颜色
            g.setColor(Color.red);
            //历史坐标>0
            if(pre_x>0&&pre_y>0){
                //绘制一条线段,从上一次鼠标拖动事件点到本次鼠标拖动事件点
                g.drawLine( pre_x, pre_y, x, y);
            }
            //保存当前鼠标坐标,称为上一次的历史坐标
            pre_x=x;
            pre_y=y;
        }
        //定义鼠标拖动监听类
        class PaintListener implements MouseMotionListener{
            //鼠标移动的处理方法
            public void mouseMoved(MouseEvent e){
            }
            //鼠标拖动的处理方法,负责画画工作
            public void mouseDragged(MouseEvent e){
                //获得鼠标当前坐标
                x=e.getX();
                y=e.getY();
                //重画,reprint()触发paint()
                MouseEventDemo.this.repaint();
            }
        } 
        //定义鼠标监听类
        class ResetListener implements MouseListener{
            //鼠标单击事件处理
            public void mouseClicked(MouseEvent e){
            }
            //鼠标按下事件处理
            public void mousePressed(MouseEvent e){
                //获取鼠标按键,判断是否为右键
                if(e.getButton()==MouseEvent.BUTTON3){
                    //重画面板(擦除原来的轨迹)
                    MouseEventDemo.this.p.repaint();
                }
            }
            //鼠标松开事件处理,重置历史坐标
            public void mouseReleased(MouseEvent e){
                //鼠标松开时,将历史坐标设置为-1(重置)
                pre_x=-1;
                pre_y=-1;
            }
            //鼠标进入事件处理
            public void mouseEntered(MouseEvent e){
            }
            //鼠标退出事件处理
            public void mouseExited(MouseEvent e){
            }
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            new MouseEventDemo();
    
        }
    
    }
  • 相关阅读:
    合并一个二维数组中相同项,其他数量则相加
    清除空值
    数组的一维下标换为指定的key值
    二维数组排序
    word输出
    解决Zend加密的PHP页面出现Fatal error: Incompatible file format: The encoded file has format major ID 1, whereas the Loader expects 4 in
    nginx反向代理substitutions4nginx模块实现替换字符盗站 nginx.conf配置
    ubuntu 编译安装nginx 并添加substitutions4nginx模块
    windows 2008 R2 wincache 不稳定
    php5.5及php5.6 wincache无法启用问题
  • 原文地址:https://www.cnblogs.com/chenttc/p/7719558.html
Copyright © 2011-2022 走看看