Data Structure Visualizer Java
👤 Sharing: AI
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class DataStructureVisualizer extends JFrame {
private JPanel mainPanel;
private JComboBox<String> structureSelection;
private JTextField valueInput;
private JButton addButton;
private JButton removeButton;
private JPanel visualizationPanel;
private JLabel statusLabel;
private ArrayList<Integer> arrayList = new ArrayList<>();
private LinkedList<Integer> linkedList = new LinkedList<>();
private Stack<Integer> stack = new Stack<>();
private Queue<Integer> queue = new LinkedList<>();
private String selectedStructure = "ArrayList"; // Default
public DataStructureVisualizer() {
setTitle("Data Structure Visualizer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null); // Center the window
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
// Top Panel (Controls)
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
structureSelection = new JComboBox<>(new String[]{"ArrayList", "LinkedList", "Stack", "Queue"});
valueInput = new JTextField(5);
addButton = new JButton("Add");
removeButton = new JButton("Remove");
statusLabel = new JLabel("Status: Ready");
controlPanel.add(new JLabel("Data Structure:"));
controlPanel.add(structureSelection);
controlPanel.add(new JLabel("Value:"));
controlPanel.add(valueInput);
controlPanel.add(addButton);
controlPanel.add(removeButton);
mainPanel.add(controlPanel, BorderLayout.NORTH);
// Center Panel (Visualization)
visualizationPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawDataStructure(g);
}
};
visualizationPanel.setBackground(Color.WHITE);
mainPanel.add(visualizationPanel, BorderLayout.CENTER);
//Bottom Panel (Status)
JPanel statusPanel = new JPanel();
statusPanel.add(statusLabel);
mainPanel.add(statusPanel, BorderLayout.SOUTH);
add(mainPanel);
// Action Listeners
structureSelection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedStructure = (String) structureSelection.getSelectedItem();
visualizationPanel.repaint();
statusLabel.setText("Status: Selected " + selectedStructure);
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int value = Integer.parseInt(valueInput.getText());
addData(value);
valueInput.setText(""); // Clear the input
visualizationPanel.repaint();
statusLabel.setText("Status: Added " + value + " to " + selectedStructure);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(DataStructureVisualizer.this, "Invalid input. Please enter a number.", "Error", JOptionPane.ERROR_MESSAGE);
statusLabel.setText("Status: Invalid Input");
}
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
removeData();
visualizationPanel.repaint();
statusLabel.setText("Status: Removed from " + selectedStructure);
} catch (Exception ex) {
JOptionPane.showMessageDialog(DataStructureVisualizer.this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
setVisible(true);
}
private void addData(int value) {
switch (selectedStructure) {
case "ArrayList":
arrayList.add(value);
break;
case "LinkedList":
linkedList.add(value);
break;
case "Stack":
stack.push(value);
break;
case "Queue":
queue.offer(value);
break;
}
}
private void removeData() {
try {
switch (selectedStructure) {
case "ArrayList":
if (!arrayList.isEmpty()) {
arrayList.remove(arrayList.size() - 1);
} else {
throw new Exception("ArrayList is empty.");
}
break;
case "LinkedList":
if (!linkedList.isEmpty()) {
linkedList.removeLast();
} else {
throw new Exception("LinkedList is empty.");
}
break;
case "Stack":
if (!stack.isEmpty()) {
stack.pop();
} else {
throw new Exception("Stack is empty.");
}
break;
case "Queue":
if (!queue.isEmpty()) {
queue.poll();
} else {
throw new Exception("Queue is empty.");
}
break;
}
} catch(Exception e) {
JOptionPane.showMessageDialog(DataStructureVisualizer.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void drawDataStructure(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = 50;
int y = 50;
int width = 50;
int height = 30;
int spacing = 10;
g2d.setFont(new Font("Arial", Font.PLAIN, 12));
switch (selectedStructure) {
case "ArrayList":
for (int i = 0; i < arrayList.size(); i++) {
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(x, y, width, height);
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, width, height);
String value = String.valueOf(arrayList.get(i));
int stringWidth = g2d.getFontMetrics().stringWidth(value);
int stringHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(value, x + (width - stringWidth) / 2, y + (height + stringHeight/2) / 2 );
x += width + spacing;
}
break;
case "LinkedList":
int arrowSize = 10;
for (int i = 0; i < linkedList.size(); i++) {
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(x, y, width, height);
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, width, height);
String value = String.valueOf(linkedList.get(i));
int stringWidth = g2d.getFontMetrics().stringWidth(value);
int stringHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(value, x + (width - stringWidth) / 2, y + (height + stringHeight/2) / 2 );
if (i < linkedList.size() - 1) {
int xCenter = x + width;
int yCenter = y + height / 2;
// Draw arrow line
g2d.drawLine(xCenter, yCenter, xCenter + spacing + width/2, yCenter);
// Draw arrow head
int[] xPoints = {xCenter + spacing + width/2, xCenter + spacing + width/2 - arrowSize/2, xCenter + spacing + width/2 - arrowSize/2};
int[] yPoints = {yCenter, yCenter - arrowSize/2, yCenter + arrowSize/2};
g2d.fillPolygon(xPoints, yPoints, 3);
}
x += width + spacing + width/2 + spacing/2 ;
}
break;
case "Stack":
x = 300; // Center stack horizontally
y = 350; // Start from bottom
for (int i = stack.size() - 1; i >= 0; i--) {
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(x, y, width, height);
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, width, height);
String value = String.valueOf(stack.get(i));
int stringWidth = g2d.getFontMetrics().stringWidth(value);
int stringHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(value, x + (width - stringWidth) / 2, y + (height + stringHeight/2) / 2 );
y -= height + spacing;
}
break;
case "Queue":
x = 50;
for (Integer value : queue) {
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(x, y, width, height);
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, width, height);
String stringValue = String.valueOf(value);
int stringWidth = g2d.getFontMetrics().stringWidth(stringValue);
int stringHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(stringValue, x + (width - stringWidth) / 2, y + (height + stringHeight/2) / 2 );
x += width + spacing;
}
break;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DataStructureVisualizer();
}
});
}
}
```
👁️ Viewed: 10
Comments