JAVA模拟实现移臂调度算法,包含用户图形界面
时间: 2024-03-26 15:34:42 浏览: 70
磁盘调度算法 图形界面 java实现
以下是一个使用Java Swing实现的移臂调度算法的图形用户界面:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DiskSchedulerGUI extends JFrame {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private JTextField requestField;
private JTextField headField;
private JTextField minField;
private JTextField maxField;
private JButton simulateButton;
private JPanel chartPanel;
private List<Integer> requests;
private int head;
private int minCylinder;
private int maxCylinder;
public DiskSchedulerGUI() {
setTitle("Disk Scheduler");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(new BorderLayout());
JPanel inputPanel = createInputPanel();
add(inputPanel, BorderLayout.NORTH);
chartPanel = createChartPanel();
add(chartPanel, BorderLayout.CENTER);
}
private JPanel createInputPanel() {
JPanel inputPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
inputPanel.add(new JLabel("Request Queue:"), c);
c.gridx = 1;
c.gridy = 0;
requestField = new JTextField(20);
inputPanel.add(requestField, c);
c.gridx = 0;
c.gridy = 1;
inputPanel.add(new JLabel("Head Position:"), c);
c.gridx = 1;
c.gridy = 1;
headField = new JTextField(10);
inputPanel.add(headField, c);
c.gridx = 0;
c.gridy = 2;
inputPanel.add(new JLabel("Min Cylinder:"), c);
c.gridx = 1;
c.gridy = 2;
minField = new JTextField(10);
inputPanel.add(minField, c);
c.gridx = 0;
c.gridy = 3;
inputPanel.add(new JLabel("Max Cylinder:"), c);
c.gridx = 1;
c.gridy = 3;
maxField = new JTextField(10);
inputPanel.add(maxField, c);
simulateButton = new JButton("Simulate");
simulateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
simulate();
}
});
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(simulateButton);
JPanel panel = new JPanel(new BorderLayout());
panel.add(inputPanel, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return panel;
}
private JPanel createChartPanel() {
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (requests != null) {
int chartWidth = getWidth() - 40;
int chartHeight = getHeight() - 80;
int x0 = 20;
int y0 = getHeight() - 40;
int dx = chartWidth / requests.size();
int dy = chartHeight / (maxCylinder - minCylinder + 1);
g.setColor(Color.BLACK);
// Draw x-axis and y-axis
g.drawLine(x0, y0, x0 + chartWidth, y0);
g.drawLine(x0, y0, x0, y0 - chartHeight);
// Draw cylinder labels on y-axis
for (int i = minCylinder; i <= maxCylinder; i++) {
int y = y0 - (i - minCylinder) * dy;
g.drawString(Integer.toString(i), x0 - 20, y);
}
// Draw request bars on x-axis
for (int i = 0; i < requests.size(); i++) {
int x = x0 + i * dx;
int y = y0 - (requests.get(i) - minCylinder) * dy;
g.setColor(Color.BLUE);
g.fillRect(x, y, dx, dy);
g.setColor(Color.BLACK);
g.drawRect(x, y, dx, dy);
}
// Draw head position
int headX = x0 + requests.indexOf(head) * dx;
g.setColor(Color.RED);
g.drawLine(headX, y0, headX, y0 - chartHeight);
}
}
};
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT - 100));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return panel;
}
private void simulate() {
String requestStr = requestField.getText().trim();
String[] requestArray = requestStr.split("\\s+");
requests = new ArrayList<>();
for (String s : requestArray) {
requests.add(Integer.parseInt(s));
}
head = Integer.parseInt(headField.getText().trim());
minCylinder = Integer.parseInt(minField.getText().trim());
maxCylinder = Integer.parseInt(maxField.getText().trim());
// Run disk scheduler algorithm
DiskScheduler scheduler = new DiskScheduler(requests, head, minCylinder, maxCylinder);
scheduler.run();
// Update chart panel
chartPanel.repaint();
}
public static void main(String[] args) {
DiskSchedulerGUI gui = new DiskSchedulerGUI();
gui.setVisible(true);
}
}
```
在这个GUI中,用户可以输入磁盘请求队列、磁头位置、最小和最大磁柱号码。当用户单击“Simulate”按钮时,程序将运行移臂调度算法,并在图形面板中可视化显示磁头的移动。
阅读全文