Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Moon Lander Application: In this assignment you need to animate moon lander

ID: 3761720 • Letter: T

Question

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:

To display the lander image in your JPanel, do the following in the paintComponent method of your JPanel class:

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 along with the two image files as a single zipped file. You SHOULD name the JPanel class as ImagePanel and your application class as HW3.

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.util.*;
class ImagePanel extends JPanel implements Runnable
{
boolean isgoingdown = false;
int x = 200, y = 290, side = 100;
int previous = 289;
Thread runner;
Random rand = new Random();
Image lander = Toolkit.getDefaultToolkit().getImage( "lander.jpg" );
Image landerfire = Toolkit.getDefaultToolkit().getImage( "landerfire.jpg" );
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.drawImage( landerfire, x, y, this );
}
public void run()
{
while ( runner != null )
{
while(isgoingdown != true && y != 0 && y != 290)
{
repaint();
side = 50 + rand.nextInt( 200 );
y = y - 1;
try
{
Thread.sleep( 3 );
}
catch ( Exception e ) {}
}
while(isgoingdown && y != 0 && y != 290)
{
repaint();
side = 50 + rand.nextInt( 200 );
y = y + 1;
try
{
Thread.sleep( 3 );
}
catch ( Exception e ) {}
}
while(y == 0)
{
if(isgoingdown)
{
isgoingdown = false;
}
else
{
isgoingdown = true;
y++;
}
}
while(y == 290)
{
if(isgoingdown)
{
isgoingdown = false;
y--;
}
}
}
}
public void startAnimation()
{
if ( runner == null )
{
runner = new Thread( this );
runner.start();
}
}
public void stopAnimation()
{
runner = null;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote