Results 1 to 3 of 3
  1. #1
    hubikmar is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    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.

  3. #3
    hubikmar is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. using the mousemotionlistener and mouselistener
    By ravirajparab in forum New To Java
    Replies: 1
    Last Post: 12-21-2009, 07:11 PM
  2. MouseMotionListener 'scrolling'
    By Thez in forum Java 2D
    Replies: 3
    Last Post: 03-12-2009, 11:48 AM
  3. Replies: 0
    Last Post: 04-23-2008, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •