File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Moon Lander Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Moon Lander" Watch "Moon Lander" New topic
Author

Moon Lander

Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
I am completly stupid about this stuff. I am suppose to write a program that uses threads and animation. I was wondering if there is anyone out there that would not mind helping me with this. Like I said, I am very dumb when it comes to this stuff and need constant supervision, haha. If there is anyone out there willing to spend some time with me on this, would you please reply to this.
Stephen
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
here is my assignment, would someone let me know if they understand this at all, because i don't.

One of the most common application of using Threads (Runnable interface in particular) is in Animating Images. In this assignment you will be designing a "simple" moon lander application. (Note that this assignment is more challenging than the previous ones, so start working on it ASAP).
Your application will animate the moon lander ascending and descending. You will be using a customized JPanel class to draw the moon lander image. To implement your JPanel as a thread you need to implement it as a Runnable interface and put the code for the animation in the run method. You will need to make a thread object to launch the thread. Give that thread a reference to the Runnable object in its constructor. The thread then calls the run method of that object, when you call the thread object's start method. This call can be made in the startAnimation method of the JPanel, as shown below (Note the difference between this and the example in Fig 15.17 on page 872 (4th Ed.) or page 817 (5th Ed.)):

class ImagePanel extends JPanel implements Runnable
{
private Thread runner;
....
public void startAnimation()
{
if ( runner == null ) {
runner = new Thread(this);
}
runner.start();
}
....
public void stopAnimation()
{
if ( runner != null )
runner = null;
}
....
}
In this case, the this argument to the Thread constructor specifies that the object whose run method should be called when the thread executes is an instance of the ImagePanel object.
Inside the run method of your ImagePanel class you should have a while loop that keeps running as long as the runner thread object is not null, as shown below:

public void run()
{
while ( runner != null )
{
// give your code here
}
}
Using the while loop as shown above provides a very simple mechanism to stop the current thread. All you need to do to stop the current thread is to set the thread variable runner to null anywhere in your JPanel, as shown in the stopAnimation method above. You can restart a new thread by calling the startAnimation method of your JPanel class, which is shown above. This is a common idiom that you will find in many multithreaded applications.
The Moon Lander Application:
In this assignment you need to animate moon lander images. There are two images provided below. You will be displaying the lander.jpg image when the lander is descending and displaying the landerFire.jpg image when the lander is ascending. To load the images in your JPanel you need to do the following in its constructor:
Image lander = Toolkit.getDefaultToolkit().getImage( "lander.jpg" );
Image landerFire = Toolkit.getDefaultToolkit().getImage( "landerfire.jpg" );

To display the lander image in your JPanel, do the following in the paintComponent method of your JPanel class:
g.drawImage( lander, x, y, this );

where, x, y are the upper left hand corner coordinates of the JPanel where the image will be displayed. To simulate the ascent or desent of the lander all you need to do is change the y coordinate appropriately inside the run method of your JPanel and call repaint(). Make sure you put the current running thread to sleep for about 0.1 sec. inside the while loop of the run method so that the moon lander's motion is slow and visible.
You can use the getHeight() and getWidth() methods of the JPanel to find out its dimensions so that you can appropriately draw the lander. You can use a boolean variable to track whether the lander is ascending or descending. Once it reaches the top or bottom edge of the JPanel you can toggle the value of that variable. Make sure to draw the lander image when descending and the landerFire image when ascending.

Deliverables:
You should submit the appropriate .java files in a zipped directory.
Animation.class
ImagePanel.class
lander.jpg (Right click on the image to save it)
landerfire.jpg
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moon Lander
 
Similar Threads
struts 1.beginner, code not working
include directive question in java black belt
Reading of pdf file as stream
Getting the priog assignment
help on ejb with wl5.1