应用界面
SimulationOfProducerAndConsumerProblems.java
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
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) {
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