画图板简单实现

项目一——画图板部分功能实现

一.参考应用界面
这里写图片描述

二.实现功能
1.左边面板界面实现
2.中间面板界面实现
3.底部面板界面实现
4.画直线、椭圆、矩形、圆角矩形功能实现
5.多边形功能实现
6.铅笔、橡皮、刷子功能实现
7.喷枪功能实现
8.颜色选择实现
9.设置窗体属性与调用函数
10.测试函数

三.主要步骤
1、左边面板(Leftpanel)界面实现

//构建普通方法,设置面板属性:大小、背景颜色、位置、按钮
public void Leftpanel() {
        JPanel Leftpanel=new JPanel();
        Dimension d=new Dimension(70,100);
        Leftpanel.setPreferredSize(d);
        Leftpanel.setBackground(Color.BLACK);
        jf.add(Leftpanel,BorderLayout.WEST);
            String images[]={ "airbrush", "brush", "color_picker", "curve","dot_rect", "eraser", "fill","line", "magnifier", "oval","pencil", "polygon", "rect", "round_rect", "star", "word" };
            for(int i=0;i<images.length;i++){
            ImageIcon image=new        
            ImageIcon("images/"+images[i]+".jpg");
            JButton button=new JButton(image);
            Dimension dd=new Dimension(25,25);
            button.setPreferredSize(dd);
            Leftpanel.add(button);
        }
    }

问题与回顾:
①普通方法和构造方法的区别
构造方法
功能:对一个类及变量实例化。构造方法返回值为对象在内存中的地址。
格式:public 类名(){}
普通方法
格式:public 返回类型 方法名(){}
②Dimension:用于设置绝对布局,只设置宽和高。
③BorderLayout边框布局示意图
这里写图片描述
2.中间面板(Centerpanel)界面实现

public void Centerpanel() {
        JPanel Centerpanel = new JPanel();
        Centerpanel.setBackground(Color.WHITE);
        jf.add(Centerpanel,BorderLayout.CENTER);
    }

3.底部面板(Bottompanel)界面实现

public void Bottompanel() {
        JPanel Bottompanel = new JPanel();
        Bottompanel.setBackground(Color.GRAY);
        Dimension db = new Dimension(100, 50);
        Bottompanel.setPreferredSize(db);
        for (int i = 0; i < colors.length; i++) {
            JButton button = new JButton();
            Dimension d = new Dimension(25, 25);
            button.setPreferredSize(d);
            button.setBackground(colors[i]);
        }

        jf.add(Bottompanel, BorderLayout.SOUTH);
    }

4.画直线、椭圆、矩形、圆角矩形功能实现
4.1思路:点击左边面板直线按钮,中间面板按下并拖动鼠标,松开即可。左边面板需要给按钮添加监听,并获得当前触发按钮的命令(currentCommand),区分不同按钮。
4.2代码实现
①Leftpanel面板先要有监听器 :一个类implements ActionListener ;

// ACtionListener接口不能new,有一个普通类,没有类名-》匿名类 =>匿名内部类

ActionListener actionListener = new ActionListener() {
// 重写了接口的抽象方法
// 事件触发
public void actionPerformed(ActionEvent e) {
currendCommand = e.getActionCommand();
}
};

MouseListener mouseListener = new MouseListener() {

            // 鼠标点击
            public void mouseClicked(MouseEvent e) {

            }

            // 鼠标进入
            public void mouseEntered(MouseEvent e) {

            }

            // 鼠标退出
            public void mouseExited(MouseEvent e) {

            }

            // 鼠标按下
            public void mousePressed(MouseEvent e) {
                Px = e.getX();
                Py = e.getY();
            }

            // 鼠标释放
            public void mouseReleased(MouseEvent e) {
                Rx = e.getX();
                Ry = e.getY();
                if ("line".equals(currentCommand)) {
                    g.drawLine(Px, Py, Rx, Ry);
                } else if ("oval".equals(currentCommand)) {
                    g.drawOval(Math.min(Px, Rx), Math.min(Py, Ry),
                            Math.abs(Px - Rx), Math.abs(Py - Ry));
                }else if ("rect".equals(currentCommand)){
                    g.drawRect((Math.min(Px, Rx)), Math.min(Py, Ry),
                            Math.abs(Px - Rx), Math.abs(Ry - Py));
                } else if ("round_rect".equals(currentCommand)) {
                    g.drawRoundRect(Math.min(Px, Rx),
                            Math.min(Py, Ry), Math.abs(Px - Rx),
                            Math.abs(Ry - Py), 10, 10);
                }
            }
        };

④左边面板按钮从图片上区分不同命令效果

//e.getActionCommond();
button.setActionCommand(images[i]);

注:反着画矩形、圆角矩形、椭圆时注意取较小的起点坐标和宽度高度的绝对值。
5、多边形功能实现
5.1 使用方法:第一下画直线,单击某坐标点连接该点与直线的终点,可多次单击,最后双击某点连接最近一次单击的点和第一下画的直线的起点,闭合图形。
5.2 累码思路:
①在监听器(MouseListener)中添加鼠标按下时的命令,如果多边形按钮获得命令,画第一条直线时记录起始坐标和终点坐标(最后要闭合多边形)
②在监听器(MouseListener)中添加点击事件,鼠标连续单击则交换首尾坐标画直线,双击时连接第一条直线时的起始坐标。
③点击过程为按下-》释放-》点击过程,因此上一个释放点坐标需要记录下来;双击是累加过来的,即双击1下的方法+双击2下的方法。
5.3.代码实现

 else if ("polygon".equals(currentCommand)) {
                    if (count == 0) {
                        polygonx = Px;
                        polygony = Py;
                        g.drawLine(Px, Py, Rx, Ry);
                        Rx1 = e.getX();
                        Ry1 = e.getY();
                        count++;
                    }

                }
public void mouseClicked(MouseEvent e) {
                if ("polygon".equals(currentCommand)){
                    clickcount = e.getClickCount();
                    if (clickcount == 1) {
                        Xclick1 = e.getX();
                        Yclick1 = e.getY();
                        g.drawLine(Rx1, Ry1, Xclick1, Yclick1);
                        Rx1 = Xclick1;
                        Ry1= Yclick1;
                    } else if (clickcount == 2) {
                        Xclick2 = e.getX();
                        Yclick2 = e.getY();
                        g.drawLine(polygonx, polygony, Xclick2, Yclick2);
                        count=0;
                    }

                }
            }

5.4 为避免选择多边形没画完就选择其他图形,再回来选择多边形时,又继续上次的画,应每次在ActionListener监听器中,将count初始化。

ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
                count=0;
                currentCommand = e.getActionCommand();
    }
};

6铅笔、橡皮、笔刷功能实现
①代码实现

MouseMotionListener mouseMotionlistener = new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
                Dx = e.getX();
                Dy = e.getY();
                if ("pencil".equals(currentCommand)) {
                    g.drawLine(Px, Py, Dx, Dy);
                    Px = Dx;
                    Py = Dy;
                } else if ("eraser".equals(currentCommand)) {
                    g.setColor(Color.white);
                    Stroke stroke=new BasicStroke(10);
                    ((Graphics2D) g).setStroke(stroke);
                    g.drawLine(Px, Py, Dx, Dy);
                    Px = Dx;
                    Py = Dy;
                } else if ("brush".equals(currentCommand)) {
                    Stroke stroke = new BasicStroke(10);
                    g.setStroke(stroke);
                    g.drawLine(Px, Py, Dx, Dy);
                    Px = Dx;
                    Py = Dy;
                }

            }

            public void mouseMoved(MouseEvent e) {

            }
        };

②注意:选择橡皮擦后,画笔颜色和大小的恢复。可在左边面板监听器(ActionListener)中初始化橡皮擦大小及颜色。

if ("erase".equals(currentComman)) {
                    g.setColor(Color.WHITE);
                    g.setStroke(stroke);

                } else {
                    g.setColor(currentColor);
                    BasicStroke stroke1 = new BasicStroke(1);
                    g.setStroke(stroke1);
                }

7.喷枪实现

else if("airbrush".equals(currentCommand)){
                    for (int i = 0; i < 25; i++) {
                        int nextInt1 = random.nextInt(8) - 4;
                        int nextInt2 = random.nextInt(8) - 4;
                        g.drawLine(Dx + nextInt1, Dy + nextInt2, Dx + nextInt1,
                                Dy + nextInt2);
                    }
                }

8.颜色选择实现
①首先我们要知道要先要有监听器(ActionListener),再有按钮,再为按钮添加事件。

ActionListener actionListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String command = e.getActionCommand();
                int i = Integer.valueOf(command);
                Color color = colors[i];
                g.setColor(color);
                currentcolor = color;
            }

        };
        for (int i = 0; i < colors.length; i++) {
            JButton button = new JButton();
            Dimension d = new Dimension(25, 25);
            button.setPreferredSize(d);
            button.setBackground(colors[i]);
            button.addActionListener(actionListener);
            button.setActionCommand(i + "");
            Bottompanel.add(button);
            jf.add(Bottompanel, BorderLayout.SOUTH);
        }

9.设置窗体属性与调用函数

public void DB() {
        setFrameParams();
        Leftpanel();
        Centerpanel();
        Bottompanel();
        jf.setVisible(true);
        g = (Graphics2D) Centerpanel.getGraphics();
        jf.setAlwaysOnTop(true);
        Centerpanel.addMouseMotionListener(mouseMotionLisener);

    }

    public void setFrameParams() {
        JFrame jf = new JFrame();
        jf.setTitle("画图板");
        jf.setSize(500, 500);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);
        jf.setLayout(new BorderLayout());
    }

10.测试函数

public class pTest {
    public static void main(String[] args) {
        类名 p = new 类名();
    }
}

大家实现了 么,下节我们来封装咯~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值