画板UI的实现

一 .首先创建窗体
this.setTitle("huaban3");
this.setSize(500, 600);
// 居中
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setVisible(true);

1.创建左边面板、下面的面板、中间面板

// 创建面板
        JPanel paneldong = new JPanel();
        JPanel paneldown = new JPanel();
        JPanel panelzhong = new JPanel();

二.设置左边的面板为流式布局,创建一个按钮组对象,用来管理所有单选按钮,导入图片

paneldong.setLayout(new FlowLayout(0, 0, 0));
ButtonGroup bg = new ButtonGroup();
for (int i = 0; i < 16; i++) {
    JRadioButton jr = new JRadioButton();
    // 给图像加按钮给按钮命名
    jr.setActionCommand(i + "");
    ImageIcon m1 = new ImageIcon("images/draw" + i + ".jpg");
    ImageIcon m2 = new ImageIcon("images/draw" + i + "-1.jpg");
    ImageIcon m3 = new ImageIcon("images/draw" + i + "-2.jpg");
    ImageIcon m4 = new ImageIcon("images/draw" + i + "-3.jpg");
    // 给按钮添加图片
    jr.setIcon(m1);// 设置的是默认图片
    jr.setRolloverIcon(m2);// 设置的鼠标移动到上面的图片
    jr.setPressedIcon(m3);// 设置的是鼠标按下这个 按钮后的图片
    jr.setSelectedIcon(m4);// 设置这个按钮被选中后的图片
    // 给东面板添加按钮
    paneldong.add(jr);
    // 设置边框为没有
    jr.setBorder(null);
    // 把按钮添加到按钮组
    bg.add(jr);
    }

1.设置左边面板的背景色 尺寸
2.获取画笔,画笔在要画的区域获取
3.创建鼠标监听类并继承MouseListener 导入方法

三.下边有颜色按钮的面板
1.首先创建2个小面板(下左 下右)
2.设置下面板为流式布局
3.定义一个颜色数组
Color[] colors = { Color.blue, Color.cyan, Color.GRAY, Color.orange,
Color.RED, Color.blue, Color.cyan, Color.GRAY, Color.orange,
Color.RED, Color.blue, Color.cyan, Color.GRAY, Color.orange,
Color.RED, Color.blue, Color.cyan, Color.GRAY, Color.orange,
Color.RED, Color.blue, Color.cyan, Color.GRAY, Color.orange,
Color.RED, };

4.往下右面板添加按钮,设置按钮颜色
5.创建颜色按钮监听器,给按钮添加监听器
6.设置下左面板为绝对布局,添加两个按钮到下左面板中
7.设置按钮的位置 边框效果

button1.setBorder(new BevelBorder(0, Color.white, Color.gray));

四.颜色监听类
1.通过重载构造方法传参
public ColorListener(huaban3 hb1) {
hb = hb1;
}

2.先要通过获取事件源的方式获取源头
Object obj = arg0.getSource();
3.把父类类型强转型为子类类型

JButton button = (JButton) obj;

4.获取按钮背景色

Color color = button.getBackground();

5.把这个颜色设置到画笔上,画笔就有颜色了
hb.g.setColor(color);
五.功能实现
1.在鼠标点击方法中得到第一次点击获取的点
2.获取被选中的按钮,通过按钮来进行选择
3.在鼠标释放方法中得到释放的坐标
4.通过得到的命令来实现不同的功能(画笔 矩形 多边形 取色器)。。。。。。

    public void mouseDragged(MouseEvent arg0) {
        // 获取当前拖动后的坐标
        x2 = arg0.getX();
        y2 = arg0.getY();
        if ("6".equals(command)) {
            // 画笔
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;

        } else if ("7".equals(command)) {
            // 设置粗细
            g.setStroke(new BasicStroke(20));
            // 画线
            g.drawLine(x1, y1, x2, y2);
            // 保存当前这一次的下x2,y2坐标
            x1 = x2;
            y1 = y2;
            // 还原粗细
            g.setStroke(new BasicStroke(1));
        } else if ("2".equals(command)) {
            // 设置橡皮擦初始颜色为画板背景色
            g.setColor(Color.white);
            // 设置粗细
            g.setStroke(new BasicStroke(20));
            // 画线
            g.drawLine(x1, y1, x2, y2);
            // 保存当前这一次的下x2,y2坐标
            x1 = x2;
            y1 = y2;
            // 还原粗细
            g.setStroke(new BasicStroke(1));
            // 设置橡皮擦的恢复颜色为刷子颜色
            g.setColor(Color.black);
        } else if ("8".equals(command)) {
            // Random 随意,偶然的行动
            Random ran = new Random();
            // 实现喷桶功能
            for (int i = 0; i < 80; i++) {
                // 随机坐标的偏移量
                int pylx = ran.nextInt(40) - 20;
                int pyly = ran.nextInt(40) - 20;
                g.drawLine(x2 + pylx, y2 + pyly, x2 + pylx, y2 + pyly);
            }
        }
    }   public void mouseReleased(MouseEvent arg0) {
        // 释放
        x2 = arg0.getX();
        y2 = arg0.getY();
        if ("10".equals(command)) {
            // 画笔
            g.drawLine(x1, y1, x2, y2);
        } else if ("12".equals(command)) {
            // 矩形
            g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
                    Math.abs(y2 - y1));
        } else if ("13".equals(command)) {
            // 绘制多边形

            if (flag) {
                // 第一次释放
                g.drawLine(x1, y1, x2, y2);
                // 修改flag的值为false
                flag = false;

                x4 = x1;
                y4 = y1;
            } else {
                // 不是第一次释放
                g.drawLine(x3, y3, x2, y2);

            }
            x3 = x2;
            y3 = y2;
        } else if ("4".equals(command)) {
            try {
                // 重新获取x y 坐标 相对于屏幕
                x2 = arg0.getXOnScreen();
                y2 = arg0.getYOnScreen();
                // 1.创建一个机器人对象
                Robot robot = new Robot();
                // 2.构造一个矩形区域,这个区域,就是你要获取颜色的区域
                Rectangle rect = new Rectangle(x2, y2, 1, 1);
                // BufferedImage图像缓冲区create 产生Screen屏幕Capture捕获
                BufferedImage image = robot.createScreenCapture(rect);
                // 3.获取图片的背景像素点颜色:三原色RGB
                int v = image.getRGB(0, 0);
                Color color = new Color(v);
                // 4.把颜色设置到画笔上
                g.setColor(color);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }

        }
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值