Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make rectangle move in a random direction

 
Angel Brown
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to learn Java swing and I wanted to draw a rectangle that will move in a random direction but when it collides with a wall it should change a direction of its movement.
So I have this code:

but it stops after the first time when it is done and it moves so quickly that I cannot see the movement.
I have another code it works perfectly fine I just don't understand why the first code doesn't work when I used the same logic but tried to do it more complicated
here is my second code

Can someone help thanks in advance
 
Everybody's invited. Except this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!