编程语言
首页 > 编程语言> > Java 从零开始实现一个画图板、以及图像处理功能,代码可复现

Java 从零开始实现一个画图板、以及图像处理功能,代码可复现

作者:互联网

Java 从零开始实现一个画图板、以及图像处理功能,代码可复现

这是一个学习分享博客,带你从零开始实现一个画图板、图像处理的小项目,为了降低阅读难度,本博客将画图板的一步步迭代优化过程展示给读者,篇幅较长,Java初学者可放心食用。(文末有源代码)

本博客实现的功能(根据本文讲解的顺序)

界面效果

image
image
image

image

image

image

怎么样?如果觉得还不错的话就请继续看下去吧!
首先我们要写一个界面,就要给界面添加一个监听器,对监听器不太熟悉的同学,可以看我的这篇文章 常见监听器用法

第一步:创建画布

package drawBoard_test;

import javax.swing.*;
import java.awt.*;

public class DrawUI extends JFrame {

    String[] strs = {"直线","签字笔","实时直线", "谢尔宾斯基地毯","递归KLine","矩形", "圆", "实心矩形", "实心圆", "等腰三角形", "三角形", "多边形",
            "改进多边形","立方体",  "橡皮擦", "撤回", "保存", "打开"};
    Color[] color = {Color.red,Color.white,Color.black,Color.blue};
    //添加功能和颜色按钮
    public void addButton(){
        for(String str : strs){
            JButton btn = new JButton(str);
            add(btn);
        }
        Dimension dim = new Dimension(30,30);
        for(Color c : color){
            JButton btn = new JButton();
            btn.setBackground(c);
            btn.setPreferredSize(dim);
            add(btn);
        }
    }

    public void initUI(){
        this.setTitle("画图板");
        FlowLayout flow = new FlowLayout();
        this.setLayout(flow);
        this.setSize(1000,800);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.addButton();
        this.setVisible(true);
        Graphics g = getGraphics();

    }

    public static void main(String[] args) {
        DrawUI drawUI = new DrawUI();
        drawUI.initUI();
    }
}

第二步:为窗体和按钮添加监听器

package drawBoard_test;

import java.awt.event.*;

public class DrawListenerFather implements ActionListener, MouseListener, MouseMotionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }
    @Override
    public void mousePressed(MouseEvent e) {

    }
    @Override
    public void mouseReleased(MouseEvent e) {

    }
    @Override
    public void mouseEntered(MouseEvent e) {

    }
    @Override
    public void mouseExited(MouseEvent e) {

    }
    @Override
    public void mouseDragged(MouseEvent e) {

    }
    @Override
    public void mouseMoved(MouseEvent e) {

    }
}

接下来,创建我们需要的监听器DrawListener,我们如果想在画图板上绘制的话,需要将主页面的画笔g传给监听器,
所以我们给监听器添加成员变量Graphic g;并添加setG()方法。

package drawBoard_test;

public class DrawListener extends DrawListenerFather {
    private Graphics g;

    public void setG(Graphics g) {
        this.g = g;
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }
    @Override
    public void mousePressed(MouseEvent e) {

    }
    @Override
    public void mouseReleased(MouseEvent e) {

    }
    @Override
    public void mouseEntered(MouseEvent e) {

    }
    @Override
    public void mouseDragged(MouseEvent e) {

    }
    @Override
    public void mouseMoved(MouseEvent e) {

    }
}
package drawBoard_test;

import javax.swing.*;
import java.awt.*;

public class DrawUI extends JFrame {
    DrawListener dl = new DrawListener();
    String[] strs = {"直线","签字笔","实时直线", "谢尔宾斯基地毯","递归KLine","矩形", "圆", "实心矩形", "实心圆", "等腰三角形", "三角形", "多边形",
            "改进多边形","立方体",  "橡皮擦", "撤回", "保存", "打开"};
    Color[] color = {Color.red,Color.white,Color.black,Color.blue};
    public void addButton(){
        for(String str : strs){
            JButton btn = new JButton(str);
            btn.addActionListener(dl); //添加事件监听器
            add(btn);
        }
        Dimension dim = new Dimension(30,30);
        for(Color c : color){
            JButton btn = new JButton();
            btn.setBackground(c);
            btn.setPreferredSize(dim);
            btn.addActionListener(dl); //添加事件监听器
            add(btn);
        }
    }

    public void initUI(){
        this.setTitle("画图板");
        FlowLayout flow = new FlowLayout();
        this.setLayout(flow);
        this.setSize(1000,800);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.addButton();
        this.setVisible(true);
        this.addMouseListener (dl);
        this.addMouseMotionListener (dl);//添加鼠标监听器
        Graphics g = getGraphics ();
        dl.setG(g); //将窗体的画笔g传入监听器
    }

    public static void main(String[] args) {
        DrawUI drawUI = new DrawUI();
        drawUI.initUI();
    }
}

接下来我们就可以去实现我们的绘图功能了!

第三步,完善监听器的功能

我们在监听器中创建一个字符串shapeName,当点击按钮时,将按钮上的字符赋给shapeName,再根据shapeName的值来决定鼠标监听器的具体行为

绘制直线以及更换画笔颜色

package drawBoard_test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

public class DrawListener extends DrawListenerFather {
    private Graphics g;
    String shapeName = null;  //按钮上的图形名称
    String btn_action ;  //按钮上的字符串
    Color color;  //记录当前画笔的颜色
    int x2,y2,x3,y3; //存放坐标
    public void setG(Graphics g) {
        this.g = g;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        /**
         *有的小伙伴可能会有疑问,为什么要用btn_action做一个中间量呢?试想一下,如果我们直接使用switch(shapeName),那么我们点击颜色按钮的时候
         * shapeName就会被换成空值"",我们就需要重新点击图形按钮再进行绘制。
         */
        btn_action = e.getActionCommand(); 
        
        if(btn_action.equals("")){
            JButton btn = (JButton) e.getSource(); //getSource方法获取触发此次事件的组件对象,返回值为Object类型
            color = btn.getBackground(); //获取按钮组件的背景颜色
            g.setColor(color);
            return;
        }else {
            shapeName =  btn_action;
        }

    }
    @Override
    public void mousePressed(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        x3 = e.getX();
        y3 = e.getY();
        if(shapeName == null) return;
        switch(shapeName){
            case "直线":
                g.drawLine(x2, y2, x3, y3);
                break;
        }
    }

}
    

此时,画图板可以绘制出直线,我们来看一下效果
image

实现直线的绘制之后,其余功能的实现也是水到渠成的,我们继续往下看。

矩形、圆、实心矩形、实心圆、等腰三角形、谢尔宾斯基地毯、递归KLine、立方体、橡皮擦功能以及颜色按钮的实现

package drawBoard_test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

public class DrawListener extends DrawListenerFather {
    private Graphics g;
    String shapeName = null;
    String btn_action ;
    Color color;
    int x2,y2,x3,y3;
    public void setG(Graphics g) {
        this.g = g;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        btn_action = e.getActionCommand(); //btn_action
        if(btn_action.equals("")){
            JButton btn = (JButton) e.getSource();
            color = btn.getBackground();
            g.setColor(color);
            return;
        }else {
            shapeName =  btn_action;
        }

    }
    @Override
    public void mousePressed(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        x3 = e.getX();
        y3 = e.getY();
        if(shapeName == null) return;
        switch(shapeName){
            case "直线":
                g.drawLine(x2, y2, x3, y3);
                break;

            case "矩形":
                g.drawRect(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                break;
            case "圆" :
                g.drawOval(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                break;
            case "谢尔宾斯基地毯" :
                Sierpinski(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                break;
            case "递归KLine" :
                KLine(x2,y2,x3,y3,y3-y2);
                break;
            case "实心矩形" :

                g.fillRect(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                break;
            case "实心圆" :

                g.fillOval(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                break;
            case "等腰三角形" :
                g.drawLine(x2,y3,x3,y3);
                g.drawLine(x2,y3,(x2+x3)/2,y2);
                g.drawLine(x3,y3,(x2+x3)/2,y2);
                break;
            case "立方体" :
                g.drawRect(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                g.drawLine(x2+(int)((x3-x2)*1.414/4),y2-(int)((y3-y2)*1.414/4),x2,y2);
                g.drawLine(x2+(int)((x3-x2)*1.414/4),y2-(int)((y3-y2)*1.414/4),x3+(int)((x3-x2)*1.414/4),y2-(int)((y3-y2)*1.414/4));
                g.drawLine(x3,y2,x3+(int)((x3-x2)*1.414/4),y2-(int)((y3-y2)*1.414/4));
                g.drawLine(x3+(int)((x3-x2)*1.414/4),y3-(int)((y3-y2)*1.414/4),x3+(int)((x3-x2)*1.414/4),y2-(int)((y3-y2)*1.414/4));
                g.drawLine(x3+(int)((x3-x2)*1.414/4),y3-(int)((y3-y2)*1.414/4),x3,y3);
                break;

            case "橡皮擦" :
                Color pre = g.getColor(); //记录之前的颜色 ,用完再换回去
                g.setColor( new JButton().getBackground());
                g.fillRect(Math.min(x2,x3),Math.min(y2,y3),Math.abs(x3-x2),Math.abs(y3-y2));
                g.setColor(pre);
                break;
        }
    }
    //递归KLine
    public void KLine(int x1 , int y1 , int x2 , int y2, int x){
      if(Math.abs(x2-x1)<= 1 || Math.abs(y2-y1) <= 1 || x < 1){
        g.drawLine(x1, y1, x2, y2);
        specialList.add(new Point(x1,y1));

        return;
      }
      Random random = new Random(0);
      int ran = random.nextInt(x);
      int mid = ((y2+y1)/2-x+ran*2);
      x = (int)(x*0.618);
      KLine(x1, y1, (x1+x2)/2, mid,x);
      KLine((x1+x2)/2, mid, x2,y2,x);

    }
    //谢尔宾斯基地毯
    public void Sierpinski(int x,int y,int w,int h){
        if(w>0&&h>0){
            g.fillRect(x+w/3,y+h/3,w/3,h/3);
            Sierpinski(x,y,w/3,h/3);
            Sierpinski(x+w/3,y,w/3,h/3);
            Sierpinski(x+2*w/3,y,w/3,h/3);
            Sierpinski(x,y+h/3,w/3,h/3);
            Sierpinski(x+2*w/3,y+h/3,w/3,h/3);
            Sierpinski(x,y+2*h/3,w/3,h/3);
            Sierpinski(x+w/3,y+2*h/3,w/3,h/3);
            Sierpinski(x+2*w/3,y+2*h/3,w/3,h/3);
        }
    }
    
}

较复杂一点的图形功能:签字笔、实时直线、三角形、多边形、改进多边形的实现

package drawBoard_test;

import java.awt.*;
import java.awt.event.MouseEvent;

public class Triangle {
  static int x1,y1,x2,y2,x3,y3; //对应三角形的三个点
  static int num; //作为已经点了几个点的控制信号
  public void drawTriangle(MouseEvent e , Graphics g){
    if(num == 0){
      x1 = e.getX();
      y1 = e.getY();
      num++;
    }else if(num == 1){
      x2 = e.getX();
      y2 = e.getY();
      g.drawLine(x1,y1,x2,y2);
      num++;
    }else if(num == 2){
      x3 = e.getX();
      y3 = e.getY();
      g.drawLine(x3,y3,x2,y2);
      g.drawLine(x3,y3,x1,y1);
      num=0;
    }
  }
}

package drawBoard_test;

import java.awt.*;
import java.awt.event.MouseEvent;

public class Polygon {
    static int x1,y1,x2,y2,x3,y3;
    static int num;
    public void drawPolygon(MouseEvent e , Graphics g){
        if(num == 0){
            x1 = e.getX();
            y1 = e.getY();
            num++;
        }else if(num == 1){
            x2 = e.getX();
            y2 = e.getY();
            g.drawLine(x1,y1,x2,y2);
            num++;
        }else if (num == 2){
            if(e.getButton()==3){
                g.drawLine(x1,y1,x2,y2);
                num=0;
                return;
            }
            x3 = e.getX();
            y3 = e.getY();
            g.drawLine(x3,y3,x2,y2);
            num++;
        }else if(num == 3){
            if(e.getButton()==3){
                g.drawLine(x1,y1,x3,y3);
                num=0;
                return;
            }
            x2 = e.getX();
            y2 = e.getY();
            g.drawLine(x3,y3,x2,y2);
            num--;
        }
    }
}

package drawBoard_test;

import java.awt.*;
import java.util.ArrayList;

public class PolygonPro {
    //挑选x坐标最大的点作为基准点,计算其余点与基准点的正切值,根据正切值从大到小依次连接,得到一个多边形。
    public void drawPolygonPro(ArrayList<Point> list, Graphics g){
        if(list.size() == 0||list.size() == 1||list.size() == 2) return;
        int right = findRight(list);
        System.out.println(right);
        Point rightPoint  = new Point(list.get(right).x, list.get(right).y);

        list.remove(right);
        double[] tan = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
            tan[i] = ((double) rightPoint.y-list.get(i).y)/(rightPoint.x-list.get(i).x);
        }
        int pre;
        int cur = indexOfMax(tan);

        g.drawLine(list.get(cur).x,list.get(cur).y, rightPoint.x, rightPoint.y);
        tan[cur] = Integer.MIN_VALUE;
        for (int i = 0; i < tan.length-1; i++) {
            pre = cur;
            cur = indexOfMax(tan);

            g.drawLine(list.get(pre).x,list.get(pre).y, list.get(cur).x,list.get(cur).y);
            tan[cur] = Integer.MIN_VALUE;
        }
        g.drawLine(list.get(cur).x,list.get(cur).y, rightPoint.x, rightPoint.y);

    }

    private int findRight(ArrayList<Point> list) {
        int result = 0;
        for (int i = 1; i < list.size(); i++) {
            result = list.get(i).x>list.get(result).x?i:result;
        }
        return result ;
    }

    //返回数组中的最大值的下标
    private int indexOfMax(double[] tan){
        int v= 0 ;
        for(int i = 1 ; i < tan.length; i ++){
            v = tan[i]>tan[v]?i:v;
        }
        return v;
    }
}
package drawBoard_test;

import java.awt.*;
import java.awt.event.MouseEvent;

public class Pen {
    public static int x1,y1,x2,y2;
    public static int state = 1;

    public void draw(MouseEvent e , Graphics g) {
        switch(state){
            case 1 :
                x1 = e.getX();
                y1 = e.getY();
                state = 2;
                break;
            case 2 :
                x2 = e.getX();
                y2 = e.getY();
                g.drawLine(x2,y2,x1,y1);
                state = 3;
                break;
            case 3 :
                x1 = e.getX();
                y1 = e.getY();
                g.drawLine(x2,y2,x1,y1);
                state = 2;
                break;
        }
    }
}

package drawBoard_test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

public class RealLine {
    public static int x1,y1,x2,y2,x3,y3;
    public void draw(MouseEvent e , Graphics g){
        Color pre = g.getColor();
        g.setColor( new JButton().getBackground());
        if(x2 !=0 ){
            g.drawLine(x2,y2,x1,y1);
        }
        g.setColor(pre);
        x3 = e.getX();
        y3 = e.getY();
        g.drawLine(x3,y3,x1,y1);
        x2=x3;
        y2=y3;
    }
}

监听器DrawListener中的代码可以参考以下代码

ArrayList<Point> list = new ArrayList<>();//用于存放改进多边形的所有的顶点。

@Override
    public void mouseClicked(MouseEvent e) {
        if(shapeName == null) return;
        switch(shapeName){
            case "三角形" :
                new Triangle().drawTriangle(e,g);
                break;
            case "多边形":
                new Polygon().drawPolygon(e,g);
                break;
            case "改进多边形":
                if(e.getButton()==3){
                    new PolygonPro().drawPolygonPro(list,g);
                    list.clear();
                    break;
                }else{
                    Point point = new Point(e.getX(),e.getY());
                    list.add(point);
                    break;
                }
            default:
                break;
        }
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        if(shapeName == null) return;
        switch (shapeName){
            case "实时直线":
                new RealLine().draw(e,g);
                break;
            case "签字笔":
                new Pen().draw(e,g);
                break;
        }
    }
    @Override
    public void mousePressed(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        if(shapeName == null) return;
        switch (shapeName){
            case "实时直线":
                RealLine.x1 = e.getX();
                RealLine.y1 = e.getY();
                RealLine.x2 = 0;
                break;
        }
    }

第四步:实现重绘

到这里,我们的画图板的雏形已经完成了,但是也存在以下几个问题:

如何解决这些问题呢?

我们可以把每个的图形看作一个类,再用List集合把它们存储起来,然后重写主页面的paint方法(paint方法会在窗体初
始化、拖动、改变尺寸、移出屏幕、最小化、最大化时调用),将List中的图形 在这个方法中遍历绘制出来。

@Override
    public void paint(Graphics g){
        super.paint(g);
        for(Shapes shape : dl.shapeList){
            shape.drawShape(g);
        }
    }

由于ArrayList只能存放一种对象,所以我们先创建一个父类shape,让shape的子类去重写drawShape方法。
在paint方法中遍历ArrayList集合时,每个对象调用自己独特的的drawShape方法,实现重绘。

package drawBoard_test2;

import java.awt.Color;
import java.awt.Graphics;
public class Shapes {
    public String shapeName; // 图形的名称(要根据图形的名称,判断重绘的方法)
    public Color color;  //画笔颜色(每个图形都有自己的颜色,重绘的时候图形的颜色也一样要保留)

    public void drawShape (Graphics g){
        g.setColor(color);

    }
}
package drawBoard_test2;

import javax.swing.*;
import java.awt.*;

public class BasicShape extends Shapes {
    private int x1,y1,x2,y2;

    public BasicShape(String shapeName, Color color,int x1, int y1, int x2, int y2) {
        this.shapeName = shapeName;
        this.color = color;
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
    @Override
    public void drawShape (Graphics g){
        super.drawShape(g);
        switch (shapeName){
            case "直线":
                g.setColor(color);
                g.drawLine(x1,y1,x2,y2);
                break;
            case "矩形":
                g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                break;
            case "圆" :
                g.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                break;
            case "谢尔宾斯基地毯" :
                Sierpinski(g,Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                break;

            case "实心矩形" :
                g.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                break;

            case "实心圆" :
                g.fillOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                break;

            case "等腰三角形" :
                g.drawLine(x1,y2,x2,y2);
                g.drawLine(x1,y2,(x1+x2)/2,y1);
                g.drawLine(x2,y2,(x1+x2)/2,y1);
                break;
            case "立方体" :
                g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                g.drawLine(x1+(int)((x2-x1)*1.414/4),y1-(int)((y2-y1)*1.414/4),x1,y1);
                g.drawLine(x1+(int)((x2-x1)*1.414/4),y1-(int)((y2-y1)*1.414/4),x2+(int)((x2-x1)*1.414/4),y1-(int)((y2-y1)*1.414/4));
                g.drawLine(x2,y1,x2+(int)((x2-x1)*1.414/4),y1-(int)((y2-y1)*1.414/4));
                g.drawLine(x2+(int)((x2-x1)*1.414/4),y2-(int)((y2-y1)*1.414/4),x2+(int)((x2-x1)*1.414/4),y1-(int)((y2-y1)*1.414/4));
                g.drawLine(x2+(int)((x2-x1)*1.414/4),y2-(int)((y2-y1)*1.414/4),x2,y2);
                break;

            case "橡皮擦" :
                Color pre = g.getColor(); //记录之前的颜色 ,用完再换回去
                g.setColor( new JButton().getBackground());
                g.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
                g.setColor(pre);
                break;

            default:
                break;
        }
    }
    public void Sierpinski(Graphics g,int x,int y,int w,int h){
        if(w>0&&h>0){
            g.fillRect(x+w/3,y+h/3,w/3,h/3);
            Sierpinski(g,x,y,w/3,h/3);
            Sierpinski(g,x+w/3,y,w/3,h/3);
            Sierpinski(g,x+2*w/3,y,w/3,h/3);
            Sierpinski(g,x,y+h/3,w/3,h/3);
            Sierpinski(g,x+2*w/3,y+h/3,w/3,h/3);
            Sierpinski(g,x,y+2*h/3,w/3,h/3);
            Sierpinski(g,x+w/3,y+2*h/3,w/3,h/3);
            Sierpinski(g,x+2*w/3,y+2*h/3,w/3,h/3);
        }

    }
}

当绘制出一个图形时,要将该图形加入到List集合中,所以监听器中的代码参考以下代码

@Override
    public void mouseReleased(MouseEvent e) {
        x3 = e.getX();
        y3 = e.getY();
        if(shapeName == null) return;
        switch(shapeName){
            case "直线":
            case "矩形":
            case "圆" :
            case "谢尔宾斯基地毯":
            case "实心矩形" :
            case "实心圆" :
            case "等腰三角形" :
            case "立方体" :
            case "橡皮擦" :
                BasicShape basicShape = new BasicShape(shapeName, new Color(color.getRGB()), x2, y2, x3, y3);
                basicShape.drawShape(g);
                shapeList.add(basicShape);
                break;
        }
    }

至此,我们就完成了简单图形的重绘。
我们还剩签字笔、实时直线、递归KLine、三角形、多边形、改进多边形等图形需要绘制。

这些图形有什么共同的属性可以提取吗?他们的共同点是坐标点都比较多,数量不能确定,我们可以设置一个List属性,把每个图形的点
都存在这个集合里, 然后重绘时,调用drawShape方法把集合里的点取出来,再绘制出来。

说做就做,我们创建一个specialShape类,主要属性为一个ArrayList集合,其余属性根据绘制的需要来定。

package drawBoard_test2;

import java.awt.*;
import java.util.ArrayList;

public class SpecialShape extends Shapes {
    public ArrayList<Point> specialList = new ArrayList<>();
    private Point first;
    private Point pre;
    private Point cur;

    public SpecialShape(String shapeName, Color color, ArrayList<Point> specialList) {
        this.shapeName = shapeName;
        this.color = color;
        for (Point p : specialList) {
            this.specialList.add(p);
        }
    }

    @Override
    public void drawShape(Graphics g) {
        super.drawShape(g);
        switch (shapeName) {
            case "三角形":
            case "多边形":
            case "改进多边形":
                if (specialList.isEmpty()) break;
                int i = 0;
                first = specialList.get(i++);
                cur = first;
                while (i < specialList.size()) {
                    pre = cur;
                    cur = specialList.get(i++);
                    g.drawLine(pre.x, pre.y, cur.x, cur.y);
                }
                g.drawLine(first.x, first.y, cur.x, cur.y);
                break;
            case "签字笔":
            case "递归KLine":
            case "实时直线":
                if (specialList.isEmpty()) break;
                int j = 0;
                while (j < specialList.size()-1) {
                    g.drawLine(specialList.get(j).x, specialList.get(j).y, specialList.get(j+1).x, specialList.get(++j).y);
                }
                break;
        }
    }
}

接下来,我们需要做的就是将每个图形的点按顺序添加进specialList中,点都收集完之后,将一个新建的specialShape对象放入我们的图形集合ShapeList中,所以我们修改每个图形中的代码:

/**
 * @param specialList 三角形的顶点存入SpecialShape的集合,存入的顺序应该为顺次连接的点的顺序
 * @param shapeList 重绘时使用的图形集合
 */
    public void drawTriangle(MouseEvent e , Graphics g, ArrayList<Point> specialList, Color color, ArrayList<Shapes> shapeList){
        if(num == 0){
        specialList.clear();
        x1 = e.getX();
        y1 = e.getY();
        num++;
        specialList.add(new Point(x1,y1));
        }else if(num == 1){
        x2 = e.getX();
        y2 = e.getY();
        g.drawLine(x1,y1,x2,y2);
        num++;
        specialList.add(new Point(x2,y2));
        }else if(num == 2){
        x3 = e.getX();
        y3 = e.getY();
        g.drawLine(x3,y3,x2,y2);
        g.drawLine(x3,y3,x1,y1);
        num=0;
        specialList.add(new Point(x3,y3));
        SpecialShape specialShape = new SpecialShape("三角形", new Color(color.getRGB()), specialList);
        shapeList.add(specialShape);
        }
    }

相应的监听器中的代码,做出相应的修改,

/**
 * 创建一个specialList集合用来存放每个图形的点,将它传入图形的绘制方法中,
 * 当收集到所有的点时,将以集合作为成员变量创建的specialShape对象存入shape集合中。
 */

ArrayList<Point> specialList = new ArrayList<>();

case "三角形" :
    new Triangle().drawTriangle(e,g,specialList,color,shapeList);
    break;

其他的类的方法也是如出一辙,大家在写出来之后,可以和鄙人的代码进行比对。这里给出其余代码:

public class Polygon {
  static int x1,y1,x2,y2,x3,y3;
  static int num;

  /**
   * 
    * @param e
   * @param g 
   * @param specialList 多边形的顶点存入SpecialShape的集合,存入的顺序应该为顺次连接的点的顺序
   * @param color 
   * @param shapeList 重绘时使用的图形集合
   */  
  public void drawPolygon(MouseEvent e , Graphics g, ArrayList<Point> specialList, Color color, ArrayList<Shapes> shapeList){
    if(num == 0){  //第一个点
      x1 = e.getX();
      y1 = e.getY();
      num++;
      specialList.clear();
      specialList.add(new Point(x1,y1));
    }else if(num == 1){ //
      x2 = e.getX();
      y2 = e.getY();
      g.drawLine(x1,y1,x2,y2);
      num++;
      specialList.add(new Point(x2,y2));
    }else if (num == 2){
      if(e.getButton()==3){ //右键结束时,所有的点已经确定,我们新建一个specialShape对象存入specialList集合中。
        g.drawLine(x1,y1,x2,y2);
        num=0;
        SpecialShape specialShape = new SpecialShape("多边形", new Color(color.getRGB()), specialList);
        shapeList.add(specialShape);
        specialList.clear();
        return;
      }
      x3 = e.getX();
      y3 = e.getY();
      g.drawLine(x3,y3,x2,y2);
      specialList.add(new Point(x3,y3));
      num++;
    }else if(num == 3){
      if(e.getButton()==3){
        g.drawLine(x1,y1,x3,y3);
        num=0;
        SpecialShape specialShape = new SpecialShape("多边形", new Color(color.getRGB()), specialList);
        shapeList.add(specialShape);
        specialList.clear();
        return;
      }
      x2 = e.getX();
      y2 = e.getY();
      g.drawLine(x3,y3,x2,y2);
      specialList.add(new Point(x2,y2));
      num--;
    }
  }
}
/**
 * 多边形对应监听器中的方法
 * mouseClicked方法
 */
case "多边形":
        new Polygon().drawPolygon(e,g,specialList,color,shapeList);
        break;

/**
 * 改进多边形类的draw方法
 * @param ArrayList<Point> list 多边形顶点的集合,顺序为鼠标绘制时 点击的顺序
 * @param ArrayList<Point> specialList 多边形的顶点存入SpecialShape的集合,存入的顺序应该为顺次连接的点的顺序
 * @param ArrayList<Shapes> shapeList 重绘时使用的图形集合
 */
public void drawPolygonPro(ArrayList<Point> list, Graphics g, ArrayList<Point> specialList, Color color, ArrayList<Shapes> shapeList){
        if(list.size() == 0||list.size() == 1||list.size() == 2) return;
        int right = findRight(list);
        System.out.println(right);
        Point rightPoint  = new Point(list.get(right).x, list.get(right).y);
        specialList.add(rightPoint);
        list.remove(right);
        double[] tan = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
        tan[i] = ((double) rightPoint.y-list.get(i).y)/(rightPoint.x-list.get(i).x);
        }
        int pre;
        int cur = indexOfMax(tan);
        specialList.add(list.get(cur));
        g.drawLine(list.get(cur).x,list.get(cur).y, rightPoint.x, rightPoint.y);
        tan[cur] = Integer.MIN_VALUE;
        for (int i = 0; i < tan.length-1; i++) {
        pre = cur;
        cur = indexOfMax(tan);
        specialList.add(list.get(cur));
        g.drawLine(list.get(pre).x,list.get(pre).y, list.get(cur).x,list.get(cur).y);
        tan[cur] = Integer.MIN_VALUE;
        }
        g.drawLine(list.get(cur).x,list.get(cur).y, rightPoint.x, rightPoint.y);

        }
/**
 * 改进多边形对应监听器中的方法
 */
    case "改进多边形":
        if(e.getButton()==3){

        new PolygonPro().drawPolygonPro(list,g,specialList,color,shapeList);
        SpecialShape specialShape = new SpecialShape("改进多边形",color,specialList);
        shapeList.add(specialShape);
        specialList.clear();
        list.clear();
        break;
        }else{
        Point point = new Point(e.getX(),e.getY());
        list.add(point);
        break;
        }

case "递归KLine":
        KLine(x2,y2,x3,y3,Math.abs(y3-y2));
        specialList.add(new Point(x3,y3));
        SpecialShape specialShape = new SpecialShape(shapeName, new Color(color.getRGB()), specialList);
        shapeList.add(specialShape);
        specialList.clear();
        break;
/**
 * 递归Kline实现方法
 */
public void KLine(int x1 , int y1 , int x2 , int y2, int x){
        if(Math.abs(x2-x1)<= 1 || Math.abs(y2-y1) <= 1 || x < 1){
        g.drawLine(x1, y1, x2, y2);
        specialList.add(new Point(x1,y1));

        return;
        }
        Random random = new Random(0);
        int ran = random.nextInt(x);
        int mid = ((y2+y1)/2-x+ran*2);
        x = (int)(x*0.618);
        KLine(x1, y1, (x1+x2)/2, mid,x);
        KLine((x1+x2)/2, mid, x2,y2,x);

        }

/**
 * 签字笔类的代码修改
 */
public void draw(MouseEvent e , Graphics g, ArrayList<Point> specialList, Color color, ArrayList<Shapes> shapeList) {
        switch(state){
        case 1 :
        x1 = e.getX();
        y1 = e.getY();
        specialList.add(new Point(x1,y1));
        state = 2;
        break;
        case 2 :
        x2 = e.getX();
        y2 = e.getY();
        specialList.add(new Point(x2,y2));
        g.drawLine(x2,y2,x1,y1);
        state = 3;
        break;
        case 3 :
        x1 = e.getX();
        y1 = e.getY();
        specialList.add(new Point(x1,y1));
        g.drawLine(x2,y2,x1,y1);
        state = 2;
        break;
        }

        }

/**
 * mouseDragged
  */
case "签字笔":
        new Pen().draw(e,g,specialList,color,shapeList);
        break;

/**
 * mouseReleased
 */
case "签字笔" :
        SpecialShape specialShape2 = new SpecialShape(shapeName, new Color(color.getRGB()), specialList);
        shapeList.add(specialShape2);
        specialList.clear();
        Pen.state=1;
        break;

public class RealLine {
  public static int x1,y1,x2,y2,x3,y3;
  public void draw(MouseEvent e , Graphics g, ArrayList<Point> specialList, Color color, ArrayList<Shapes> shapeList){
    Color pre = g.getColor();
    g.setColor( new JButton().getBackground());
    if(x2 !=0 ){
      g.drawLine(x2,y2,x1,y1);
    }
    g.setColor(pre);
    x3 = e.getX();
    y3 = e.getY();
    g.drawLine(x3,y3,x1,y1);
    x2=x3;
    y2=y3;
  }
}
/**
 * mousePressed
 */
    case "实时直线":
        specialList.add(new Point(x2,y2));
        RealLine.x1 = e.getX();
        RealLine.y1 = e.getY();
        RealLine.x2 = 0;
        break;

/**
 * mouseReleased
  */
    case "实时直线":
        specialList.add(new Point(x3,y3));
        SpecialShape specialShape3 = new SpecialShape(shapeName, new Color(color.getRGB()), specialList);
        shapeList.add(specialShape3);
        specialList.clear();
        break;

接下来,我们发现,图形确实可以实现重绘了,但是每次调用paint方法时,绘制的速度总是很慢,尤其是重绘谢尔宾斯基地毯时,是肉眼可见的慢,这是什么原因导致的呢?

我们知道,绘制的内容要显示到屏幕上,需要把 内存数据 提交 给显卡 ,通过显卡再渲染计算 显示到屏幕。
计算机的计算速度是非常快的,但是我们每计算出几个像素点,就直接输出到屏幕上,以至于 要画的次数很多,这导致了计算机IO 与 计算不匹配。

我们如何解决这种问题?

计算快,但IO很慢,我们就让计算机先计算好,再输出到屏幕上。
我们使用 缓存(BufferedImage类),把下一帧需要显示的画面上所有的图形内容都计算好并存起来,然后再一次性绘出 。

BufferedImage 缓存图片 属性:宽、高 格式为像素存储格式 使用Graphics类作为画笔

    public void paint(Graphics g){
        super.paint(g);
        BufferedImage bufferedImage = new BufferedImage(1000,800,BufferedImage.TYPE_INT_ARGB);
        Graphics buffg = bufferedImage.getGraphics();
        for(Shapes shape : dl.shapeList){
            shape.drawShape(buffg);

        }
        g.drawImage(bufferedImage,0,0,null);
    }

此时再来试试重绘的功能,是不是感觉很神奇。

public class RealLine {
    public static int x1,y1,x2,y2,x3,y3;
    public void draw(MouseEvent e , Graphics g, ArrayList<Point> specialList, Color color, ArrayList<Shapes> shapeList){
        Color pre = g.getColor();
        g.setColor( new JButton().getBackground());
        if(x2 !=0 ){
            g.drawLine(x2,y2,x1,y1);
        }
        g.setColor(pre);
        x3 = e.getX();
        y3 = e.getY();
        g.drawLine(x3,y3,x1,y1);
        x2=x3;
        y2=y3;
        BufferedImage bufferedImage = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
        Graphics buffs = bufferedImage.getGraphics();
        for(Shapes shape : shapeList){
            shape.drawShape(buffs);
        }
        g.drawImage(bufferedImage,0,0,null);
    }
}

第五步:实现撤回,清空功能

/**
 * actionPerformed
 */

switch(shapeName) {
    case "撤回":
        if (!shapeList.isEmpty()) {
        shapeList.remove(shapeList.size() - 1);
        drawUI.paint(g);
        }
        break;
    case "清空" :
        shapeList.clear();
        drawJPanel.paint(g);
        break;
}

/**
 * 然后我们需要涉及到传值的问题,我们在监听器页面添加一个drawUI对象成员,然后把DrawUI类中的main函数中的drawUI对象传给监听器
  */

第六步:打开与保存操作

package drawBoard_test2;

import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageShape extends Shapes {
    BufferedImage bufferedImage;
    
    @Override //重绘方法
    public void drawShape(Graphics g){
        g.drawImage(bufferedImage,0,0,null);
    }
    //封装 BufferedImage的set方法
    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }
}
String fileName;

/**
 * 打开操作步骤:将图片转化为二维数组,遍历每个点在画图板上画出
 * JFileChooser 文件选择器
 * FileNameExtensionFilter 文件过滤器,构造方法的参数JPG & GIF Images为筛选文件的选项, "jpg", "gif"为筛选文件的类型
 * 
 */
    //actionPerformed
    case "打开" :
        JFileChooser chooser = new JFileChooser(); 
        FileNameExtensionFilter filter = new FileNameExtensionFilter (
        "JPG & GIF Images", "jpg", "gif");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) { //JFileChooser.APPROVE_OPTION 批准选项
        System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getPath());
        fileName = chooser.getSelectedFile().getPath(); //获取文件的本地路径
        }
        BufferedImage bufferedImage = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
        Graphics buffg = bufferedImage.getGraphics();
        int[][] img = getImagePixel(fileName);
        drawImage(buffg,img);
        ImageShape imageShape = new ImageShape();
        imageShape.setBufferedImage(bufferedImage);
        g.drawImage(bufferedImage,0,0,null);
        shapeList.add(imageShape);
        break;
/**
 * drawImage将图形画在画图板上
  */
public  void drawImage(Graphics g ,int[][] img){

    for (int i = 0; i < img.length; i++) {
        for (int j = 0; j < img[i].length; j++) {
            Color c = new Color(img[i][j]);
            g.setColor(c);
            g.drawOval(i , j, 1, 1);
        }
    }
}
/**
 * getImagePixel 返回图片的二维数组

 */
public static int[][] getImagePixel(String filePath) {

    File file = new File(filePath); //filePath为文件路径
    BufferedImage bi = null;
    try{
        bi = ImageIO.read(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    int w = bi.getWidth();
    int h = bi.getHeight();
    int[][] imIndex = new int[w][h];
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            int pixel = bi.getRGB(i,j);
            imIndex[i][j] = pixel;
        }
    }
    return imIndex;
}

}

return imIndex;

}
/**
 * 保存为的文件名的后缀应为png
 */
case "保存":
    JFileChooser chooser2 = new JFileChooser();
    FileNameExtensionFilter filter2 = new FileNameExtensionFilter(
            "JPG & GIF Images", "jpg","gif"
        );
    chooser2.setFileFilter(filter2);
    int returnVal2 = chooser2.showSaveDialog(null);
    if(returnVal2 == JFileChooser.APPROVE_OPTION){
        System.out.println("You choose to save this file:" +
        chooser2.getSelectedFile().getPath());
    }
    //把所有的图形重绘到bufferedImage上,再把bufferedImage存入图片文件中
    BufferedImage bufferedImage2 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg2 = bufferedImage2.getGraphics();
    for(Shape shape : shapeList ){
        shape.drawShape(buffg2);
    }
    File file2 = new File(chooser2.getSelectedFile().getPath());
    try {
        ImageIO.write(bufferedImage2,"png",file2);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    break;

第七步:美化界面,并添加图片处理功能按钮

此时我们的画布、图形按钮、颜色按钮放在一起,如果我们后面再加入图形处理按钮,界面将会变得很不整洁,
所以我们使用边框布局来将窗体分区管理。

我们要将画板从整个窗体改成了一个JPanel,但是我们的重绘功能还需要重写过的paint方法,所以我们新建一个DrawJPanel类
来继承JPanel类,去重写paint方法。

public class DrawUI extends JFrame {
    DrawListener dl = new DrawListener();

    String[] strs = {"直线","签字笔","实时直线", "谢尔宾斯基地毯","递归KLine","矩形", "圆", "实心矩形", "实心圆", "等腰三角形", "三角形", "多边形",
            "改进多边形","立方体",  "橡皮擦", "撤回", "保存", "打开"};
    Color[] color = {Color.red,Color.yellow,Color.black,Color.blue};
    public void addShapeButton(JComponent component){
        for(String str : strs){
            JButton btn = new JButton(str);
            btn.addActionListener(dl);
            component.add(btn);
        }
    }
    public void addColorButton(JComponent component){
        Dimension dim = new Dimension(30,30);
        for(Color c : color){
            JButton btn = new JButton();
            btn.setBackground(c);
            btn.setPreferredSize(dim);
            btn.addActionListener(dl);
            component.add(btn);
        }
        Dimension dim2 = new Dimension(95,30);
        JButton btn = new JButton("选择颜色...");
        btn.setPreferredSize(dim2);
        btn.addActionListener(dl);
        component.add(btn);
    }
    public void addBeautyButton(JComponent component){
        String[] str = {"原图","马赛克","灰度","二值化","背景替换","油画","图片融合","磨皮"};
        for(String s : str){
            JButton btn = new JButton(s);
            btn.addActionListener(dl);
            component.add(btn);
        }
    }

    public void initUI(){
        JFrame jf = new JFrame("画图板");
        jf.setTitle("画图板");
        jf.setLayout(new BorderLayout());
        jf.setSize(1000,800);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //菜单栏
        JMenuBar jMenuBar = new JMenuBar();
        JMenu jMenu = new JMenu("菜单",true);
        String[] Menu = {"撤回","打开","保存","清空"};
        for(String s : Menu){
            JMenuItem jMenuItem = new JMenuItem(s);
            jMenu.add(jMenuItem);
            jMenuItem.addActionListener(dl);
        }
        jMenuBar.add(jMenu);
        jf.setJMenuBar(jMenuBar);

        JPanel shapeChooserPanel = new JPanel();
        DrawJPanel drawPanel = new DrawJPanel(); 
        JPanel ChooserPanel = new JPanel();
        JPanel ColorChooserPanel = new JPanel();
        ChooserPanel.setLayout(new BorderLayout());
        dl.drawJPanel = drawPanel;
        JPanel RightPanel = new JPanel();
        //大小
        Dimension dim = new Dimension(150,80);
        shapeChooserPanel.setPreferredSize(dim);
        ChooserPanel.setPreferredSize(dim);
        Dimension dim2 = new Dimension(150,330);
        RightPanel.setPreferredSize(dim2);
        ColorChooserPanel.setPreferredSize(dim2);
        ChooserPanel.setPreferredSize(dim2);
        //背景颜色
        Color color1 = new Color(-3355444);
        shapeChooserPanel.setBackground(color1);
        Color color2 = new Color(-6710887);
        ColorChooserPanel.setBackground(color2);
        ChooserPanel.setBackground(color2);
        RightPanel.setBackground(color1);
        //方位
        jf.add(shapeChooserPanel,BorderLayout.NORTH);
        jf.add(ChooserPanel,BorderLayout.EAST);
        jf.add(drawPanel,BorderLayout.CENTER);
        ChooserPanel.add(RightPanel,BorderLayout.SOUTH);
        ChooserPanel.add(ColorChooserPanel,BorderLayout.NORTH);

        //添加按钮
        addShapeButton(shapeChooserPanel);
        addColorButton(ColorChooserPanel);
        addBeautyButton(RightPanel);
        jf.setVisible(true);
        Graphics g = drawPanel.getGraphics ();
        drawPanel.addMouseMotionListener(dl);
        drawPanel.addMouseListener(dl);
        drawPanel.setDl(dl);
        dl.setG(g);

    }

    public static void main(String[] args) {
        new DrawUI().initUI();
    }
}

/**
 * 监听器中的actionPerformed方法
 */
if(btn_action.equals("选择颜色...")){
            color = JColorChooser.showDialog(drawJPanel, "选择颜色", Color.red);
            System.out.println(color.getRGB());
            g.setColor(color);
            return;
        }

第八步:图像处理功能

深入理解color类:

/**
 * 马赛克
 * 把像素点放大
 */
case "马赛克":
        BufferedImage bufferedImage3 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
        Graphics buffg3 = bufferedImage3.getGraphics();
        int[][] img3 = getImagePixel(fileName);
        drawImage_MSK(buffg3,img3);

        ImageShape imageShape3 = new ImageShape();
        imageShape3.setBufferedImage(bufferedImage3);
        g.drawImage(bufferedImage3,0,0,null);
        shapeList.add(imageShape3);
        break;

public  void drawImage_MSK(Graphics g ,int[][] img){
    int w = (drawJPanel.getWidth()- img.length)/2;
    int h = (drawJPanel.getHeight()- img[0].length)/2;
    for (int i = 0; i < img.length; i+=8) {
        for (int j = 0; j < img[i].length; j+=8) {
            Color c = new Color(img[i][j]);
            g.setColor(c);
            g.fillRect(i+w , j+h, 8, 8);
        }
    }
}   

/**
 * 灰度图像
 * rgb三个分量都相同,一般可以取其平均值
 * 这里使用的是灰度值的浮点法计算,读者可以参考该网址,尝试一下Gamma校正算法
 * https://baike.baidu.com/item/%E7%81%B0%E5%BA%A6%E5%80%BC/10259111?fr=aladdin
 */
case "灰度":
    BufferedImage bufferedImage6 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg6 = bufferedImage6.getGraphics();
    int[][] img6 = getImagePixel(fileName);
    drawImage_gray(buffg6,img6);
    ImageShape imageShape6 = new ImageShape();
    imageShape6.setBufferedImage(bufferedImage6);
    g.drawImage(bufferedImage6,0,0,null);
    shapeList.add(imageShape6);
    break;

public  void drawImage_gray(Graphics g ,int[][] img){
    int w = (drawJPanel.getWidth()- img.length)/2;
    int h = (drawJPanel.getHeight()- img[0].length)/2;
    for (int i = 0; i < img.length; i++) {
        for (int j = 0; j < img[i].length; j++) {
            int value = img[i][j];
            int red = (value>>16) & 0xff;
            int green = (value>>8) & 0xff;
            int blue = value & 0xff;
            int gray = (int) (0.3 * red + 0.59 * green + 0.11 * blue);
            Color c = new Color(gray,gray,gray);
            g.setColor(c);
            g.fillRect(i+w , j+h, 1, 1);
        }
    }
}
/**
 * 二值图像
 * 指仅有黑白两色的图像(大于某值的画白,小于某值的画黑)
 */
case "二值化":
    BufferedImage bufferedImage7 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg7 = bufferedImage7.getGraphics();
    int[][] img7 = getImagePixel(fileName);
    drawImage_binary(buffg7,img7);
    ImageShape imageShape7 = new ImageShape();
    imageShape7.setBufferedImage(bufferedImage7);
    g.drawImage(bufferedImage7,0,0,null);
    shapeList.add(imageShape7);
    break;

public  void drawImage_binary(Graphics g ,int[][] img){
    int w = (drawJPanel.getWidth()- img.length)/2;
    int h = (drawJPanel.getHeight()- img[0].length)/2;
    for (int i = 0; i < img.length; i++) {
        for (int j = 0; j < img[i].length; j++) {
            int value = img[i][j];
            int red = (value>>16) & 0xff;
            int green = (value>>8) & 0xff;
            int blue = value & 0xff;
            int gray = (int) (0.3 * red + 0.59 * green + 0.11 * blue);
            if(gray < 150){
                g.setColor(Color.black);
            }else {
                g.setColor(Color.white);
            }
            g.fillRect(i+w , j+h, 1, 1);
        }
    }
}
/**
 * 背景替换图像
 * 当图片的背景为白色时,我们将大于某一值的像素点,替换为另一张图片的像素点
 */

case "背景替换":
    BufferedImage bufferedImage8 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg8 = bufferedImage8.getGraphics();
    int[][] img8 = getImagePixel(fileName);
    int[][] background = getImagePixel("C:\\Users\\13630\\Desktop\\背景.jpg"); 
    drawImage_replaceBackground(buffg8,img8,background);
    ImageShape imageShape8 = new ImageShape();
    imageShape8.setBufferedImage(bufferedImage8);
    g.drawImage(bufferedImage8,0,0,null);
    shapeList.add(imageShape8);
    break;

public  void drawImage_replaceBackground(Graphics g ,int[][] img,int[][] background){
    int w = (drawJPanel.getWidth()- img.length)/2;
    int h = (drawJPanel.getHeight()- img[0].length)/2;
    for (int i = 0; i < img.length; i++) {
        for (int j = 0; j < img[i].length; j++) {
            int value = img[i][j];
            int red = (value>>16) & 0xff;
            int green = (value>>8) & 0xff;
            int blue = value & 0xff;
            int gray = (int) (0.3 * red + 0.59 * green + 0.11 * blue);
            if(gray > 240&&i< background.length&&j<background[i].length){
                g.setColor(new Color(background[i][j]));
            }else {
                g.setColor(new Color(img[i][j]));
            }
            g.fillRect(i+w , j+h, 1, 1);
        }
    }
}
/**
 * 原理与马赛克类似,不同的是油画效果要填充随机大小的色块
 */
case "油画":
    BufferedImage bufferedImage9 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg9 = bufferedImage9.getGraphics();
    int[][] img9 = getImagePixel(fileName);
    drawImage_OilPainting(buffg9,img9);
    ImageShape imageShape9 = new ImageShape();
    imageShape9.setBufferedImage(bufferedImage9);
    g.drawImage(bufferedImage9,0,0,null);
    shapeList.add(imageShape9);
    break;

public  void drawImage_OilPainting(Graphics g ,int[][] img){
    int w = (drawJPanel.getWidth()- img.length)/2;
    int h = (drawJPanel.getHeight()- img[0].length)/2;
    for (int i = 0; i < img.length; i+=5) {
        for (int j = 0; j < img[i].length; j+=5) {
            g.setColor(new Color(img[i][j]));
            Random random = new Random();
            int ran = random.nextInt(20)+5;
            g.fillOval(i+w , j+h, ran, ran);
        }
    }
}
/**
 * 需要两张照片
 * 融合后图片像素点的颜色 为融合前的两张照片像素点颜色以不同比例融合
 */
case "图片融合":
    BufferedImage bufferedImage10 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg10 = bufferedImage10.getGraphics();
    int[][] img10 = getImagePixel(fileName);
    int[][] background2 = getImagePixel("C:\\Users\\13630\\Desktop\\背景.jpg");
    drawImage_fusion(buffg10,img10,background2);
    ImageShape imageShape10 = new ImageShape();
    imageShape10.setBufferedImage(bufferedImage10);
    g.drawImage(bufferedImage10,0,0,null);
    shapeList.add(imageShape10);
    break;

public  void drawImage_fusion(Graphics g ,int[][] img,int[][] background){
    int w = Math.min(img.length, background.length);
    int h = Math.min(img[0].length, background[0].length);
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            Color ca = new Color(img[i][j]);
            Color cb = new Color(background[i][j]);
            int red = (int) (ca.getRed()*0.7+ cb.getRed()*0.3);
            int green = (int)(ca.getGreen()* 0.3+cb.getGreen()*0.7);
            int blue = (int)(ca.getBlue()*0.3+ cb.getBlue()*0.7);
            Color c = new Color(red,green,blue);
            g.setColor(c);
            g.fillRect(i , j, 1, 1);
        }
    }
}
case "原图":
    BufferedImage bufferedImage5 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
    Graphics buffg5 = bufferedImage5.getGraphics();
    int[][] img5 = getImagePixel(fileName);
    drawImage(buffg5,img5);
    ImageShape imageShape5 = new ImageShape();
    imageShape5.setBufferedImage(bufferedImage5);
    g.drawImage(bufferedImage5,0,0,null);
    shapeList.add(imageShape5);
    break;
 //画在画图区域的中央
public  void drawImage(Graphics g ,int[][] img){
    int w = (drawPanel.getWidth()- img.length)/2;
    int h = (drawPanel.getHeight()- img[0].length)/2;
    for (int i = 0; i < img.length; i++) {
        for (int j = 0; j < img[i].length; j++) {
            Color c = new Color(img[i][j]);
            g.setColor(c);
            g.drawOval(w+i , h+j, 1, 1);
        }
    }
}
package drawBoard_test2;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class SkinGrinding {
  public static int x1,y1,x2,y2;
  public static int state = 1;

  public void draw(MouseEvent e , Graphics2D g, ArrayList<Point> specialList, int[][] img, ArrayList<Shapes> shapeList,int w,int h) {
    switch(state){
      case 1 :
        x1 = e.getX();
        y1 = e.getY();
        specialList.add(new Point(x1,y1));
        state = 2;
        break;
      case 2 :
        x2 = e.getX();
        y2 = e.getY();
        g.setColor(new Color(img[x2-w][y2-h]));
        specialList.add(new Point(x2,y2));
        g.drawLine(x2,y2,x1,y1);
        state = 3;
        break;
      case 3 :
        x1 = e.getX();
        y1 = e.getY();
        specialList.add(new Point(x1,y1));
        g.setColor(new Color(img[x1-w][y1-h]));
        g.drawLine(x2,y2,x1,y1);
        state = 2;
        break;
    }

  }
}

监听器中添加的代码

监听器中加一个img11[][],用来存放当然处理的照片的像素点
/**
 * actionPerformed
 */
case "磨皮":
    img11 = getImagePixel(fileName);
    break;
/**
 * mousePressed
  */
case "磨皮":
    g2D = (Graphics2D)g;
    g2D.setStroke (new BasicStroke (3));
    specialList.add(new Point(x2,y2));
    break;
/**
 * mouseReleased
  */
case "磨皮":
    SpecialShape specialShape4 = new SpecialShape(shapeName, new Color(color.getRGB()), specialList);
    shapeList.add(specialShape4);
    specialList.clear();
    SkinGrinding.state=1;
    break;
/**
 * mouseDragged
  */
case "磨皮":
    int w = (drawJPanel.getWidth()- img11.length)/2;
    int h = (drawJPanel.getHeight()- img11[0].length)/2;
    new SkinGrinding().draw(e,g2D,specialList,img11,shapeList,w,h);
    break;

第九步:”更多操作“界面的绘制

package drawBoard_test2;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class ButtonUI extends JFrame {
    public static DrawUI drawUI;
    public void init (){
        JFrame jf = new JFrame();
        jf.setTitle("更多操作");
        jf.setSize(380,500);
        jf.setLocationRelativeTo(drawUI);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setLayout(new FlowLayout());
        addJSlider(jf);
        addButton(jf);
        addJSlider2(jf);
        jf.setVisible(true);

    }
    public void addButton (JFrame component){
        String[] strings = {"放大130%","缩小50%","向左旋转","向右旋转"};
        for(String s : strings){
            JButton btn = new JButton(s);
            component.add(btn);
            btn.addActionListener(DrawUI.dl);
        }

    }
    public void addJSlider(JFrame component){
        JLabel jl = new JLabel("缩放比例(%):");
        JSlider jSlider = new JSlider(0,200);
        jSlider.setToolTipText("缩放比例");
        jSlider.setMajorTickSpacing(30);
        jSlider.setMinorTickSpacing(10);
        jSlider.setPaintLabels(true);
        jSlider.setPaintTicks(true);
        jSlider.addChangeListener(DrawUI.dl);
        component.add(jl);
        component.add(jSlider);

    }
    public void addJSlider2(JFrame component){

        JLabel jl1 = new JLabel("红色亮度(%):");
        JSlider jSlider1 = new JSlider(0,0,200,100);
        jSlider1.setToolTipText("红色");
        jSlider1.setMajorTickSpacing(30);
        jSlider1.setMinorTickSpacing(10);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.addChangeListener(DrawUI.dl);
        component.add(jl1);
        component.add(jSlider1);

        JLabel jl2 = new JLabel("绿色亮度(%):");
        JSlider jSlider2 = new JSlider(0,0,200,100);
        jSlider2.setToolTipText("绿色");
        jSlider2.setMajorTickSpacing(30);
        jSlider2.setMinorTickSpacing(10);
        jSlider2.setPaintLabels(true);
        jSlider2.setPaintTicks(true);
        jSlider2.addChangeListener(DrawUI.dl);
        component.add(jl2);
        component.add(jSlider2);

        JLabel jl3 = new JLabel("蓝色亮度(%):");
        JSlider jSlider3 = new JSlider(0,0,200,100);
        jSlider3.setToolTipText("蓝色");
        jSlider3.setMajorTickSpacing(30);
        jSlider3.setMinorTickSpacing(10);
        jSlider3.setPaintLabels(true);
        jSlider3.setPaintTicks(true);
        jSlider3.addChangeListener(DrawUI.dl);
        component.add(jl3);
        component.add(jSlider3);

        //确认和取消按钮;
        JButton btn1 = new JButton("确认");
        btn1.addActionListener(DrawUI.dl);
        component.add(btn1);

        JButton btn2 = new JButton("取消");
        btn2.addActionListener(DrawUI.dl);
        component.add(btn2);

    }

    public static void main(String[] args) {
        new ButtonUI().init();
    }
}

第十步:放大、缩小功能


@Override
public void stateChanged(ChangeEvent e) {
    JSlider jSlider = (JSlider)e.getSource();
    String s = jSlider.getToolTipText();
    switch (s){
        case "缩放比例":
            multiple =  jSlider.getValue();
            int[][] img = getImagePixel(fileName);
            BufferedImage bufferedImage = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
            Graphics buffg = bufferedImage.getGraphics();
            drawImage_multiple(buffg,img);
            g.drawImage(bufferedImage,0,0,null);
            break;
    }
}

public void drawImage_multiple(Graphics g , int[][] img){
    int w = (int)((drawJPanel.getWidth()- img.length*1.0*(multiple)/100)/2);
    int h = (int)((drawJPanel.getHeight()- img[0].length*1.0*multiple/100)/2);
    for (int i = 0; i < img.length; i++) {
        for (int j = 0; j < img[i].length; j++) {
            g.setColor(new Color(img[i][j]));
            for (int k = (int)(i*1.0*multiple/100); k < (int)((i+1)*1.0*multiple/100) ; k++) {
                for (int l = (int)(1.0*j*multiple/100); l < (int)((j+1)*1.0*multiple/100); l++) {
                    g.drawRect(k+w,l+h,1,1);
                }
            }
        }
    }
}

第十一步:图片的颜色调整

package drawBoard_test2;

import javax.swing.text.Segment;
import java.awt.image.BufferedImage;
/**
 * 这是一个用来存储图片的动态数组类 /可以实现数组自动扩容
 * 存储的图片对象类型是: BufferedImage
 * 目前实现了:
 * add方法
 * get方法
 * remove方法
 * size方法
 */
public class ImageArray {

    private BufferedImage[] imgArray = {};

    /**
     * 数组默认初始化容量
     */
    private static final int defaultLength = 10;
    
    private int  size;

    /**
     * 数组当前的空间容量
     */
    private int length;
    
    // 每张存入进来图片的三 通道矩阵数组
    public ColorArray[] redArray = {};
    public ColorArray[] greenArray = {};
    public ColorArray[] blueArray = {};

    public int getSize(){
        return size;
    }

    //放大或缩小redArray的数值
    public int[][] multiple(int multiple , ColorArray colorArray){
        int w = colorArray.array.length;
        int h = colorArray.array[0].length;
        int[][] res = new int[w][h];
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                res[i][j] = Math.min(255,(int)(colorArray.array[i][j]*1.0*multiple/100));
            }
        }
        return res;
    }

    /**
     * 图片动态数组的初始化构造方法
     */
    public ImageArray(int initSize){
        if(initSize < defaultLength){
            length = defaultLength;
            imgArray = new BufferedImage[length];
            redArray = new ColorArray[length];
            greenArray = new ColorArray[length];
            blueArray = new ColorArray[length];
            size = 0;
        }else{
            length = initSize;
            imgArray = new BufferedImage[length];
            redArray = new ColorArray[length];
            greenArray = new ColorArray[length];
            blueArray = new ColorArray[length];
            size = 0;
        }
    }

    public void add(BufferedImage img){
        if(size >= length){
            int oldlength = length;
            length = oldlength + oldlength>>1;
            BufferedImage[] newArray = new BufferedImage[length];
            for (int i = 0; i < oldlength; i++) {
                newArray[i] = imgArray[i];
            }
            imgArray = newArray;
            newArray = null;
        }
        imgArray[size] = img ;
        redArray[size] = new  ColorArray(img,ColorArray.TYPE_RED);
        greenArray[size] = new  ColorArray(img,ColorArray.TYPE_GREEN);
        blueArray[size] = new  ColorArray(img,ColorArray.TYPE_BLUE);
        size++;
    }

    public void remove(int index) {
        imgArray[index] = null;
        size--;
    }
    //注意index的合法性
    public BufferedImage get(int index) {
        return imgArray[index] ;
    }

}

ColorArray 二维数组类,存放并处理颜色矩阵

package drawBoard_test2;

import java.awt.image.BufferedImage;

public class ColorArray{
    static final int TYPE_RED = 0;
    static final int TYPE_GREEN = 1;
    static final int TYPE_BLUE = 2;
    public int[][] array = {};
    ColorArray(BufferedImage img , int type){
        if(type == TYPE_RED){
            array = new int[img.getWidth()][img.getHeight()];
            for (int i = 0; i < img.getWidth(); i++) {
                for (int j = 0; j < img.getHeight(); j++) {
                    array[i][j] = (img.getRGB(i,j)>>16) & 0xff;
                }
            }
        }else if(type == TYPE_GREEN){
            array = new int[img.getWidth()][img.getHeight()];
            for (int i = 0; i < img.getWidth(); i++) {
                for (int j = 0; j < img.getHeight(); j++) {
                    array[i][j] = (img.getRGB(i,j)>>8) & 0xff;
                }
            }
        }else if(type == TYPE_BLUE){
            array = new int[img.getWidth()][img.getHeight()];
            for (int i = 0; i < img.getWidth(); i++) {
                for (int j = 0; j < img.getHeight(); j++) {
                    array[i][j] = img.getRGB(i,j) & 0xff;
                }
            }

        }
    }
}
@Override
    public void stateChanged(ChangeEvent e) {

        JSlider jSlider = (JSlider)e.getSource();
        String s = jSlider.getToolTipText();
        switch (s){
            case "缩放比例":
                multiple =  jSlider.getValue();
                int[][] img = getImagePixel(fileName);
                BufferedImage bufferedImage = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
                Graphics buffg = bufferedImage.getGraphics();
                drawImage_multiple(buffg,img);
                g.drawImage(bufferedImage,0,0,null);
                break;
            case "红色":
                multipleRed =  jSlider.getValue();
                BufferedImage bufferedImage1 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
                Graphics buffg1 = bufferedImage1.getGraphics();
                drawImage_multiple_color(buffg1,imageArray);

                g.drawImage(bufferedImage1,0,0,null);
                break;
            case "绿色":
                multipleGreen =  jSlider.getValue();
                BufferedImage bufferedImage2 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
                Graphics buffg2 = bufferedImage2.getGraphics();
                drawImage_multiple_color(buffg2,imageArray );
                g.drawImage(bufferedImage2,0,0,null);
                break;
            case "蓝色":
                multipleBlue =  jSlider.getValue();
                BufferedImage bufferedImage3 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
                Graphics buffg3 = bufferedImage3.getGraphics();
                drawImage_multiple_color(buffg3,imageArray);
                g.drawImage(bufferedImage3,0,0,null);
                break;
        }

    }

//注意:在图片打开的时候将从图片提取出来的BufferedImage放入imageArray中
public void drawImage_multiple_color(Graphics g , ImageArray imageArray){
    int index = imageArray.getSize()-1;
    int w = (drawJPanel.getWidth()- imageArray.get(index).getWidth())/2;
    int h = (drawJPanel.getHeight()- imageArray.get(index).getHeight())/2;
    int[][] red  ;
    int[][] green;
    int[][] blue ;
    red =   imageArray.multiple(multipleRed,imageArray.redArray[index]);
    green = imageArray.multiple(multipleGreen,imageArray.greenArray[index]);
    blue =  imageArray.multiple(multipleBlue,imageArray.blueArray[index]);
    for (int i = 0; i < imageArray.get(index).getWidth(); i++) {
        for (int j = 0; j < imageArray.get(index).getHeight() ; j++) {
            g.setColor(new Color(red[i][j],green[i][j],blue[i][j]));
            g.drawRect(i+w,j+h,1,1);
        }
    }
}    

第十二步:旋转

 case "向左旋转":
     BufferedImage bufferedImage15 = new BufferedImage(800,800,BufferedImage.TYPE_INT_ARGB);
     int img15[][] = getImagePixel(fileName);
     img15 = RotateRight(img15);
     Graphics buffg15 = bufferedImage15.getGraphics();
     drawImage(buffg15,img15);
     g.drawImage(bufferedImage15,0,0,null);
     ImageShape imageShape15 = new ImageShape();
     imageShape15.setBufferedImage(bufferedImage15);
     shapeList.add(imageShape15);
     break;

public  int[][] RotateRight(int[][] img){
    int w = img.length;
    int h = img[0].length;
    int[][] newImg = new int[h][w];
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            newImg[h-j-1][w-i-1] = img[i][j];
        }
    }
    return newImg;
}![image](https://www.icode9.com/i/l/?n=22&i=blog/2555328/202204/2555328-20220414151025986-1397523916.png)

效果图片:
image

一点点心得总结

1、开始写代码之前,一定要明确自己要实现什么功能,达到什么效果。
2、如何实现这样的效果。
3、实现过程中:当前实现的效果是否符合预期,如果不符合要重新制定计划。
4、搜集资料,撰写博客,发现自己的不足,旧知新学。

源代码以及图片素材链接
提取码:t1gp

标签:Java,y1,int,画图板,从零开始,x2,new,y2,public
来源: https://www.cnblogs.com/classicltl/p/16145023.html