package Question2; import javax.swing.JFrame; /** Displays a linked list. */ public class LinkedListViewer { public static void main(String args[]) { JFrame frame = new LinkedListFrame(); frame.setTitle("LinkedListViewer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
package Question2; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class LinkedListFrame extends JFrame { public LinkedListFrame() { setSize(FRAME_WIDTH, FRAME_HEIGHT); JPanel panel = new JPanel(); panel.add(makeButtonPanel ()); component = new LinkedListComponent(); component.setPreferredSize(new Dimension( COMPONENT_WIDTH, COMPONENT_HEIGHT)); panel.add(component); add(panel); } private JPanel makeButtonPanel() { JPanel panel = new JPanel(); addButton = new JButton("Add"); addButton.addActionListener(new AddButtonListener()); panel.add(addButton); newButton = new JButton("New Iter"); newButton.addActionListener(new NewButtonListener()); panel.add(newButton); nextButton = new JButton("Next"); nextButton.addActionListener(new NextButtonListener()); panel.add(nextButton); removeButton = new JButton("Remove"); removeButton.addActionListener(new RemoveButtonListener()); panel.add(removeButton); return panel; } class AddButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { component.add(); } } class NewButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { component.newIterator(); } } class NextButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { component.next(); } } class RemoveButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { component. remove(); } } private JButton newButton; private JButton nextButton; private JButton addButton; private JButton removeButton; private LinkedListComponent component; private static final int FRAME_WIDTH = 500; private static final int FRAME_HEIGHT = 400; private static final int COMPONENT_WIDTH = 500; private static final int COMPONENT_HEIGHT = 350; }
package Question2; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class LinkedListComponent extends JComponent { public LinkedListComponent() { list = new LinkedList(); newIterator(); next = 1; } public void newIterator() { iterator = (LinkedListIterator) list.listIterator(); repaint(); removeOk = false; } public void next() { if (iterator.hasNext()) { iterator.next(); repaint(); removeOk = true; } } public void add() { iterator.add(new Integer(next)); next++; repaint(); removeOk = false; } public void remove() { if (removeOk) { iterator.remove(); repaint(); removeOk = false; } } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; list.draw(g2); if (iterator != null) iterator.draw(g2); } private LinkedList list; private LinkedListIterator iterator; private int next = 1; private boolean removeOk = false; }
package Question2; import java.awt.Graphics2D; import java.util.NoSuchElementException; public class LinkedListIterator implements ListIterator { /** Constructs an iterator that points to the front of the linked list. */ public LinkedListIterator() { position = null; previous = null; } private class Node { public Object data; public Node next; } /** Adds an element to the front of the linked list. @param element the element to add */ public void addfirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; first = newNode; } /** Moves the iterator past the next element. @return the traversed element */ public Object next() { if (!hasNext()) throw new NoSuchElementException(); previous = position; // Remember for remove if (position == null) position = first; else position = position. next; return position.data; } /** Tests if there is an element after the iterator position. @return true if there is an element after the iterator position */ public boolean hasNext() { if (position == null) return first != null; else return position.next != null; } /** Adds an element before the iterator position and moves the iterator past the inserted element. @param element the element to add */ public void add(Object element) { if (position == null) { addfirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; } previous = position; } /** Removes the first element in the linked list. @return the removed element */ public Object removefirst() { if (first == null) throw new NoSuchElementException(); Object element = first.data; first = first.next; return element; } /** Removes the last traversed element. This method may only be called after a call to the next() method. */ public void remove() { if (previous == position) throw new IllegalStateException(); if (position == first) { removefirst(); } else { previous.next = position.next; } position = previous; } /** Sets the last traversed element to a different value. @param element the element to set */ public void set(Object element) { if (position == null) throw new NoSuchElementException(); position. data = element; } public void draw(Graphics2D g2) { // TODO: Draw iterator LinkedList.draw(g2); } private Node position; private Node previous; private Node first; }
package Question2; import java.awt.geom.Point2D; import java.util.ArrayList; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Line2D; import java.util.NoSuchElementException; /** A linked list is a sequence of nodes with efficient element insertion and removal. This class contains a subset of the methods of the standard java.util.LinkedList class. */ public class LinkedList { /** Constructs an empty linked list. */ public LinkedList() { first = null; } /** Returns the first element in the linked list. @return the first element in the linked list */ public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; } /** Removes the first element in the linked list. @return the removed element */ public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object element = first.data; first = first.next; return element; } /** Adds an element to the front of the linked list. @param element the element to add */ public void addfirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; first = newNode; } /** Returns an iterator for iterating through this list. @return an iterator for iterating through this list */ public ListIterator listIterator() { return new LinkedListIterator(); } public static void draw(Graphics2D g2) { // TODO: Draw all nodes for (int i = 0; i < count; i++) { Rectangle node = new Rectangle((int)(points.get(i).getX()), (int)(points.get(i).getY()), 30, 30); g2.draw(node); } } public static int count = 0; public static ArrayList<Point2D.Double> points; private Node first; private class Node { public Node() { // TODO: Determine coordinates for this node if (LinkedList.count == 0) { LinkedList.points.add(new Point2D.Double(20, 20)); LinkedList.count++; } else { LinkedList.points.add(new Point2D.Double(30 + points.get(LinkedList.count).getX(), 30 + points.get(LinkedList.count).getY())); LinkedList.count++; } } public Object data; public Node next; // TODO: Instance fields for coordinates(Done) } }
package Question2; /** A list iterator allows access of a position in a linked list. This interface contains a subset of the methods of the standard java.util.ListIterator interface. The methods for backward traversal are not included. */ public interface ListIterator { /** Moves the iterator past the next element. @return the traversed element */ Object next(); /** Tests if there is an element after the iterator position. @return true if there is an element after the iterator position */ boolean hasNext(); /** Adds an element before the iterator position and moves the iterator past the inserted element. @param element the element to add */ void add(Object element); /** Removes the last traversed element. This method may only be called after a call to the next() method. */ void remove(); /** Sets the last traversed element to a different value. @param element the element to set */ void set(Object element); }
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Iterator; import java.util.LinkedList; import javax.swing.JComponent; public class LinkedListComponent extends JComponent { private static final int COMPONENT_WIDTH = 500; private static final int COMPONENT_HEIGHT = 350; private java.util.LinkedList<Integer> list; private Iterator iterator; private int next = 0; private static final int delta = 10; public LinkedListComponent() { list = new java.util.LinkedList<Integer>(); newIterator(); } // also use frame.pack(); in main(...) @Override public Dimension getPreferredSize() { return new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT); } public void newIterator() { System.out.println("newIterator()"); iterator = list.listIterator(); //other iterators //iterator = list.descendingIterator(); //int startIndex = 5; //iterator = list.listIterator(startIndex); repaint(); } public void next() { System.out.println("not impl."); } /** * add element to list */ public void add() { System.out.println("add()"); list.add(next++); repaint(); System.out.println("list.size()=" + list.size()); } /** * remove element from list */ public void remove() { System.out.println("remove()"); if (--next < 0) { next = 0; } System.out.println("next=" + next); if (list.isEmpty()) { return; } list.removeLast(); repaint(); } @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); g2.setColor(Color.black); g2.draw3DRect(0, 0, COMPONENT_WIDTH - 1, COMPONENT_HEIGHT - 1, false); int previous = -1; for (Integer inner : list) { //for(Iterator i$ = list.iterator(); i$.hasNext();) { //Integer inner = (Integer)i$.next(); g2.draw3DRect(delta + inner * 30, delta, 20, 20, false); g2.drawString("" + inner, 5 + delta + inner * 30, 15 + delta); if (previous != -1) { //g2.draw(Shape s); g2.fill3DRect(20 + delta + previous * 30, delta + delta / 2, 10, 4, false); } previous = inner; } g2.dispose(); } }
//ver. with Next button impl. import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Iterator; import java.util.LinkedList; import javax.swing.JComponent; public class LinkedListComponent extends JComponent { private static final int COMPONENT_WIDTH = 500; private static final int COMPONENT_HEIGHT = 350; private java.util.LinkedList<Integer> list; private Iterator iterator; private int next = 0; private static final int delta = 10; public LinkedListComponent() { list = new java.util.LinkedList<Integer>(); newIterator(); } // also use frame.pack(); in main(...) @Override public Dimension getPreferredSize() { return new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT); } public void newIterator() { System.out.println("newIterator()"); iterator = list.listIterator(); //other iterators //iterator = list.descendingIterator(); //int startIndex = 5; //iterator = list.listIterator(startIndex); repaint(); } int valueReturnedByIterator = -1; public void next() { if (iterator.hasNext()) { System.out.println("iterator.next()"); valueReturnedByIterator = (Integer) iterator.next(); //Line:44 } } /** * add element to list */ public void add() { System.out.println("add()"); list.add(next++); repaint(); System.out.println("list.size()=" + list.size()); } /** * remove element from list */ public void remove() { System.out.println("remove()"); if (--next < 0) { next = 0; } System.out.println("next=" + next); if (list.isEmpty()) { return; } list.removeLast(); repaint(); } @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); g2.setColor(Color.black); g2.draw3DRect(0, 0, COMPONENT_WIDTH - 1, COMPONENT_HEIGHT - 1, false); int previous = -1; for (Integer inner : list) { //for(Iterator i$ = list.iterator(); i$.hasNext();) { //Integer inner = (Integer)i$.next(); g2.draw3DRect(delta + inner * 30, delta, 20, 20, false); g2.drawString("" + inner, 5 + delta + inner * 30, 15 + delta); if (previous != -1) { //g2.draw(Shape s); g2.fill3DRect(20 + delta + previous * 30, delta + delta / 2, 10, 4, false); } previous = inner; /// if (valueReturnedByIterator != -1) { g2.fill3DRect(15 + delta + valueReturnedByIterator * 30, delta + delta / 2, 10, 20, false); } } g2.dispose(); } }
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
at java.util.LinkedList$ListItr.next(LinkedList.java:696)
at question2.LinkedListComponent.next(LinkedListComponent.java:44)
at question2.LinkedListFrame$NextButtonListener.actionPerformed(LinkedListFrame.java:72)| DaniWeb Message | |
| Cancel Changes | |