Freshman Java Curriculum Design Practice-Mini Printmaking Board (with source code and design documents)

I am Chenpi, an ITer in Internet Coding. Search for " JavaLib of Chenpi " on WeChat to read the latest article as soon as possible, and reply to [ Information ] to get my carefully organized technical information, e-books, first-line interview information and excellent. Resume template.

Article Directory


introduction


During the university, I took a Java language course as an elective. At the end of the semester, I designed and developed one 迷你版画图板and got it 优秀的成绩. Here is how to develop this mini-printing board. Students who have good basic knowledge of Java can complete the development in one hour! If you want 完整版可运行源代码, and 课程设计报告, you can follow my public account to contact me (see the introduction at the top of the article), and you can get it for free. Welcome everyone to like and collect!


Insert picture description here


System function structure


This program completes a software similar to the Windows drawing board, not a professionally developed and designed drawing software. This software runs under the Windows environment, the overall function structure diagram is as follows:

Insert picture description here

Module function code


First, you need to define the basic drawing class, and other classes to implement this type of overriding drawing method, such as circle, solid circle, ellipse, rectangle, etc. need to implement this base class.

package com.nobody;

import java.awt.Graphics2D;
import java.io.Serializable;

/**
 * @Description 图纸类,其他类实现此类重写绘画方法
 * @Author Mr.nobody
 * @Date 2021/5/30
 * @Version 1.0
 */
public class Drawings implements Serializable {

    // 定义色彩属性
    int R, G, B;

    // 定义字体属性
    int type;

    // 定义线条粗细属性
    float stroke;

    // 定义字体风格属性
    String s1, s2;

    // 定义坐标属性
    int x1, y1;
    int x2, y2;

    void draw(Graphics2D g) {}; // 定义绘图方法
}


Then define the graphics class preset by the system, inherit the above basic class, rewrite the draw method, and realize drawing specific graphics. The following simple examples of two of them, the demo code is as follows:

package com.nobody;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;

/**
 * @Description 圆类
 * @Author Mr.nobody
 * @Date 2021/5/30
 * @Version 1.0
 */
public class Circle extends Drawings {

    @Override
    void draw(Graphics2D g) {
        g.setPaint(new Color(R, G, B));
        g.setStroke(new BasicStroke(stroke));
        g.drawOval(Math.min(x1, x2), Math.min(y1, y2),
                Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)),
                Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)));
    }
}
package com.nobody;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;

/**
 * @Description 实心矩形类
 * @Author Mr.nobody
 * @Date 2021/5/30
 * @Version 1.0
 */
public class FillRect extends Drawings {
    @Override
    void draw(Graphics2D g) {
        g.setPaint(new Color(R, G, B));
        g.setStroke(new BasicStroke(stroke));
        g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    }
}


The main class of the artboard is the entire artboard container, and other components will be added to this artboard class to assemble a complete artboard together. First, you need to define some basic information representing the entire artboard in the class, as follows:

package com.nobody;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
import java.io.*;

/**
 * @Description 画板主类
 * @Author Mr.nobody
 * @Date 2021/5/30
 * @Version 1.0
 */
public class MiniDrawPad extends JFrame {

    // 按钮数组,存放上面名称的功能按钮
    private JButton[] choices;

    // 全部字体定义
    private String[] styleNames = {"微软雅黑", "华文中宋", "黑体", "宋体", "隶书", "华文彩云", "仿宋_GB2312", "华文行楷",
            "方正舒体", "新宋体", "幼圆", "Times New Roman", "Arial", "Serif", "Monospaced", "SonsSerif",
            "Garamond", "Courier New", "Chiller", "Edwardian Script ITC", "Chaparral Pro Light"};

    // 按钮面板定义
    JToolBar buttonPanel;

    // 显示鼠标状态的提示条
    private JLabel statusBar;

    // 画图区域
    private DrawPanel drawingArea;

    // 画图区域的初始宽高
    private static final int WIDTH = 1200, HEIGHT = 800;

    // 用来存放基本图形的数组
    Drawings[] itemList = new Drawings[4000];

    int index = 0; // 当前已经绘制的图形数目

    // 设置默认画图状态为随笔画
    private int currentChoice = 3;
    // 初始化画笔颜色为黑色
    private Color color = Color.black;

    // 用来存放当前色彩值
    int R, G, B;

    // 用来存放当前字体风格
    int f1, f2;

    // 用来存放当前字体
    String style1;

    // 设置画笔粗细,默认值为1.0f
    private float stroke = 1.0f;

    // 定义字体风格选择框 bold为粗体,italic为斜体,二者可以同时使用
    JCheckBox bold, italic;

    // 字体按钮组件
    JComboBox styles;
    
	// 省略其他方法定义
}


In the construction method of the drawing board class, the entire basic information is initialized and assigned, such as setting button rankings, button display icons, displaying text, setting button listeners, etc.

/**
     * 构造方法
     */
    public MiniDrawPad() {

        // 设置画图板标题
        super(" Mini Drawing Pad");

        // 定义菜单条
        JMenuBar bar = new JMenuBar();

        // 文件菜单项
        JMenu fileMenu = new JMenu("File(F)");
        // 快捷键 Alt + F
        fileMenu.setMnemonic('F');

        // 新建文件菜单项
        JMenuItem newItem = new JMenuItem("New         Alt+N");
        // 快捷键 Alt + N
        newItem.setMnemonic('N');
        // 监听事件
        newItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 事件被触发,则调用新建文件方法
                newFile();
            }
        });
        // 添加到文件菜单项中
        fileMenu.add(newItem);
        // 添加分隔符
        fileMenu.addSeparator();

        // 保存文件菜单项
        JMenuItem saveItem = new JMenuItem("Save         Alt+S");
        saveItem.setMnemonic('S');
        saveItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 事件被触发,则调用保存文件方法
                saveFile();
            }
        });
        fileMenu.add(saveItem);
        fileMenu.addSeparator();

        // 打开文件菜单项
        JMenuItem loadItem = new JMenuItem("Load         Alt+L");
        loadItem.setMnemonic('L');
        loadItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 事件被触发,则调用打开文件方法
                loadFile();
            }
        });
        fileMenu.add(loadItem);
        fileMenu.addSeparator();

        // 方便观看,不占用太多空间,中间省略一些代码
        // 方便观看,不占用太多空间,中间省略一些代码
        // 方便观看,不占用太多空间,中间省略一些代码

        // show();
        setVisible(true);
    }


The listener method will listen to different events. If the event is triggered, it will call a specific method to draw a picture. Several of the listener codes are as follows:

choices[0].addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        newFile();
    }
});

choices[1].addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        loadFile();
    }
});

choices[2].addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        saveFile();
    }
});


The last is the startup class, which is the entry point of the entire program. Run the main method directly to start the program.

package com.nobody;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.UIManager;

/**
 * @Description 软件入口类
 * @Author Mr.nobody
 * @Date 2021/5/30
 * @Version 1.0
 */
public class Main {

    /**
     * main 方法,程序入口
     *
     * @param args 主函数参数
     */
    public static void main(String[] args) {
        try {
            // 将画板界面设置为当前系统(Windows)风格,这样更加美观
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("设置画板界面风格失败!");
        }

        // 创建画板
        MiniDrawPad miniDrawPad = new MiniDrawPad();

        // 添加窗口事件,关闭窗口时退出程序
        miniDrawPad.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

How to get and run the program


First, you need to install JDK1.8 or above, and the program can be opened and run directly in Eclipse or IntelliJ IDEA.

The following is the program code structure, complete project and documentation, follow my public account [JavaLib of Chenpi], contact me, you can get it for free! If you don’t understand anything, you can also consult me, and I will also help answer the questions you encounter.

Insert picture description here


Insert picture description here