画图板

原创 2017年01月02日 21:09:39

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

模型

这里写图片描述

用例点

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();
    }

相关文章推荐

Java基础之简单画图板的实现

Java基础之简单画图板的实现 不难理解,画图板就是主要用鼠标的各种操作实现在窗体界面上绘图。所以实现一个简单的画图板,就必须先有一个事件源对象(窗体界面),为了更加美观,再在窗体界面上实例化两个面...

简易画图板的再次优化

前面我们实现了画板的一些功能,但是有没有注意到,每次窗口的改变,你绘制的图形就会不见了。 1、首先分析一下,为什么窗口一改变,绘制的图形就没了。这是因为窗体一旦发生改变,就会调用paint方法。 ...

java编程构建简单画图板4——重绘功能的完善

通过构建自定义队列存储图形的相关信息,利用paint方法利用存储信息绘制图形是重绘功能的实现方法,但由于不同图形的主要信息个数不同,图形信息的存储、调用、方法实现会比较麻烦,程序会更复杂、易出错,解决...

Android画图板 - 创建图形

...

画图板的多图形的实现

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Flow...

画图板的重绘

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.F...

Java项目之画图板(一)

画图板(一) 此画图板为第一个版本的画图板,下一个版本将会说明这个版本的缺点和不足,下一个版本为画图板(二) 1、步骤 第一步. 创建一个窗体(布局设为BorderLayout)第二步. 布局左边...
  • c_87_84
  • c_87_84
  • 2016年12月29日 14:22
  • 243

画图板项目纲要总结

通过几次关于画图板项目的学习,我基本上了解了项目开发的流程。 第一步,要对自己所做的项目有个大致的了解,要实现的功能有个思路框架,首先我们要完成的是整个界面布局的设计,接着,要创建一个窗体容器组件,...

画图板的简单实现

import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.ButtonGroup; import java...

Android笔记二十一. 一款简易画图板开发

一款简易画图板开发                                                   转载请表明出处:http://blog.csdn.net/u012637501...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:画图板
举报原因:
原因补充:

(最多只允许输入30个字)