Solved

Random image move

Posted on 1998-07-14
4
185 Views
Last Modified: 2010-03-30
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.
0
Comment
Question by:hateli
  • 2
4 Comments
 

Expert Comment

by:HoJu
Comment Utility
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"?
0
 

Author Comment

by:hateli
Comment Utility
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

0
 
LVL 2

Accepted Solution

by:
shchuka earned 100 total points
Comment Utility
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.
0
 

Author Comment

by:hateli
Comment Utility
Please could U pass on the code ! it would be very much halpful
Thankx a lot
Anticipating a Reply ASAP
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now