Java generates two-dimensional code Graphics2D custom code eye shape

The Java 2D API provides several classes to define common geometric objects such as points, lines, curves, and rectangles. These geometry classes are  part of the java.awt.geom package. By proficient use of the Graphics2D class, you can draw any type of graphics.

Official website tutorial address: https://docs.oracle.com/javase/tutorial/2d/geometry/index.html

Let’s look at the effect first. Due to auditing reasons, the inner frame of the code hole in the lower left corner of this QR code is not generated.

 It can be seen from the effect diagram that the drawing of 11 combined code eyes has been realized, all of which are realized by Graphics2D drawing in Java. Below we form an example code for the shape drawing of each code eye. We follow the order of the renderings to illustrate the generation method.

  • Draw a square QR code eye

Code example:

package com.faea.test;

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

/**
 * 生成方形
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestOne extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        //尺寸
        int multiple = 30;
        //X坐标
        int x = 50;
        //Y坐标
        int y = 50;
        BasicStroke stroke = new BasicStroke(multiple);
        graphics2D.setStroke(stroke);
        int offset = multiple / 2;
        graphics2D.drawRect(x + offset,
                y + offset, multiple, multiple);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Rounded Rectangle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestOne());
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

  

  • Draw circular QR code eyes

Example code:

package com.faea.test;

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

/**
 * 生成圆形(粗边)
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestTwo extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        //尺寸
        int multiple = 30;
        //X坐标
        int x = 50;
        //Y坐标
        int y = 50;
        BasicStroke stroke = new BasicStroke(multiple);
        graphics2D.setStroke(stroke);

        int offset = multiple / 2;
        int size = multiple / 4;
        graphics2D.drawOval(x + offset, y + offset, size, size);


    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("circle_thick_edge");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestTwo());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Effect:

  • Draw a rounded rectangle QR code eye

code:

package com.faea.test;

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

/**
 * 生成圆角矩形
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestThree extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        //尺寸
        int multiple = 30;
        //X坐标
        int x = 50;
        //Y坐标
        int y = 50;

        BasicStroke stroke = new BasicStroke(multiple / 4);
        graphics2D.setStroke(stroke);


        graphics2D.drawRoundRect(x, y, multiple,
                multiple, multiple / 2, multiple / 2);


    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("rounded_thick_edge");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestThree());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 Effect:

  • Draw a square dot QR code eye

code:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

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

/**
 * 生成圆点
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestFour extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        int multiple = model.getMultiple();
        for (Point point : model.getPointList()) {
            int x = 20 + (int) point.getX() * model.getMultiple();
            int y = 20 + (int) point.getY() * model.getMultiple();
            graphics2D.fillOval(x, y, multiple, multiple);
        }

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("round_dot");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestFour());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Effect:

  •  Draw a small square dot QR code eye

code:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

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

/**
 * 生成方点
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestFive extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);
        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        int multiple = model.getMultiple();
        int offsetX = multiple / 8, offsetY = multiple / 8;
        int width = multiple - offsetX * 2, height = multiple - offsetY * 2;
        for (Point point : model.getPointList()) {
            int x = 20 + (int) point.getX() * model.getMultiple();
            int y = 20 + (int) point.getY() * model.getMultiple();
            graphics2D.fillRect(x + offsetX, y + offsetY, width, height);
        }

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("square_dot");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestFive());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

  • Draw single-sided rounded QR code eyes

code:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

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

/**
 * 生成方点
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestSix extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);

        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        BasicStroke stroke = new BasicStroke(model.getMultiple());
        graphics2D.setStroke(stroke);

        int x = 20 + (int) model.getPointList().get(0).getX() * model.getMultiple();
        int y = 20 + (int) model.getPointList().get(0).getY() * model.getMultiple();
        int width = model.getHoldPointNum() * model.getMultiple() - model.getMultiple() / 2;
        int offset = model.getMultiple() / 4;
        x += offset;
        y += offset;
        int height = width;
        int arcWidth = width / 2;
        int arcHeight = arcWidth;
        Path2D path = new Path2D.Double();
        path.moveTo(x, y + arcHeight);
        path.quadTo(x, y, x + arcWidth, y);
        path.lineTo(x + width, y);
        path.lineTo(x + width, y + height);
        path.lineTo(x, y + height);
        path.closePath();
        graphics2D.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("single_sided_fillet");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestSix());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 

  • Draw eye shape QR code eye

code:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

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

/**
 * 生成眼睛形状
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestSeven extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);

        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        BasicStroke stroke = new BasicStroke(model.getMultiple());
        graphics2D.setStroke(stroke);

        int x = 20 + (int) model.getPointList().get(0).getX() * model.getMultiple();
        int y = 20 + (int) model.getPointList().get(0).getY() * model.getMultiple();
        int width = model.getHoldPointNum() * model.getMultiple() - model.getMultiple() / 2;
        int offset = model.getMultiple() / 4;
        x += offset;
        y += offset;
        int height = width;
        int arcWidth = width / 2;
        int arcHeight = arcWidth;

        Path2D path = new Path2D.Double();
        path.moveTo(x, y + height - arcHeight);
        path.lineTo(x, y);
        path.lineTo(x + width - arcWidth, y);
        path.quadTo(x + width, y, x + width, y + arcHeight);
        path.lineTo(x + width, y + height);
        path.lineTo(x + arcWidth, y + height);
        path.quadTo(x, y + height, x, y + height - arcHeight);
        path.closePath();
        graphics2D.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("eye_shape");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestSeven());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 

  • Draw bubble shape QR code eye

Code example:

package com.faea.test;

import com.alibaba.fastjson.JSON;
import com.faea.qrcode.constants.CodeEyePositionEnum;
import com.faea.qrcode.model.CodeEyeOutDrawModel;

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

/**
 * 生成气泡形状
 *
 * @author liuchao
 * @date 2023/4/11
 */
public class CodeCreateTestNine extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;
        graphics2D.setColor(Color.RED);

        String json = "{'eyePosition':'LEFT_TOP','holdPointNum':7,'multiple':8,'pointList':[{'x':0,'y':0},{'x':1,'y':0},{'x':2,'y':0},{'x':3,'y':0},{'x':4,'y':0},{'x':5,'y':0},{'x':6,'y':0},{'x':0,'y':1},{'x':6,'y':1},{'x':0,'y':2},{'x':6,'y':2},{'x':0,'y':3},{'x':6,'y':3},{'x':0,'y':4},{'x':6,'y':4},{'x':0,'y':5},{'x':6,'y':5},{'x':0,'y':6},{'x':1,'y':6},{'x':2,'y':6},{'x':3,'y':6},{'x':4,'y':6},{'x':5,'y':6},{'x':6,'y':6}]}";
        CodeEyeOutDrawModel model = JSON.parseObject(json, CodeEyeOutDrawModel.class);
        BasicStroke stroke = new BasicStroke(model.getMultiple());
        graphics2D.setStroke(stroke);

        int x = 20 + (int) model.getPointList().get(0).getX() * model.getMultiple();
        int y = 20 + (int) model.getPointList().get(0).getY() * model.getMultiple();
        int width = model.getHoldPointNum() * model.getMultiple() - model.getMultiple() / 2;
        int offset = model.getMultiple() / 4;
        x += offset;
        y += offset;
        int height = width;
        int arcWidth = width / 2;
        int arcHeight = arcWidth;

        Path2D path = new Path2D.Double();
        path.moveTo(x, y + arcHeight);
        //左上角
        if (CodeEyePositionEnum.LEFT_TOP.equals(model.getEyePosition())) {
            path.quadTo(x, y, x + arcWidth, y);
            path.lineTo(x + width, y);
            path.lineTo(x + width, y + height - arcHeight);
            path.quadTo(x + width, y + height, x + width - arcWidth, y + height);
            path.lineTo(x + arcWidth, y + height);
            path.quadTo(x, y + height, x, y + height - arcHeight);
            //右上角
        } else if (CodeEyePositionEnum.RIGHT_TOP.equals(model.getEyePosition())) {
            path.lineTo(x, y);
            path.lineTo(x + width - arcWidth, y);
            path.quadTo(x + width, y, x + width, y + arcHeight);
            path.lineTo(x + width, y + height - arcHeight);
            path.quadTo(x + width, y + height, x + width - arcWidth, y + height);
            path.lineTo(x + arcWidth, y + height);
            path.quadTo(x, y + height, x, y + height - arcHeight);

            //左下角
        } else {
            path.quadTo(x, y, x + arcWidth, y);
            path.lineTo(x + width - arcWidth, y);
            path.quadTo(x + width, y, x + width, y + arcHeight);
            path.lineTo(x + width, y + height);
            path.lineTo(x + arcWidth, y + height);
            path.quadTo(x, y + height, x, y + height - arcHeight);

        }
        path.closePath();
        graphics2D.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("air_bubble");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CodeCreateTestNine());
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

 The above are code examples for drawing various eyelets. Dozens of different styles of eyelets can be formed by combining different shapes of outer eye + inner eye. If you have other styles, you can leave a message to communicate, and the editor will continue Learn to expand.

Guess you like

Origin blog.csdn.net/u011837804/article/details/130091685