Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Random image move

In my applet i want to randomly generate and move the images. I also
want to store current positions of all the images moving around. it should
move in upwards directions and in a particulare region. Please help
me out with the code.
  • 2
1 Solution
Hateli, could you state the question more clearly? In particular, I'm wondering:

- What kind of images are you generating?
- How often do you want to generate and move them? Every second? In response to a mouse click or keypress?
- What do you mean by "It should move in upwards directions and in a particular region"?
hateliAuthor Commented:
Hi hoju ,

I have one image of of balloon.gif. i want to randomly
move the ballons from down to up . my applet is of 500,
300 size. now balloons should be randomly flying only in
area of right hand corner of applet that is say 200 pixels
from right corner. it should start as soon as applet
gets started. if i press a space bar stone flys from
down to up and if it hits a ballon i get 100 pts.

I hope thsi is clear to you

Anticipating a reply

Sounds like you need a separate thread for drawing the balloon.
Here's what you can try to do.

1. Create a class called Balloon, which extends Canvas and in its paint() method draws the ballon.gif in itself.

2. Create a class called BalloonMove, which extends Thread.  This one should have
variables x and y for current location and an object of type Balloon.

3. Provide a mechanism to specify to an object of the class BalloonMove where to draw the balloon (e.g. put a method in it, which would accept Applet a as a parameter and store it in some private object variable)

4. In BalloonMove.start() method, add the balloon object to the applet and initialize the initial coordinates x and y to something using Math.random() for x and applet's height
for y.

5. In BalloonMove.run() method, decrement the y coordinate by whatever you want the balloon to move, then use setLocation() method of the ballon object to set the new location of the balloon.

6. Add a method getCoordinates() to the BallonMove class, which would return, for example, a Point object with the current x and y coordinates.

7. In the applet's start method(), put the code to create an object of type BalloonMove, tell it what the applet is (see 3), and start it.  In the applet's run() method, you can use BalloonMove's method getCoordinates() to determine the current position of the balloon.  Do not forget to stop the BalloonMove thread in the applet's stop() method.

Good luck.
hateliAuthor Commented:
Please could U pass on the code ! it would be very much halpful
Thankx a lot
Anticipating a Reply ASAP

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now