import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; class MyCanvas extends JComponent { //String list = "5, 6, 7, 8"; int top; int middle = 2; int bottom = 3; public void getNums(int n) { top = n; } // This is for drawing the "Stack" public void paint(Graphics g) { // Basic dimensions and the lines g.drawRect (10, 40, 200, 230); g.drawLine (10, 111, 210, 111); g.drawLine (10, 190, 210, 190); // Headers and such g.setFont(new Font("Arial", Font.BOLD, 20)); g.drawString ("Cache:", 15, 26); g.drawString ("" + top, 105, 80); g.drawString ("" + middle, 105, 160); g.drawString ("" + bottom, 105, 240); g.drawString ("Input:", 260, 26); } } ///////////////////////////////////////////////// // Uses a linked hashmap to aid in insertion. class LRUCache extends LinkedHashMap { public int size; // Constructor method (Private) public LRUCache(int size){ //Size, default load factor super(size, 0.75f, true); //Sets the size this.size = size; } //Creates a new instance of the Cache public static LRUCache newInstance(int size) { // Returns the new cache with designated size. return new LRUCache(size); } // Overrided LinkedHashMap's removeEldestEntry method @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > size; } public String setAndReturn(int num) { LRUCache runCache = LRUCache.newInstance(3); String newNum = Integer.toString(num); runCache.put(newNum, newNum); String returnValue = runCache.toString(); return returnValue; } } //////////////////////////////////////////////// // The actual handler for the GUI public class GUI extends JFrame { Scanner in = new Scanner(System.in); JLabel jl; JLabel jlTop; JLabel jlMid; JLabel jlBot; JButton jb; JPanel jp; JTextField jt; // This method controls the frame itself public GUI() { this.setTitle("LRU Demonstration"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(30, 30, 500, 400); this.getContentPane().add(new MyCanvas()); this.setVisible(true); // Setup of the text field/button jp = new JPanel(); jl = new JLabel(); jlTop = new JLabel(); jlMid = new JLabel(); jlBot = new JLabel(); jt = new JTextField("5", 10); jp.add(jt); jb = new JButton("Add to Cache"); jp.add(jb); // Setup of the button getting information. jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //jp.setBackground(Color.BLACK); String input = jt.getText(); jl.setText(input); jlTop.setText(input); } }); jp.add(jl, jlTop); add(jp, BorderLayout.SOUTH); } // The main method simply creates an instance of the GUI public static void main(String[] a) { String userLength = JOptionPane.showInputDialog("Enter a value for the size of the cache"); LRUCache runCache = LRUCache.newInstance(Integer.parseInt(userLength)); for(int i=0; i<10; i++) { String userInput = JOptionPane.showInputDialog("Enter a value into the cache"); runCache.put(userInput, userInput); String output = runCache.toString(); JOptionPane.showMessageDialog(null, "Cache: \n(Bottom, Middle, Top)\n" + output); } } }