| | |
java rubberbanding
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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.
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)
•
•
Join Date: Mar 2009
Posts: 18
Reputation:
Solved Threads: 0
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 : )
Java Syntax (Toggle Plain Text)
import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.util.ArrayList; public class RubberLinesPanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private Point point1 = null, point2 = null; private ArrayList<Line2D.Double> lines; //----------------------------------------------------------------- // Constructor: Sets up this panel to listen for mouse events. //----------------------------------------------------------------- public RubberLinesPanel() { lines = new ArrayList<Line2D.Double>(); LineListener listener = new LineListener(); addMouseListener (listener); addMouseMotionListener (listener); setBackground (Color.black); setPreferredSize (new Dimension(400, 200)); } //----------------------------------------------------------------- // Draws the current line from the intial mouse-pressed point to // the current position of the mouse. //----------------------------------------------------------------- public void paintComponent (Graphics page) { super.paintComponent (page); page.setColor (Color.yellow); for(int i=0;i<lines.size();i++) { if(lines.get(i) != null) { page.drawLine((int)lines.get(i).getX1(),(int)lines.get(i).getY1(), ( int)lines.get(i).getX2(),(int)lines.get(i).getY2()); } } } //***************************************************************** // Represents the listener for all mouse events. //***************************************************************** private class LineListener implements MouseListener, MouseMotionListener { //-------------------------------------------------------------- // Captures the initial position at which the mouse button is // pressed. //-------------------------------------------------------------- public void mousePressed (MouseEvent event) { point1 = event.getPoint(); } //-------------------------------------------------------------- // Gets the current position of the mouse as it is dragged and // redraws the line to create the rubberband effect. //-------------------------------------------------------------- public void mouseDragged (MouseEvent event) { point2 = event.getPoint(); Line2D.Double newLine = new Line2D.Double(point1,point2); lines.add(newLine); repaint(); } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event){} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} } } import javax.swing.JFrame; public class RubberLines { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Rubber Lines"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new RubberLinesPanel()); frame.pack(); frame.setVisible(true); } }
•
•
Join Date: Mar 2009
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
You're adding a new line for every mouseDragged() event. You only want to add a line when the mouse is released.
Java Syntax (Toggle Plain Text)
public void mouseDragged (MouseEvent event) { } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) { point2 = event.getPoint(); Line2D.Double newLine = new Line2D.Double(point1,point2); lines.add(newLine); repaint(); } public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} } }
Last edited by YingKang; 34 Days Ago at 7:05 pm.
•
•
Join Date: Mar 2009
Posts: 18
Reputation:
Solved Threads: 0
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 : )
Java Syntax (Toggle Plain Text)
import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.util.ArrayList; public class RubberLinesPanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private Point point1 = null, point2 = null; private ArrayList<Line2D.Double> lines; //----------------------------------------------------------------- // Constructor: Sets up this panel to listen for mouse events. //----------------------------------------------------------------- public RubberLinesPanel() { lines = new ArrayList<Line2D.Double>(); LineListener listener = new LineListener(); addMouseListener (listener); addMouseMotionListener (listener); setBackground (Color.black); setPreferredSize (new Dimension(400, 200)); } //----------------------------------------------------------------- // Draws the current line from the intial mouse-pressed point to // the current position of the mouse. //----------------------------------------------------------------- public void paintComponent (Graphics page) { super.paintComponent (page); page.setColor (Color.yellow); for(int i=0;i<lines.size();i++) { if(lines.get(i) != null) { page.drawLine((int)lines.get(i).getX1(),(int)lines.get(i).getY1(), (int)lines.get(i).getX2(),(int)lines.get(i).getY2()); } } } //***************************************************************** // Represents the listener for all mouse events. //***************************************************************** private class LineListener implements MouseListener, MouseMotionListener { //-------------------------------------------------------------- // Captures the initial position at which the mouse button is // pressed. //-------------------------------------------------------------- public void mousePressed (MouseEvent event) { point1 = event.getPoint(); } //-------------------------------------------------------------- // Gets the current position of the mouse as it is dragged and // redraws the line to create the rubberband effect. //-------------------------------------------------------------- public void mouseDragged (MouseEvent event) { point2 = event.getPoint(); Line2D.Double newLine = new Line2D.Double(point1,point2); lines.add(newLine); point1 = point2; repaint(); } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} } }
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:

I thought you wanted to be able to draw multiple lines by clicking and dragging to stretch the line, like this:
Java Syntax (Toggle Plain Text)
import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.util.ArrayList; import javax.swing.JFrame; public class RubberLinesPanel extends JPanel { private static final long serialVersionUID = 1L; private Point point1 = null, point2 = null; private ArrayList<Line2D.Double> lines; //----------------------------------------------------------------- // Constructor: Sets up this panel to listen for mouse events. //----------------------------------------------------------------- public RubberLinesPanel() { lines = new ArrayList<Line2D.Double>(); LineListener listener = new LineListener(); addMouseListener(listener); addMouseMotionListener(listener); setBackground(Color.black); setPreferredSize(new Dimension(400, 200)); } //----------------------------------------------------------------- // Draws the current line from the intial mouse-pressed point to // the current position of the mouse. //----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent(page); if (point2 != null) { // draw the temporary "rubberband" line page.setColor(Color.GREEN); page.drawLine(point1.x, point1.y, point2.x, point2.y); } // draw all of the completed lines page.setColor(Color.yellow); for (int i = 0; i < lines.size(); i++) { if (lines.get(i) != null) { page.drawLine((int)lines.get(i).getX1(), (int)lines.get(i).getY1(), (int)lines.get(i).getX2(), (int)lines.get(i).getY2()); } } } //***************************************************************** // Represents the listener for all mouse events. //***************************************************************** private class LineListener implements MouseListener, MouseMotionListener { //-------------------------------------------------------------- // Captures the initial position at which the mouse button is // pressed. //-------------------------------------------------------------- public void mousePressed(MouseEvent event) { point1 = event.getPoint(); } //-------------------------------------------------------------- // Gets the current position of the mouse as it is dragged and // redraws the line to create the rubberband effect. //-------------------------------------------------------------- public void mouseDragged(MouseEvent event) { point2 = event.getPoint(); repaint(); } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseClicked(MouseEvent event) { } public void mouseReleased(MouseEvent event) { // add the line to the collection to be drawn Line2D.Double newLine = new Line2D.Double(point1, point2); lines.add(newLine); point2 = null; repaint(); } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } public void mouseMoved(MouseEvent event) { } } //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Rubber Lines"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new RubberLinesPanel()); frame.pack(); frame.setVisible(true); } }
![]() |
Similar Threads
- FT Junior Java Developer Needed for Major Media firm in NYC (Software Development Job Offers)
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Front-end Java Software Engineer for Digital Video/Media Market Leader (Software Development Job Offers)
- Senior Software Engineer (Java) (Web Development Job Offers)
- Java Software Engineer (Software Development Job Offers)
- Java Front-end Developer Engineer for Stealth Media Start-up (Software Development Job Offers)
- Java Developer Required (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: Java msql INSERT is inserting NULL
- Next Thread: [JAVA}I am having problem with making Quiz program
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component coordinates database defaultmethod development dice dragging ebook eclipse educational error exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite ingres input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree websites windows






