计算机系统:生产与消费过程模拟

该博客通过Java Swing展示了生产者消费者问题的模拟,详细介绍了应用界面的实现,并提供了GitHub仓库链接供读者查看完整代码。
摘要由CSDN通过智能技术生成

应用界面

在这里插入图片描述

SimulationOfProducerAndConsumerProblems.java

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

/**
 * @author Zfans
 * @date 2020/10/18 10:49
 */

enum ProcessType {
   
    PRODUCE, BUFFER, CONSUME
}

class Constant {
   
    public static final int WINDOW_WIDTH = 800;
    public static final int WINDOW_HEIGHT = 400;
    public static final int THREAD_SLEEP_MILLIS = 5;
    public static final int PRI_X = 0;
    public static final int PRI_Y = 35;
    public static final int HIDE_X = -50;
    public static final int END_X = 250;

    public static final String IMAGE_FILE_PATH1 = "src/猪.png";
    public static final String IMAGE_FILE_PATH2 = "src/乘.png";
    public static final String ICON_FILE_PATH = "src/生产.png";

    public static int PRODUCE_X_AXIS_SPEED = 3;
    public static int CONSUME_X_AXIS_SPEED = 3;
    public static boolean produce = false;
    public static boolean consume = false;
    public static Integer product = 0;
    public static Integer max_product = 10;
}

public class SimulationOfProducerAndConsumerProblems {
   

    public static boolean isNumeric(String str) {
   
        for (int i = 0; i < str.length(); i++) {
   
            if (!Character.isDigit(str.charAt(i))) {
   
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
   

        //改变swing控件的风格为当前系统(windwos 10)风格的代码
        try {
   
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
   
            e.printStackTrace();
        }

        JFrame jFrame = new JFrame();
        jFrame.setLayout(null);
        jFrame.setSize(Constant.WINDOW_WIDTH, Constant.WINDOW_HEIGHT);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //把窗口位置设置到屏幕中心
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setTitle("模拟生产者与消费者问题");
        jFrame.getContentPane().setBackground(Color.white);

        jFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(Constant.ICON_FILE_PATH));

        ProcessPanel producerPanel = new ProcessPanel(ProcessType.PRODUCE, Constant.HIDE_X, Constant.PRI_Y, Constant.IMAGE_FILE_PATH1, null);
        ProcessPanel bufferPanel = new ProcessPanel(ProcessType.BUFFER, Constant.PRI_X, Constant.PRI_Y, Constant.IMAGE_FILE_PATH1, Constant.IMAGE_FILE_PATH2);
        ProcessPanel consumerPanel = new ProcessPanel(ProcessType.CONSUME, Constant.HIDE_X, Constant.PRI_Y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡森Zfans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值