Solved

Random image move

Posted on 1998-07-14
4
186 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running Jira on Raspberry PI 2? 3 261
solarwind tftp server 2 44
Securing Jmx Console and web console 2 64
thymeleaf natural templating vs JSP 2 68
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

910 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

17 Experts available now in Live!

Get 1:1 Help Now