Solved

Random image move

Posted on 1998-07-14
4
188 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
ID: 1226048
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
ID: 1226049
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
ID: 1226050
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
ID: 1226051
Please could U pass on the code ! it would be very much halpful
Thankx a lot
Anticipating a Reply ASAP
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 41
eclipse apache tomcat admin console 52 124
How do I remove an object from a 3 40
Java Eclipse Loop 3 21
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

821 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