Results 1 to 3 of 3
Thread: MouseMotionListener
- 04-13-2011, 08:17 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
MouseMotionListener
Hi,
I am writting a simple graphics editor and I am having a trouble with
mouseMotionListener speed performance. I write a simple class to demonstrate the issue. It basically just counts the Euklidiean distance between two registered points measured in pixels while the mouse is dragged, stores it in set and when mouse is released, it prints the result. I think it really should not slow down the listener though the distance between two points can be, if you drag the mouse a bit fast, more than 50 pixels. The actual trouble I have in my graphic editor, which is caused by this, looks like this.
http://www.imagebanana.com/view/6eh87b2t/obr.png
Thanks a lot
CODE
Java Code:public class ZkouskaMouse extends JPanel { public ZkouskaMouse() { setPreferredSize(new Dimension(600, 600)); MyAdapter adapter = new MyAdapter(); addMouseMotionListener(adapter); addMouseListener(adapter); } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("TEST"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new ZkouskaMouse(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setLocation(new Point(100, 0)); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } class MyAdapter extends MouseAdapter { Point pocBod = new Point(); Point pom1; boolean first = false; Set<Double> set = new HashSet<Double>(); @Override public void mouseDragged(MouseEvent e) { if (first) { pom1 = new Point(pocBod.x, pocBod.y); first = false; } double a = Math.sqrt(Math.pow(e.getPoint().x - pom1.x, 2) + Math.pow(e.getPoint().y - pom1.y, 2)); set.add(a); pom1 = e.getPoint(); } @Override public void mousePressed(MouseEvent e) { pocBod = e.getPoint(); first = true; } @Override public void mouseReleased(MouseEvent e) { for (Double double1 : set) { System.out.println(double1); } set.clear(); System.out.println(""); } } }Last edited by hubikmar; 04-13-2011 at 08:41 PM.
- 04-14-2011, 02:29 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
MouseEvents are NOT generated for every pixel when the mouse is moved. There fore you need to interpolate between points or use the drawLine(...) method to draw a line between the two points.
- 04-15-2011, 11:35 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Thank you a lot.The drawLine works great. Here is how it looks now.
obr2.png - Bilder und Fotos kostenlos auf ImageBanana hochladen
Similar Threads
-
using the mousemotionlistener and mouselistener
By ravirajparab in forum New To JavaReplies: 1Last Post: 12-21-2009, 07:11 PM -
MouseMotionListener 'scrolling'
By Thez in forum Java 2DReplies: 3Last Post: 03-12-2009, 11:48 AM -
Demonstrating the MouseListener and MouseMotionListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks