java rubberbanding

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

Join Date: Mar 2009
Posts: 18
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

Error while running

 
0
  #11
34 Days Ago
Originally Posted by Ezzaral View Post
I think you are getting more detailed information than "error message". I can guess what it is, but actually posting those error messages would be helpful. They are usually very specific in telling you the problem.

edit: posted while you were posting this most recent version.
there is not error while I compile the last version of my code I posted here, but as soon as I run and start to draw, messages in console is the following:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at RubberLinesPanel.paintComponent(RubberLinesPanel.java:43)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,477
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #12
34 Days Ago
So examine your loop and the code in it and try to figure out how it could access an index that is too high for your collection.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 18
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

it looks not right

 
0
  #13
34 Days Ago
I revised my code again. it compiles and runs, but when i draw a single line, it looks like mulitiple lines together. what the pronblem is? can anyone help please ? thank you : )
  1. import javax.swing.JPanel;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.geom.Line2D;
  5. import java.util.ArrayList;
  6. public class RubberLinesPanel extends JPanel
  7. {
  8. /**
  9.   *
  10.   */
  11. private static final long serialVersionUID = 1L;
  12. private Point point1 = null, point2 = null;
  13. private ArrayList<Line2D.Double> lines;
  14.  
  15. //-----------------------------------------------------------------
  16. // Constructor: Sets up this panel to listen for mouse events.
  17. //-----------------------------------------------------------------
  18. public RubberLinesPanel()
  19. {
  20. lines = new ArrayList<Line2D.Double>();
  21.  
  22.  
  23. LineListener listener = new LineListener();
  24. addMouseListener (listener);
  25. addMouseMotionListener (listener);
  26.  
  27. setBackground (Color.black);
  28. setPreferredSize (new Dimension(400, 200));
  29. }
  30.  
  31. //-----------------------------------------------------------------
  32. // Draws the current line from the intial mouse-pressed point to
  33. // the current position of the mouse.
  34. //-----------------------------------------------------------------
  35. public void paintComponent (Graphics page)
  36. {
  37. super.paintComponent (page);
  38.  
  39. page.setColor (Color.yellow);
  40.  
  41. for(int i=0;i<lines.size();i++)
  42. {
  43. if(lines.get(i) != null)
  44. {
  45. page.drawLine((int)lines.get(i).getX1(),(int)lines.get(i).getY1(),
  46. ( int)lines.get(i).getX2(),(int)lines.get(i).getY2());
  47. }
  48. }
  49.  
  50.  
  51. }
  52.  
  53. //*****************************************************************
  54. // Represents the listener for all mouse events.
  55. //*****************************************************************
  56. private class LineListener implements MouseListener,
  57. MouseMotionListener
  58. {
  59. //--------------------------------------------------------------
  60. // Captures the initial position at which the mouse button is
  61. // pressed.
  62. //--------------------------------------------------------------
  63. public void mousePressed (MouseEvent event)
  64. {
  65. point1 = event.getPoint();
  66.  
  67. }
  68.  
  69. //--------------------------------------------------------------
  70. // Gets the current position of the mouse as it is dragged and
  71. // redraws the line to create the rubberband effect.
  72. //--------------------------------------------------------------
  73. public void mouseDragged (MouseEvent event)
  74. {
  75. point2 = event.getPoint();
  76.  
  77.  
  78. Line2D.Double newLine = new Line2D.Double(point1,point2);
  79. lines.add(newLine);
  80.  
  81. repaint();
  82.  
  83. }
  84.  
  85. //--------------------------------------------------------------
  86. // Provide empty definitions for unused event methods.
  87. //--------------------------------------------------------------
  88. public void mouseClicked (MouseEvent event) {}
  89. public void mouseReleased (MouseEvent event){}
  90. public void mouseEntered (MouseEvent event) {}
  91. public void mouseExited (MouseEvent event) {}
  92. public void mouseMoved (MouseEvent event) {}
  93. }
  94. }
  95. import javax.swing.JFrame;
  96.  
  97. public class RubberLines
  98. {
  99. //-----------------------------------------------------------------
  100. // Creates and displays the application frame.
  101. //-----------------------------------------------------------------
  102. public static void main (String[] args)
  103. {
  104. JFrame frame = new JFrame ("Rubber Lines");
  105. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  106.  
  107. frame.getContentPane().add (new RubberLinesPanel());
  108.  
  109. frame.pack();
  110. frame.setVisible(true);
  111. }
  112. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,477
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #14
34 Days Ago
You're adding a new line for every mouseDragged() event. You only want to add a line when the mouse is released.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #15
34 Days Ago
Well, what is the size of your array when it draws two lines next to each other? Is it one, or two? Do some debugging. If the array's size is two, then no wonder it looks like that. If its one, my theory is kaput, but have you even made this simple check?
Out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #16
34 Days Ago
Oops, posted at the same time as you Ezzaral.
Out.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 18
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

it draws lines but loses "rubber banding" effect

 
0
  #17
34 Days Ago
Originally Posted by Ezzaral View Post
You're adding a new line for every mouseDragged() event. You only want to add a line when the mouse is released.
I moved the code to mouseReleased from mousedragged,. it seems to run but lose its "rubber banding "effect, which is this program supposed to do. is there any way to keep the "rubber banding"effect while drawing the line? thanks
  1. public void mouseDragged (MouseEvent event)
  2. {
  3.  
  4. }
  5.  
  6. //--------------------------------------------------------------
  7. // Provide empty definitions for unused event methods.
  8. //--------------------------------------------------------------
  9. public void mouseClicked (MouseEvent event) {}
  10. public void mouseReleased (MouseEvent event)
  11. {
  12. point2 = event.getPoint();
  13.  
  14.  
  15. Line2D.Double newLine = new Line2D.Double(point1,point2);
  16. lines.add(newLine);
  17.  
  18. repaint();
  19. }
  20. public void mouseEntered (MouseEvent event) {}
  21. public void mouseExited (MouseEvent event) {}
  22. public void mouseMoved (MouseEvent event) {}
  23. }
  24. }
Last edited by YingKang; 34 Days Ago at 7:05 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,477
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
1
  #18
34 Days Ago
Keep the point2 assignment and a repaint() in mouseDragged() . Leave mouseReleased() as you have it, but also set point2 to null. Then alter your paintComponent() method to draw a line from point1 to point2 if point2 is not null.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 18
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster
 
0
  #19
34 Days Ago
thanks for all the help. I revised my code again. it works fine now, with the help from the nice people here and a friend of mine. what I did is I added "point1 = point2" in mouseDragged class. thanks again to everyone who left message here. thank you Ezzaral : )
  1. import javax.swing.JPanel;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.geom.Line2D;
  5. import java.util.ArrayList;
  6. public class RubberLinesPanel extends JPanel
  7. {
  8. /**
  9.   *
  10.   */
  11. private static final long serialVersionUID = 1L;
  12. private Point point1 = null, point2 = null;
  13. private ArrayList<Line2D.Double> lines;
  14.  
  15. //-----------------------------------------------------------------
  16. // Constructor: Sets up this panel to listen for mouse events.
  17. //-----------------------------------------------------------------
  18. public RubberLinesPanel()
  19. {
  20. lines = new ArrayList<Line2D.Double>();
  21.  
  22. LineListener listener = new LineListener();
  23. addMouseListener (listener);
  24. addMouseMotionListener (listener);
  25.  
  26. setBackground (Color.black);
  27. setPreferredSize (new Dimension(400, 200));
  28. }
  29.  
  30. //-----------------------------------------------------------------
  31. // Draws the current line from the intial mouse-pressed point to
  32. // the current position of the mouse.
  33. //-----------------------------------------------------------------
  34. public void paintComponent (Graphics page)
  35. {
  36. super.paintComponent (page);
  37.  
  38. page.setColor (Color.yellow);
  39.  
  40. for(int i=0;i<lines.size();i++)
  41. {
  42. if(lines.get(i) != null)
  43. {
  44. page.drawLine((int)lines.get(i).getX1(),(int)lines.get(i).getY1(),
  45. (int)lines.get(i).getX2(),(int)lines.get(i).getY2());
  46. }
  47. }
  48.  
  49.  
  50. }
  51.  
  52. //*****************************************************************
  53. // Represents the listener for all mouse events.
  54. //*****************************************************************
  55. private class LineListener implements MouseListener,
  56. MouseMotionListener
  57. {
  58. //--------------------------------------------------------------
  59. // Captures the initial position at which the mouse button is
  60. // pressed.
  61. //--------------------------------------------------------------
  62. public void mousePressed (MouseEvent event)
  63. {
  64. point1 = event.getPoint();
  65.  
  66. }
  67.  
  68. //--------------------------------------------------------------
  69. // Gets the current position of the mouse as it is dragged and
  70. // redraws the line to create the rubberband effect.
  71. //--------------------------------------------------------------
  72. public void mouseDragged (MouseEvent event)
  73. {
  74. point2 = event.getPoint();
  75.  
  76.  
  77. Line2D.Double newLine = new Line2D.Double(point1,point2);
  78. lines.add(newLine);
  79. point1 = point2;
  80. repaint();
  81. }
  82.  
  83. //--------------------------------------------------------------
  84. // Provide empty definitions for unused event methods.
  85. //--------------------------------------------------------------
  86. public void mouseClicked (MouseEvent event) {}
  87. public void mouseReleased (MouseEvent event) {}
  88. public void mouseEntered (MouseEvent event) {}
  89. public void mouseExited (MouseEvent event) {}
  90. public void mouseMoved (MouseEvent event) {}
  91. }
  92. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,477
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #20
34 Days Ago
Ok, that isn't exactly what I thought you wanted to do, but if it works for you, great
I thought you wanted to be able to draw multiple lines by clicking and dragging to stretch the line, like this:
  1. import javax.swing.JPanel;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.geom.Line2D;
  5. import java.util.ArrayList;
  6. import javax.swing.JFrame;
  7.  
  8. public class RubberLinesPanel extends JPanel {
  9.  
  10. private static final long serialVersionUID = 1L;
  11. private Point point1 = null, point2 = null;
  12. private ArrayList<Line2D.Double> lines;
  13.  
  14. //-----------------------------------------------------------------
  15. // Constructor: Sets up this panel to listen for mouse events.
  16. //-----------------------------------------------------------------
  17. public RubberLinesPanel() {
  18. lines = new ArrayList<Line2D.Double>();
  19.  
  20. LineListener listener = new LineListener();
  21. addMouseListener(listener);
  22. addMouseMotionListener(listener);
  23.  
  24. setBackground(Color.black);
  25. setPreferredSize(new Dimension(400, 200));
  26. }
  27.  
  28. //-----------------------------------------------------------------
  29. // Draws the current line from the intial mouse-pressed point to
  30. // the current position of the mouse.
  31. //-----------------------------------------------------------------
  32. public void paintComponent(Graphics page) {
  33. super.paintComponent(page);
  34.  
  35. if (point2 != null) {
  36. // draw the temporary "rubberband" line
  37. page.setColor(Color.GREEN);
  38. page.drawLine(point1.x, point1.y, point2.x, point2.y);
  39. }
  40.  
  41. // draw all of the completed lines
  42. page.setColor(Color.yellow);
  43. for (int i = 0; i < lines.size(); i++) {
  44. if (lines.get(i) != null) {
  45. page.drawLine((int)lines.get(i).getX1(), (int)lines.get(i).getY1(),
  46. (int)lines.get(i).getX2(), (int)lines.get(i).getY2());
  47. }
  48. }
  49. }
  50.  
  51. //*****************************************************************
  52. // Represents the listener for all mouse events.
  53. //*****************************************************************
  54. private class LineListener implements MouseListener,
  55. MouseMotionListener {
  56. //--------------------------------------------------------------
  57. // Captures the initial position at which the mouse button is
  58. // pressed.
  59. //--------------------------------------------------------------
  60. public void mousePressed(MouseEvent event) {
  61. point1 = event.getPoint();
  62. }
  63.  
  64. //--------------------------------------------------------------
  65. // Gets the current position of the mouse as it is dragged and
  66. // redraws the line to create the rubberband effect.
  67. //--------------------------------------------------------------
  68. public void mouseDragged(MouseEvent event) {
  69. point2 = event.getPoint();
  70. repaint();
  71. }
  72.  
  73. //--------------------------------------------------------------
  74. // Provide empty definitions for unused event methods.
  75. //--------------------------------------------------------------
  76. public void mouseClicked(MouseEvent event) {
  77. }
  78.  
  79. public void mouseReleased(MouseEvent event) {
  80. // add the line to the collection to be drawn
  81. Line2D.Double newLine = new Line2D.Double(point1, point2);
  82. lines.add(newLine);
  83. point2 = null;
  84. repaint();
  85. }
  86.  
  87. public void mouseEntered(MouseEvent event) {
  88. }
  89.  
  90. public void mouseExited(MouseEvent event) {
  91. }
  92.  
  93. public void mouseMoved(MouseEvent event) {
  94. }
  95. }
  96.  
  97.  
  98. //-----------------------------------------------------------------
  99. // Creates and displays the application frame.
  100. //-----------------------------------------------------------------
  101. public static void main (String[] args)
  102. {
  103. JFrame frame = new JFrame ("Rubber Lines");
  104. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  105.  
  106. frame.getContentPane().add (new RubberLinesPanel());
  107.  
  108. frame.pack();
  109. frame.setVisible(true);
  110. }
  111. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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