Graphical Linked List

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Graphical Linked List

 
1
  #1
Nov 15th, 2008
"Write a program to display a linked list graphically. Draw each element of the list as a box, and indicate the links with line segments. Draw an iterator as in Figure 15-3. Supply buttons to move the iterator and to add and remove elements."

The elements of the node appear as boxes, with a number in them, connected by lines. Much of teh following code was given to me, the parts I have written are the draw methods, as indicated by the question. So far when i run the program, the buttons show up, but not of them do anything. This is a lot of code, but I appreciate any help given.

  1. package Question2;
  2.  
  3.  
  4. import javax.swing.JFrame;
  5.  
  6. /**
  7.   Displays a linked list.
  8. */
  9. public class LinkedListViewer
  10. {
  11. public static void main(String args[])
  12. {
  13. JFrame frame = new LinkedListFrame();
  14. frame.setTitle("LinkedListViewer");
  15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. frame.setVisible(true);
  17. }
  18. }

  1. package Question2;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Dimension;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10.  
  11. public class LinkedListFrame extends JFrame
  12. {
  13. public LinkedListFrame()
  14. {
  15. setSize(FRAME_WIDTH, FRAME_HEIGHT);
  16. JPanel panel = new JPanel();
  17. panel.add(makeButtonPanel ());
  18. component = new LinkedListComponent();
  19. component.setPreferredSize(new Dimension(
  20. COMPONENT_WIDTH, COMPONENT_HEIGHT));
  21. panel.add(component);
  22. add(panel);
  23. }
  24.  
  25. private JPanel makeButtonPanel()
  26. {
  27. JPanel panel = new JPanel();
  28.  
  29. addButton = new JButton("Add");
  30. addButton.addActionListener(new AddButtonListener());
  31. panel.add(addButton);
  32.  
  33. newButton = new JButton("New Iter");
  34. newButton.addActionListener(new NewButtonListener());
  35. panel.add(newButton);
  36.  
  37. nextButton = new JButton("Next");
  38. nextButton.addActionListener(new NextButtonListener());
  39. panel.add(nextButton);
  40.  
  41. removeButton = new JButton("Remove");
  42. removeButton.addActionListener(new RemoveButtonListener());
  43. panel.add(removeButton);
  44.  
  45. return panel;
  46. }
  47.  
  48. class AddButtonListener implements ActionListener
  49. {
  50. public void actionPerformed(ActionEvent event)
  51. {
  52. component.add();
  53. }
  54. }
  55.  
  56. class NewButtonListener implements ActionListener
  57. {
  58. public void actionPerformed(ActionEvent event)
  59. {
  60. component.newIterator();
  61. }
  62. }
  63.  
  64. class NextButtonListener implements ActionListener
  65. {
  66. public void actionPerformed(ActionEvent event)
  67. {
  68. component.next();
  69. }
  70. }
  71.  
  72. class RemoveButtonListener implements ActionListener
  73. {
  74. public void actionPerformed(ActionEvent event)
  75. {
  76. component. remove();
  77. }
  78. }
  79.  
  80.  
  81.  
  82. private JButton newButton;
  83. private JButton nextButton;
  84. private JButton addButton;
  85. private JButton removeButton;
  86. private LinkedListComponent component;
  87.  
  88. private static final int FRAME_WIDTH = 500;
  89. private static final int FRAME_HEIGHT = 400;
  90. private static final int COMPONENT_WIDTH = 500;
  91. private static final int COMPONENT_HEIGHT = 350;
  92. }

  1. package Question2;
  2.  
  3.  
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import javax.swing.JComponent;
  7.  
  8. public class LinkedListComponent extends JComponent
  9. {
  10. public LinkedListComponent()
  11. {
  12. list = new LinkedList();
  13. newIterator();
  14. next = 1;
  15. }
  16.  
  17. public void newIterator()
  18. {
  19. iterator = (LinkedListIterator) list.listIterator();
  20. repaint();
  21. removeOk = false;
  22. }
  23.  
  24. public void next()
  25. {
  26. if (iterator.hasNext())
  27. {
  28. iterator.next();
  29. repaint();
  30. removeOk = true;
  31. }
  32. }
  33.  
  34. public void add()
  35. {
  36. iterator.add(new Integer(next));
  37. next++;
  38. repaint();
  39. removeOk = false;
  40. }
  41.  
  42. public void remove()
  43. {
  44. if (removeOk)
  45. {
  46. iterator.remove();
  47. repaint();
  48. removeOk = false;
  49. }
  50. }
  51.  
  52. public void paintComponent(Graphics g)
  53. {
  54. Graphics2D g2 = (Graphics2D) g;
  55. list.draw(g2);
  56. if (iterator != null)
  57. iterator.draw(g2);
  58. }
  59. private LinkedList list;
  60. private LinkedListIterator iterator;
  61. private int next = 1;
  62. private boolean removeOk = false;
  63. }

  1. package Question2;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.util.NoSuchElementException;
  5.  
  6. public class LinkedListIterator implements ListIterator
  7. {
  8. /**
  9.   Constructs an iterator that points to the front
  10.   of the linked list.
  11.   */
  12. public LinkedListIterator()
  13. {
  14. position = null;
  15. previous = null;
  16. }
  17. private class Node
  18. {
  19. public Object data;
  20. public Node next;
  21. }
  22. /**
  23.   Adds an element to the front of the linked list.
  24.   @param element the element to add
  25.   */
  26. public void addfirst(Object element)
  27. {
  28. Node newNode = new Node();
  29. newNode.data = element;
  30. newNode.next = first;
  31. first = newNode;
  32. }
  33. /**
  34.   Moves the iterator past the next element.
  35.   @return the traversed element
  36.   */
  37. public Object next()
  38. {
  39. if (!hasNext())
  40. throw new NoSuchElementException();
  41. previous = position; // Remember for remove
  42.  
  43. if (position == null)
  44. position = first;
  45. else
  46. position = position. next;
  47.  
  48. return position.data;
  49. }
  50.  
  51. /**
  52.   Tests if there is an element after the iterator
  53.   position.
  54.   @return true if there is an element after the iterator
  55.   position
  56.   */
  57. public boolean hasNext()
  58. {
  59. if (position == null)
  60. return first != null;
  61. else
  62. return position.next != null;
  63. }
  64.  
  65. /**
  66.   Adds an element before the iterator position
  67.   and moves the iterator past the inserted element.
  68.   @param element the element to add
  69.   */
  70. public void add(Object element)
  71. {
  72. if (position == null)
  73. {
  74. addfirst(element);
  75. position = first;
  76. }
  77. else
  78. {
  79. Node newNode = new Node();
  80. newNode.data = element;
  81. newNode.next = position.next;
  82. position.next = newNode;
  83. position = newNode;
  84. }
  85. previous = position;
  86. }
  87. /**
  88.   Removes the first element in the linked list.
  89.   @return the removed element
  90.   */
  91. public Object removefirst()
  92. {
  93. if (first == null)
  94. throw new NoSuchElementException();
  95. Object element = first.data;
  96. first = first.next;
  97. return element;
  98. }
  99. /**
  100.   Removes the last traversed element. This method may
  101.   only be called after a call to the next() method.
  102.   */
  103. public void remove()
  104. {
  105. if (previous == position)
  106. throw new IllegalStateException();
  107.  
  108. if (position == first)
  109. {
  110. removefirst();
  111. }
  112. else
  113. {
  114. previous.next = position.next;
  115. }
  116. position = previous;
  117. }
  118.  
  119. /**
  120.   Sets the last traversed element to a different
  121.   value.
  122.   @param element the element to set
  123.   */
  124. public void set(Object element)
  125. {
  126. if (position == null)
  127. throw new NoSuchElementException();
  128. position. data = element;
  129. }
  130.  
  131. public void draw(Graphics2D g2)
  132. {
  133. // TODO: Draw iterator
  134. LinkedList.draw(g2);
  135. }
  136.  
  137. private Node position;
  138. private Node previous;
  139. private Node first;
  140. }

  1. package Question2;
  2. import java.awt.geom.Point2D;
  3. import java.util.ArrayList;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.geom.Line2D;
  8. import java.util.NoSuchElementException;
  9.  
  10. /**
  11.   A linked list is a sequence of nodes with efficient
  12.   element insertion and removal. This class
  13.   contains a subset of the methods of the standard
  14.   java.util.LinkedList class.
  15. */
  16. public class LinkedList
  17. {
  18. /**
  19.   Constructs an empty linked list.
  20.   */
  21. public LinkedList()
  22. {
  23. first = null;
  24.  
  25. }
  26.  
  27. /**
  28.   Returns the first element in the linked list.
  29.   @return the first element in the linked list
  30.   */
  31. public Object getFirst()
  32. {
  33. if (first == null)
  34. throw new NoSuchElementException();
  35. return first.data;
  36. }
  37.  
  38. /**
  39.   Removes the first element in the linked list.
  40.   @return the removed element
  41.   */
  42. public Object removeFirst()
  43. {
  44. if (first == null)
  45. throw new NoSuchElementException();
  46. Object element = first.data;
  47. first = first.next;
  48. return element;
  49.  
  50. }
  51.  
  52. /**
  53.   Adds an element to the front of the linked list.
  54.   @param element the element to add
  55.   */
  56. public void addfirst(Object element)
  57. {
  58. Node newNode = new Node();
  59. newNode.data = element;
  60. newNode.next = first;
  61. first = newNode;
  62. }
  63.  
  64. /**
  65.   Returns an iterator for iterating through this list.
  66.   @return an iterator for iterating through this list
  67.   */
  68. public ListIterator listIterator()
  69. {
  70. return new LinkedListIterator();
  71. }
  72.  
  73. public static void draw(Graphics2D g2)
  74. {
  75.  
  76. // TODO: Draw all nodes
  77. for (int i = 0; i < count; i++)
  78. {
  79.  
  80. Rectangle node = new Rectangle((int)(points.get(i).getX()), (int)(points.get(i).getY()), 30, 30);
  81. g2.draw(node);
  82. }
  83.  
  84. }
  85.  
  86.  
  87. public static int count = 0;
  88. public static ArrayList<Point2D.Double> points;
  89. private Node first;
  90.  
  91. private class Node
  92. {
  93.  
  94. public Node()
  95. {
  96. // TODO: Determine coordinates for this node
  97. if (LinkedList.count == 0)
  98. {
  99. LinkedList.points.add(new Point2D.Double(20, 20));
  100. LinkedList.count++;
  101. }
  102. else
  103. {
  104. LinkedList.points.add(new Point2D.Double(30 + points.get(LinkedList.count).getX(), 30 + points.get(LinkedList.count).getY()));
  105. LinkedList.count++;
  106. }
  107.  
  108. }
  109.  
  110.  
  111. public Object data;
  112. public Node next;
  113. // TODO: Instance fields for coordinates(Done)
  114.  
  115.  
  116. }
  117.  
  118. }

  1. package Question2;
  2.  
  3. /**
  4.   A list iterator allows access of a position in a linked list.
  5.   This interface contains a subset of the methods of the
  6.   standard java.util.ListIterator interface. The methods for
  7.   backward traversal are not included.
  8. */
  9. public interface ListIterator
  10. {
  11. /**
  12.   Moves the iterator past the next element.
  13.   @return the traversed element
  14.   */
  15. Object next();
  16.  
  17. /**
  18.   Tests if there is an element after the iterator
  19.   position.
  20.   @return true if there is an element after the iterator
  21.   position
  22.   */
  23. boolean hasNext();
  24.  
  25. /**
  26.   Adds an element before the iterator position
  27.   and moves the iterator past the inserted element.
  28.   @param element the element to add
  29.   */
  30. void add(Object element);
  31.  
  32. /**
  33.   Removes the last traversed element. This method may
  34.   only be called after a call to the next() method.
  35.   */
  36. void remove();
  37.  
  38. /**
  39.   Sets the last traversed element to a different
  40.   value.
  41.   @param element the element to set
  42.   */
  43. void set(Object element);
  44. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Graphical Linked List

 
0
  #2
Nov 16th, 2008
Hello LevelSix. I compress Your program to base size using java.util.LinkedList . It is functional. You can again try to grow it to use own LinkedListIterator,own LinkedList, own Node , function draw().

Some changes:
Graphics2D g2 = (Graphics2D) g.create(); in paintComponent(Graphics g)
public Dimension getPreferredSize() in LinkedListComponent
pack() in LinkedListViewer
important:
iterator = list.listIterator();
list.add(next++);

  1.  
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.util.Iterator;
  7. import java.util.LinkedList;
  8. import javax.swing.JComponent;
  9.  
  10. public class LinkedListComponent extends JComponent {
  11.  
  12. private static final int COMPONENT_WIDTH = 500;
  13. private static final int COMPONENT_HEIGHT = 350;
  14. private java.util.LinkedList<Integer> list;
  15. private Iterator iterator;
  16. private int next = 0;
  17. private static final int delta = 10;
  18.  
  19. public LinkedListComponent() {
  20. list = new java.util.LinkedList<Integer>();
  21. newIterator();
  22. }
  23.  
  24. // also use frame.pack(); in main(...)
  25. @Override
  26. public Dimension getPreferredSize() {
  27. return new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT);
  28. }
  29.  
  30. public void newIterator() {
  31. System.out.println("newIterator()");
  32. iterator = list.listIterator();
  33. //other iterators
  34. //iterator = list.descendingIterator();
  35. //int startIndex = 5;
  36. //iterator = list.listIterator(startIndex);
  37. repaint();
  38. }
  39.  
  40. public void next() {
  41. System.out.println("not impl.");
  42. }
  43.  
  44. /**
  45.   * add element to list
  46.   */
  47. public void add() {
  48. System.out.println("add()");
  49. list.add(next++);
  50. repaint();
  51. System.out.println("list.size()=" + list.size());
  52. }
  53.  
  54. /**
  55.   * remove element from list
  56.   */
  57. public void remove() {
  58. System.out.println("remove()");
  59. if (--next < 0) {
  60. next = 0;
  61. }
  62. System.out.println("next=" + next);
  63. if (list.isEmpty()) {
  64. return;
  65. }
  66. list.removeLast();
  67. repaint();
  68. }
  69.  
  70. @Override
  71. public void paintComponent(Graphics g) {
  72. Graphics2D g2 = (Graphics2D) g.create();
  73. g2.setColor(Color.black);
  74. g2.draw3DRect(0, 0, COMPONENT_WIDTH - 1, COMPONENT_HEIGHT - 1, false);
  75. int previous = -1;
  76. for (Integer inner : list) {
  77. //for(Iterator i$ = list.iterator(); i$.hasNext();) {
  78. //Integer inner = (Integer)i$.next();
  79. g2.draw3DRect(delta + inner * 30, delta, 20, 20, false);
  80. g2.drawString("" + inner, 5 + delta + inner * 30, 15 + delta);
  81. if (previous != -1) {
  82. //g2.draw(Shape s);
  83. g2.fill3DRect(20 + delta + previous * 30, delta + delta / 2, 10, 4, false);
  84. }
  85. previous = inner;
  86. }
  87. g2.dispose();
  88. }
  89. }
quuba
Attached Thumbnails
LinkedListFrame.gif  
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Graphical Linked List

 
0
  #3
Nov 17th, 2008
Is there a way to modify this code so that a elements that would go beyond the frame appear on a new line instead? And also so that the next button iterates the count inside the square, but does not affect their positioning?

I tried adding next++; to the next button, but doing so affects the positioning.

Also, what is the function of @Override?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Graphical Linked List

 
0
  #4
Nov 17th, 2008
@Override is an annotation in java 1.5 Tiger. In this case mean, that
public void paintComponent(Graphics g) is overrided. Look - LinkedListComponent extends JComponent , that original function paintComponent from JComponent is covered by my new definition. I'm user of NetBeans IDE. I strongly recomended to You work with NetBeans or Eclipse.
In paintComponent(Graphics g) we iterate throught list to display rectangles each time, when
JComponent needed refresh.
We need use an arrow to indicate where we are if we start manualy iterate step by step.
Iterator is an interface, and we can only ask i$.hasNext() and get value (Integer)i$.next().
Iterator NOT return info about size of list, or current index!
I try implement Next button
  1. //ver. with Next button impl.
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.util.Iterator;
  7. import java.util.LinkedList;
  8. import javax.swing.JComponent;
  9.  
  10. public class LinkedListComponent extends JComponent {
  11.  
  12. private static final int COMPONENT_WIDTH = 500;
  13. private static final int COMPONENT_HEIGHT = 350;
  14. private java.util.LinkedList<Integer> list;
  15. private Iterator iterator;
  16. private int next = 0;
  17. private static final int delta = 10;
  18.  
  19. public LinkedListComponent() {
  20. list = new java.util.LinkedList<Integer>();
  21. newIterator();
  22. }
  23.  
  24. // also use frame.pack(); in main(...)
  25. @Override
  26. public Dimension getPreferredSize() {
  27. return new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT);
  28. }
  29.  
  30. public void newIterator() {
  31. System.out.println("newIterator()");
  32. iterator = list.listIterator();
  33. //other iterators
  34. //iterator = list.descendingIterator();
  35. //int startIndex = 5;
  36. //iterator = list.listIterator(startIndex);
  37. repaint();
  38. }
  39. int valueReturnedByIterator = -1;
  40.  
  41. public void next() {
  42. if (iterator.hasNext()) {
  43. System.out.println("iterator.next()");
  44. valueReturnedByIterator = (Integer) iterator.next(); //Line:44
  45. }
  46. }
  47.  
  48. /**
  49.   * add element to list
  50.   */
  51. public void add() {
  52. System.out.println("add()");
  53. list.add(next++);
  54. repaint();
  55. System.out.println("list.size()=" + list.size());
  56. }
  57.  
  58. /**
  59.   * remove element from list
  60.   */
  61. public void remove() {
  62. System.out.println("remove()");
  63. if (--next < 0) {
  64. next = 0;
  65. }
  66. System.out.println("next=" + next);
  67. if (list.isEmpty()) {
  68. return;
  69. }
  70. list.removeLast();
  71. repaint();
  72. }
  73.  
  74. @Override
  75. public void paintComponent(Graphics g) {
  76. Graphics2D g2 = (Graphics2D) g.create();
  77. g2.setColor(Color.black);
  78. g2.draw3DRect(0, 0, COMPONENT_WIDTH - 1, COMPONENT_HEIGHT - 1, false);
  79. int previous = -1;
  80. for (Integer inner : list) {
  81. //for(Iterator i$ = list.iterator(); i$.hasNext();) {
  82. //Integer inner = (Integer)i$.next();
  83. g2.draw3DRect(delta + inner * 30, delta, 20, 20, false);
  84. g2.drawString("" + inner, 5 + delta + inner * 30, 15 + delta);
  85. if (previous != -1) {
  86. //g2.draw(Shape s);
  87. g2.fill3DRect(20 + delta + previous * 30, delta + delta / 2, 10, 4, false);
  88. }
  89. previous = inner;
  90. ///
  91. if (valueReturnedByIterator != -1) {
  92. g2.fill3DRect(15 + delta + valueReturnedByIterator * 30, delta + delta / 2, 10, 20, false);
  93. }
  94. }
  95. g2.dispose();
  96. }
  97. }

but...
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)
What is Your conclusion?
quuba
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC