-1

The thing that I need is: do X++ and Y++ of an JButton until "x++ == Z and y++ == W" resulting into an movement of the button...

as far as ive got, first i need to get the coords of the button:

final int locXbtnTrapMe = igu.btnTrapMe.getX();
final int locYbtnTrapMe = igu.btnTrapMe.getY();

then specify the "Z" and "W" which are random limits as next:

int widthTrapMe = igu.btnTrapMe.getWidth();
int heightTrapMe = igu.btnTrapMe.getHeight();           
final int randX = random.nextInt(widthMainPanel-widthTrapMe);
final int randY = random.nextInt(heightMainPanel-heightTrapMe);

i specified those randoms like that so that the button doesnt go out of the main pannel... (thing i want)

Now the question: How can i do a ++ every 200ms for both locXbtnTrapMe and locYbtnTrapMe until the randX and randY get reached?

I need a smooth movement of the button from a point to another, not jumps...

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Alpha2k
  • 2,212
  • 7
  • 38
  • 65

1 Answers1

1

Now the question: How can i do a ++ every 200ms for both locXbtnTrapMe and locYbtnTrapMe until the randX and randY get reached?

Use a Swing Timer, and in the Timer's ActionListener's actionPerformed method, test the state of your watched fields, and increment them if below the maximum. Then make sure to call stop() on your Timer when the maximum has been reached.

Some decent links to help you out:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373