0

I want an image to move towards the mouse when the mouse moves towards it, and that image should move with the same speed the mouse is moving with, and when the mouse moves away from the image, the image moves in the opposite direction with the same mouse speed until the image reaches its origin point. And as for the neighbouring images they should do the same thing but with half the speed. Any idea how to do so ? i tried doing it but the images jump instead of moving.

Sherif
  • 47
  • 2
  • 8
  • 1
    Use a Swing `Timer`, for [example](http://stackoverflow.com/a/3078354/230513). – trashgod Feb 28 '16 at 13:50
  • @trashgod tried it and didnt work, because "linex" and "liney" are fixed values with a certain direction. – Sherif Feb 28 '16 at 13:59
  • 1
    *"because "linex" and "liney" are fixed values with a certain direction."* That does not make much sense to me. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) of code with an attempt at using `Timer`. – Andrew Thompson Feb 28 '16 at 14:02
  • @AndrewThompson if "linex" and "liney" increase for example, x/y = 0 then 1 then 2 then 3 and so on till10 then using a timer would generate an animation, but if x and y are equal to 10 from the start then using a timer wont do anything. – Sherif Feb 28 '16 at 14:20
  • @AndrewThompson Done – Sherif Feb 28 '16 at 18:27
  • 1
    Create your `Timer` _outside_ `paintComponent()`, and `start()` it when everything's set up, for [example](http://stackoverflow.com/a/11233735/230513). – trashgod Feb 29 '16 at 00:12
  • @trashgod created it outside and started it and still same result – Sherif Feb 29 '16 at 11:37
  • @Sherif: Please edit your question to include a [mcve] that shows your current approach. – trashgod Feb 29 '16 at 11:48
  • @trashgod the code above is the MCVE, i posted it yesterday – Sherif Feb 29 '16 at 11:55
  • Your `ActionListener` is `null`, so your `Timer` will do nothing. – trashgod Feb 29 '16 at 12:16
  • @trashgod implemented action listener and changed timer, still same result – Sherif Feb 29 '16 at 12:24
  • An empty method will also do nothing. – trashgod Feb 29 '16 at 12:41
  • @trashgod placed it in paintcomponent didnt work, put it in mouse moved didnt work, the problem i think has nothing to do with the timer, its with the values used to draw the rectangle. i dont know how to change them properly – Sherif Feb 29 '16 at 12:51
  • @Sherif: Your requirement is sufficiently complex to warrant a separate model, a shown [here](http://stackoverflow.com/a/11233735/230513). – trashgod Feb 29 '16 at 13:31

0 Answers0