画图板

本片博客是我不断话画图板的,我会根据新加的的内容,来不断完善这篇博客。
模型

这里写图片描述

用例点

1.能画基本的图形
2.能给图形上色
3.多边形重绘,每画一条直线,就能加上
4.拖拽边框,能保存图形
5.在增大的区域画图,能自动刷新出图形
6.画图拖拽鼠标过程中,能显示出图形的样式

当前完成度截图

这里写图片描述

设置通用全局变量(Field类)

在这个类中写入几个通用的公共类型的静态变量,方便其他类调用

public class Field {
    public static Color currentColor = Color.black;
    public static String currentCommand = "line";
    public static Graphics2D graphics = null;
    public static int exeCount;// 按下的次数 int pressedCount=0
}
画板部分(DrawBoard类)

    JFrame jfDrawFrame = null;// 全局变量

    public DrawBoard() {
        /** 第一步:设置窗体属性:大小、位置、标题、可见(必须要最后(add后)设置)、释放 **/
        setFrameParams();// 调用了自己的方法

        PanelLeft panelLeft = new PanelLeft(null);
        jfDrawFrame.add(panelLeft, BorderLayout.WEST);

        PanelCenter panelCenter = new PanelCenter(null);
        jfDrawFrame.add(panelCenter, BorderLayout.CENTER);

        // 设置底部的面板
        PanelBottom panelBottom = new PanelBottom(null);
        jfDrawFrame.add(panelBottom, BorderLayout.SOUTH);
        /** 第三步: 事件 **/
        jfDrawFrame.setAlwaysOnTop(true);
        jfDrawFrame.setVisible(true);

        // 必须在获得画笔:
        Graphics2D graphics2 = (Graphics2D) panelCenter.getGraphics();// 获得画笔
        // TODO 要给左边面板的g赋值
        Field.graphics = graphics2;
        // Field.graphics = graphics2;
        // Field.graphics = graphics2;
    }

    public void setFrameParams() {
        jfDrawFrame = new JFrame();
        jfDrawFrame.setSize(500, 500);
        jfDrawFrame.setLocationRelativeTo(null);
        jfDrawFrame.setTitle("画图板");
        jfDrawFrame.setDefaultCloseOperation(3);
        jfDrawFrame.setLayout(new BorderLayout());// 布局
        // null 空布局直接:给x,y坐标(屏幕适配有问题,做桌面程序没什么影响)
    }

左边框(PanelLeft类)



    public PanelLeft(Graphics2D g) {
        Field.graphics = g;

        Dimension d = new Dimension(70, 100);
        this.setPreferredSize(d);
        this.setBackground(Color.white);

        // 添加jbutton 添加图片
        String[] numble = { "star", "dot_rect", "eraser", "fill",
                "color_picker", "magnifier", "pencil", "brush", "airbrush",
                "word", "line", "curve", "rect", "polygon", "oval",
                "round_rect" };

        // 1.先要有监听器 :一个类implements ActionListener ;;
        // 接口不能new,有一个普通类,没有类名-》匿名类 =>匿名内部类
        ActionListener actionlListener = new ActionListener() {
            // 重写接口的抽象方法
            public void actionPerformed(ActionEvent e) {
                System.out.println("监听到了");
                // 区分哪个按钮触发
                Field.currentCommand = e.getActionCommand();
                // 获得当前触发按钮的命令
                Field.exeCount = 0;

                Stroke s = new BasicStroke(1);
                Field.graphics.setStroke(s);
                if ("eraser".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Color.white);
                    // 子类直接赋值给抽象父类、接口 ok:Stroke s = new BasicStroke(10);
                    Stroke s1 = new BasicStroke(10);// 接口 s = 子类;
                    Field.graphics.setStroke(s1);
                } else if ("brush".equals(Field.currentCommand)) {
                    Field.graphics.setStroke(new BasicStroke(10));
                    Field.graphics.setColor(Field.currentColor);

                } else if ("pencil".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                } else if ("airbrush".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                } else if ("line".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                } else if ("oval".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                } else if ("rect".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                } else if ("round_rect".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                } else if ("polygon".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Field.currentColor);// 设置颜色为当前记录的颜色
                }
            }
        };
        for (int i = 0; i < 16; i++) {
            ImageIcon imageIcon = new ImageIcon("images/" + numble[i] + ".jpg");
            JButton btnIcon = new JButton(imageIcon);
            // 设置大小
            Dimension preferredSize = new Dimension(25, 25);
            btnIcon.setPreferredSize(preferredSize);
            this.add(btnIcon);
            // 2.给按钮添加监听
            btnIcon.addActionListener(actionlListener);
            btnIcon.setActionCommand(numble[i]);
        }

    }


中间画板(PanelCenter类)




    int recordXEnd;
    int recordYEnd;
    // 坐标
    int XStart = 0;
    int YStart = 0;
    int XEnd = 0;
    int YEnd = 0;
    int x1;
    int y1;// 记录拖动
    // 多边形
    int recordX; // 起点 收尾相连的时候用
    int recordY;
    int recordXStart;
    int recordYStart;
    Shape shape;
    ArrayList<Shape> myList = new ArrayList<Shape>();
    Random random = new Random();// 随机数生成器

    public PanelCenter(Graphics2D g) {

        // 创建鼠标点击监听
        // 接口不能new,必须要构造一个匿名内部类
        MouseListener mouseListener = new MouseListener() {

            public void mouseClicked(MouseEvent e) {

                System.out.println("点击了");
                // 单击连接下一条线
                if ("polygon".equals(Field.currentCommand)) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1 && Field.exeCount == 1) {
                        recordX = e.getX();
                        recordY = e.getY();
                        /*
                         * graphics.drawLine(recordXEnd, recordYEnd, recordX,
                         * recordY);
                         */
                        myList.add(new LineShape(recordXEnd, recordYEnd,
                                recordX, recordY, Field.graphics.getColor(), 1));
                        repaint();
                        recordXEnd = recordX;
                        recordYEnd = recordY;
                    }
                    // 双击完成多边形
                    if (clickCount == 2 && Field.exeCount == 1) {
                        recordX = e.getX();
                        recordY = e.getY();
                        /*
                         * graphics.drawLine(recordXStart, recordYStart,
                         * recordX, recordY);
                         */
                        myList.add(new LineShape(recordXStart, recordYStart,
                                recordX, recordY, Field.graphics.getColor(), 1));
                        repaint();
                        Field.exeCount = 0;
                    }
                }
            }

            public void mouseEntered(MouseEvent e) {

                System.out.println("进入了");
            }

            public void mouseExited(MouseEvent e) {

                System.out.println("退出了");
            }

            public void mousePressed(MouseEvent e) {

                System.out.println("按下了");

                XStart = e.getX();
                YStart = e.getY();

            }

            public void mouseReleased(MouseEvent e) {

                System.out.println("释放了");
                XEnd = e.getX();
                YEnd = e.getY();
                // 画直线
                if ("line".equals(Field.currentCommand)) {
                    // graphics.drawLine(XStart, YStart, XEnd, YEnd);

                    myList.add(new LineShape(XStart, YStart, XEnd, YEnd,
                            Field.graphics.getColor(), 1));
                    repaint();

                }
                // 话椭圆
                if ("oval".equals(Field.currentCommand)) {

                    /*
                     * graphics.drawOval(Math.min(XStart, XEnd),
                     * Math.min(YStart, YEnd), Math.abs(XStart - XEnd),
                     * Math.abs(YStart - YEnd));
                     */
                    myList.add(new OvalShape(Math.min(XStart, XEnd), Math.min(
                            YStart, YEnd), Math.abs(XStart - XEnd), Math
                            .abs(YStart - YEnd), Field.graphics.getColor(), 1));
                    repaint();
                }
                // 画矩形
                if ("rect".equals(Field.currentCommand)) {
                    /*
                     * graphics.drawRect(Math.min(XStart, XEnd),
                     * Math.min(YStart, YEnd), Math.abs(XStart - XEnd),
                     * Math.abs(YStart - YEnd));
                     */
                    myList.add(new RectShape(Math.min(XStart, XEnd), Math.min(
                            YStart, YEnd), Math.abs(XStart - XEnd), Math
                            .abs(YStart - YEnd), Field.graphics.getColor(), 1));
                    repaint();
                }
                if ("round_rect".equals(Field.currentCommand)) {
                    myList.add(new Round_RectShape(Math.min(XStart, XEnd), Math
                            .min(YStart, YEnd), Math.abs(XStart - XEnd), Math
                            .abs(YStart - YEnd), Field.graphics.getColor(), 1));
                    repaint();
                }
                // 多边形
                if ("polygon".equals(Field.currentCommand)) {
                    if (Field.exeCount == 0) {
                        recordXStart = XStart;
                        recordYStart = YStart;
                        recordXEnd = XEnd;
                        recordYEnd = YEnd;
                        /*
                         * graphics.drawLine(recordXStart, recordYStart,
                         * recordXEnd, recordYEnd);
                         */
                        myList.add(new LineShape(recordXStart, recordYStart,
                                recordXEnd, recordYEnd, Field.graphics
                                        .getColor(), 1));
                        repaint();
                        Field.exeCount++;
                    } else if (Field.exeCount == 1) {
                        recordX = e.getX();
                        recordY = e.getY();
                        /*
                         * graphics.drawLine(recordXEnd, recordYEnd, recordX,
                         * recordY);
                         */
                        myList.add(new LineShape(recordXEnd, recordYEnd,
                                recordX, recordY, Field.graphics.getColor(), 1));
                        repaint();
                        recordXEnd = recordX;
                        recordYEnd = recordY;
                    }
                }
            }

        };// 匿名内部类需要最后加个;
            // 鼠标拖动事件
        MouseMotionListener mouseMotionListener = new MouseMotionListener() {

            public void mouseMoved(MouseEvent e) {

            }

            // 完成铅笔的功能
            public void mouseDragged(MouseEvent e) {

                if ("pencil".equals(Field.currentCommand)) {
                    // graphics.drawLine(XStart, YStart, e.getX(), e.getY());
                    myList.add(new PencilShape(XStart, YStart, e.getX(), e
                            .getY(), Field.graphics.getColor(), 1));
                    repaint();
                    XStart = e.getX();
                    YStart = e.getY();

                }
                // 喷枪
                else if ("airbrush".equals(Field.currentCommand)) {
                    Random random = new Random();

                    for (int i = 0; i < 30; i++) {
                        int nextInt = random.nextInt(8) - 4;
                        int nextInt2 = random.nextInt(8) - 4;
                        /*
                         * graphics.drawLine(e.getX() + nextInt, e.getY() +
                         * nextInt2, e.getX() + nextInt, e.getY() + nextInt2);
                         */
                        myList.add(new AirbrushShape(e.getX() + nextInt, e
                                .getY() + nextInt2, e.getX() + nextInt, e
                                .getY() + nextInt2, Field.graphics.getColor(),
                                1));
                        repaint();
                    }
                }
                // 橡皮
                else if ("eraser".equals(Field.currentCommand)) {

                    // graphics.drawLine(XStart, YStart, e.getX(), e.getY());
                    myList.add(new EraserShape(XStart, YStart, e.getX(), e
                            .getY(), Field.graphics.getColor(), 10));
                    repaint();
                    XStart = e.getX();
                    YStart = e.getY();
                } else if ("brush".equals(Field.currentCommand)) {

                    // graphics.drawLine(XStart, YStart, e.getX(), e.getY());
                    myList.add(new BrushShape(XStart, YStart, e.getX(), e
                            .getY(), Field.graphics.getColor(), 10));
                    repaint();
                    XStart = e.getX();
                    YStart = e.getY();
                }
                // 效果
                else if ("line".equals(Field.currentCommand)) {
                    shape = new LineShape(XStart, YStart, e.getX(), e.getY(),
                            Field.graphics.getColor(), 1);
                    repaint();
                } else if ("oval".equals(Field.currentCommand)) {
                    shape = new OvalShape(Math.min(XStart, e.getX()), Math.min(
                            YStart, e.getY()), Math.abs(XStart - e.getX()),
                            Math.abs(YStart - e.getY()),
                            Field.graphics.getColor(), 1);
                    repaint();
                } else if ("rect".equals(Field.currentCommand)) {
                    shape = new RectShape(Math.min(XStart, e.getX()), Math.min(
                            YStart, e.getY()), Math.abs(XStart - e.getX()),
                            Math.abs(YStart - e.getY()),
                            Field.graphics.getColor(), 1);
                    repaint();
                } else if ("round_rect".equals(Field.currentCommand)) {
                    shape = new Round_RectShape(Math.min(XStart, e.getX()),
                            Math.min(YStart, e.getY()), Math.abs(XStart
                                    - e.getX()), Math.abs(YStart - e.getY()),
                            Field.graphics.getColor(), 1);
                    repaint();
                } else if ("polygon".equals(Field.currentCommand)) {
                    // 多边形。效果
                    if (Field.exeCount == 0) {
                        shape = new LineShape(XStart, YStart, e.getX(),
                                e.getY(), Field.graphics.getColor(), 1);
                        repaint();

                    }
                    if (Field.exeCount == 1) {
                        shape = new LineShape(recordXEnd, recordYEnd, e.getX(),
                                e.getY(), Field.graphics.getColor(), 1);
                        repaint();
                    }

                }

            }
        };
        this.setBackground(Color.white);

        // 添加监听
        this.addMouseListener(mouseListener);
        this.addMouseMotionListener(mouseMotionListener);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (shape != null) {
            shape.Shape((Graphics2D) g);
        }
        for (int i = 0; i < myList.size(); i++) {
            myList.get(i).Shape((Graphics2D) g);
        }

    }




底部上色(PanelBottom)


    Color[] color = { Color.black, Color.white, Color.red, Color.green,
            Color.yellow, Color.blue, Color.pink, Color.gray, };

    public PanelBottom(Graphics2D g) {

        Dimension d = new Dimension(100, 70);
        this.setPreferredSize(d);
        // 创建监听事件

        ActionListener listener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("颜色选择了~~");
                String actionCommand = e.getActionCommand();
                int i = Integer.valueOf(actionCommand);// String -> int
                                                        // :Integer整数
                System.out.println(i);

                Color c = color[i];
                Field.graphics.setColor(c);
                Field.currentColor = c;

                if ("eraser".equals(Field.currentCommand)) {
                    Field.graphics.setColor(Color.white);
                }

            }
        };
        // 1 默认颜色的数组
        for (int i = 0; i < 8; i++) {
            JButton jButton = new JButton();
            jButton.setBackground(color[i]);
            Dimension dd = new Dimension(25, 25);
            jButton.setPreferredSize(dd);
            jButton.addActionListener(listener);
            jButton.setActionCommand(i + "");
            this.add(jButton);
        }

    }


使用泛型,实现增删查改功能的MyList类
private int size;
    private Object[] args = new Object[size];

    public int getSize() {
        return size;
    }

    // 增加
    public boolean add(T a) {
        int i;
        Object[] args_new = new Object[size + 1];
        for (i = 0; i < size; i++) {
            args_new[i] = args[i];
        }
        args_new[size] = a;
        args = args_new;
        size++;
        return true;
    }

    // 删除
    public boolean delete(int index) {
        Object[] args_new = new Object[size - 1];
        for (int i = 0; i < index; i++) {
            args_new[i] = args[i];
        }
        for (int i = index; i < size - 1; i++) {
            args_new[i] = args[i + 1];
        }
        args = args_new;
        size--;
        return false;
直线,椭圆等公共属性提取在一个父类,Shape类

    public int x1, y1, x2, y2;
    public Color color;
    public float stroke;

    public Shape(int x1, int y1, int x2, int y2, Color color, float stroke) {
        super();
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.color = color;
        this.stroke = stroke;
    }

    public abstract void Shape(Graphics2D g);

LineShape子类


    public LineShape(int x1, int y1, int x2, int y2, Color color, float stroke) {
        super(x1, y1, x2, y2, color, stroke);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Shape(Graphics2D g) {
        // TODO Auto-generated method stub
        g.setColor(color);
        g.setStroke(new BasicStroke(stroke));
        g.drawLine(x1, y1, x2, y2);

    }
AirbrushShape子类


    public AirbrushShape(int x1, int y1, int x2, int y2, Color color,
            float stroke) {
        super(x1, y1, x2, y2, color, stroke);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Shape(Graphics2D g) {
        // TODO Auto-generated method stub
        g.setColor(color);
        g.setStroke(new BasicStroke(stroke));
        g.drawLine(x1, y1, x2, y2);
    }

同理可以写出RectShape,Round_RectShape等子类
测试类
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DrawBoard drawBoard = new DrawBoard();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值