画板的实现

这里写图片描述

如上画板的效果图:
第一先做好界面。
1。布局:左边面板,中间面板,底部面。setLayout(new BorderLayout()),便可布局,分位,东,南,西,北

这里写图片描述

2.左边布局流式布局。setLayout(new FlowLayout());
代码如下:

这里写代码片
public class PaneleLeft extends Jpanel{
    String commd = null;
    String[] arr = { "airbrush", "brush", "color_picker", "curve",
                    "dot_rect", "eraser", "fill", "line", "magnifier", "oval",
                    "pencil", "polygon", "rect", "round_rect", "star", "word"};
    public JpanelLeft(){
    //设置左边面板的大小
    Dimension de = new Dimension(60,400);
    //按钮的大小
    Dimension de1 = new Dimension(25,25);
    setPreferredSize(de);
    //加监听用的是匿名内部类不是给面板在监听,是给里面的组件按钮加监听
    ActionListener actionlisten = new ActionListener(){
    //重写父类方法
    public void actionPerformed(ActionEvent e){
        //获取监听命令,
        commd = e.getActionCommand();
        }
    }
}
for(int i=0;i<arry.length;i++){
    JButton button = new JButton();
    button.setIcon(new ImageIcon("images/"+arr[i]+".jpg"));
    //给按钮设置命令
    button.setActionCommand(arr[i]);
    add(button);
    button.addAcitonListener(actionlisten);
    }
}

画板的中间面板的设置,及部分功能的实现如下图:
这里写图片描述

package cn.huaxin.draw.layer;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JPanel;

public class JpanelCenter extends JPanel {
    public Graphics2D graphics;
    public JpanelLeft jpanelLeft;
    public int count;
    int startx, starty, polygonMiddlex, polygonmiddley, polyStartx, polyStarty,
            endx, endy;

    public JpanelCenter(Graphics2D g, JpanelLeft jpanelLeft1) {
        graphics = g;
        jpanelLeft = jpanelLeft1;
        this.setBackground(Color.white);
        MouseMotionListener mouseMotionlistener = new MouseMotionListener() {
            // 鼠标拖动事件
            public void mouseDragged(MouseEvent e) {
                // System.out.println("鼠标拖动");

                int x = e.getX();
                int y = e.getY();
                // 铅笔
                if (jpanelLeft.commd.equals("pencil")) {

                    graphics.drawLine(startx, starty, x, y);
                    startx = x;
                    starty = y;
                } else if (jpanelLeft.commd.equals("airbrush")) {// 喷桶
                    for (int i = 0; i <= 30; i++) {

                        int ran = new Random().nextInt(8) - 4;
                        int ran1 = new Random().nextInt(8) - 4;
                        graphics.drawLine(x + ran, y + ran1, x + ran, y + ran1);
                        startx = x;
                        starty = y;
                    }

                } else if (jpanelLeft.commd.equals("brush")) {// 刷子

                    graphics.drawLine(startx, starty, x, y);
                    startx = x;
                    starty = y;

                } else if (jpanelLeft.commd.equals("eraser")) {

                    graphics.drawLine(startx, starty, x, y);
                    startx = x;
                    starty = y;

                }
            }

            public void mouseMoved(MouseEvent e) {

            }

        };
        MouseListener mouse = new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                // System.out.println("点击了");
                int mousecount = e.getClickCount();
                if ("polygon".equals(jpanelLeft.commd)) {
                    if (mousecount == 1) {

                        graphics.drawLine(polygonMiddlex, polygonmiddley,
                                e.getX(), e.getY());
                        polygonMiddlex = e.getX();
                        polygonmiddley = e.getY();
                    } else if (mousecount == 2) {
                        graphics.drawLine(polyStartx, polyStarty,
                                polygonMiddlex, polygonmiddley);
                        count = 0;
                    }
                }
            }

            // 鼠标按下
            public void mousePressed(MouseEvent e) {
                startx = e.getX();
                starty = e.getY();
                // graphics.setColor(Color.BLACK);
                if (jpanelLeft.commd.equals("polygon")) {
                    if (count == 0) {
                        polyStartx = startx;
                        polyStarty = starty;
                    }
                } else if ("eraser".equals(jpanelLeft.commd)) {
                    graphics.setColor(Color.WHITE);
                }
            }

            // 鼠标释放
            public void mouseReleased(MouseEvent e) {
                endx = e.getX();
                endy = e.getY();
                // 画线
                if (jpanelLeft.commd.equals("line")) {
                    graphics.drawLine(startx, starty, endx, endy);
                } else if (jpanelLeft.commd.equals("oval")) {// 椭圆
                    graphics.drawOval(Math.min(startx, endx),
                            Math.min(starty, endy), Math.abs(endx - startx),
                            Math.abs(endy - starty));
                } else if (jpanelLeft.commd.equals("rect")) {// 矩形
                    graphics.drawRect(Math.min(startx, endx),
                            Math.min(starty, endy), Math.abs(endx - startx),
                            Math.abs(endy - starty));
                } else if (jpanelLeft.commd.equals("polygon")) {// 多边形
                    if (count == 0) {
                        graphics.drawLine(startx, starty, endx, endy);
                        polygonMiddlex = endx;
                        polygonmiddley = endy;
                        count++;

                    }

                } else if (jpanelLeft.commd.equals("round_rect")) {
                    graphics.drawRoundRect(Math.min(startx, endx),
                            Math.min(starty, endy), endx - startx, endy
                                    - starty, 100, 100);
                }
            }

            public void mouseExited(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {

            }

        };
        this.addMouseMotionListener(mouseMotionlistener);
        this.addMouseListener(mouse);

    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值