import javax.swing.*; import java.awt.*; import java.util.List; import java.util.ArrayList; public class ControlPanel extends JFrame { private MiniMac miniMac; private JList memoryView; private JList programView; public ControlPanel() { miniMac = new MiniMac(); setupUI(); } private void setupUI() { // Setup UI components memoryView = new JList<>(); programView = new JList<>(); // Add action listeners to buttons JButton loadProgramButton = new JButton("Load Program"); loadProgramButton.addActionListener(e -> loadProgram()); // Layout components JPanel panel = new JPanel(new GridLayout(1, 2)); panel.add(new JScrollPane(memoryView)); panel.add(new JScrollPane(programView)); add(panel, BorderLayout.CENTER); add(loadProgramButton, BorderLayout.SOUTH); // Set frame properties setTitle("MiniMac Control Panel"); setSize(600, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } private void loadProgram() { // Sample method to load a program String program = "load 0 0\nload 1 10\nload 2 -1\nloop 10 { add 0 1 0; mul 0 2 0 }"; List instructions = MiniMacParser.parse(program); // Display program in program view DefaultListModel programModel = new DefaultListModel<>(); for (Instruction instruction : instructions) { programModel.addElement(instruction.toString()); } programView.setModel(programModel); // Execute program in MiniMac executeProgram(instructions); } private void executeProgram(List instructions) { for (Instruction instruction : instructions) { executeInstruction(instruction); } } private void executeInstruction(Instruction instruction) { String opcode = instruction.getOpcode(); int[] args = instruction.getArgs(); switch (opcode) { case "load": miniMac.load(args[0], args[1]); break; case "halt": miniMac.halt(); break; case "add": miniMac.add(args[0], args[1], args[2]); break; case "mul": miniMac.mul(args[0], args[1], args[2]); break; // Handle other instructions } // Update memory view updateMemoryView(); } private void updateMemoryView() { // Update memory view with MiniMac's memory contents DefaultListModel memoryModel = new DefaultListModel<>(); for (int i = 0; i < miniMac.getMemory().length; i++) { memoryModel.addElement("Memory[" + i + "]: " + miniMac.getMemory()[i]); } memoryView.setModel(memoryModel); } public static void main(String[] args) { SwingUtilities.invokeLater(ControlPanel::new); } } I added the other 3 files in the screenshots please check and help me to fix the code
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
public class ControlPanel extends JFrame {
private MiniMac miniMac;
private JList<String> memoryView;
private JList<String> programView;
public ControlPanel() {
miniMac = new MiniMac();
setupUI();
}
private void setupUI() {
// Setup UI components
memoryView = new JList<>();
programView = new JList<>();
// Add action listeners to buttons
JButton loadProgramButton = new JButton("Load
loadProgramButton.addActionListener(e -> loadProgram());
// Layout components
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(new JScrollPane(memoryView));
panel.add(new JScrollPane(programView));
add(panel, BorderLayout.CENTER);
add(loadProgramButton, BorderLayout.SOUTH);
// Set frame properties
setTitle("MiniMac Control Panel");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void loadProgram() {
// Sample method to load a program
String program = "load 0 0\nload 1 10\nload 2 -1\nloop 10 { add 0 1 0; mul 0 2 0 }";
List<Instruction> instructions = MiniMacParser.parse(program);
// Display program in program view
DefaultListModel<String> programModel = new DefaultListModel<>();
for (Instruction instruction : instructions) {
programModel.addElement(instruction.toString());
}
programView.setModel(programModel);
// Execute program in MiniMac
executeProgram(instructions);
}
private void executeProgram(List<Instruction> instructions) {
for (Instruction instruction : instructions) {
executeInstruction(instruction);
}
}
private void executeInstruction(Instruction instruction) {
String opcode = instruction.getOpcode();
int[] args = instruction.getArgs();
switch (opcode) {
case "load":
miniMac.load(args[0], args[1]);
break;
case "halt":
miniMac.halt();
break;
case "add":
miniMac.add(args[0], args[1], args[2]);
break;
case "mul":
miniMac.mul(args[0], args[1], args[2]);
break;
// Handle other instructions
}
// Update memory view
updateMemoryView();
}
private void updateMemoryView() {
// Update memory view with MiniMac's memory contents
DefaultListModel<String> memoryModel = new DefaultListModel<>();
for (int i = 0; i < miniMac.getMemory().length; i++) {
memoryModel.addElement("Memory[" + i + "]: " + miniMac.getMemory()[i]);
}
memoryView.setModel(memoryModel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ControlPanel::new);
}
}
I added the other 3 files in the screenshots please check and help me to fix the code
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images