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

One of the most common application of using Threads (Runnable interface in parti

ID: 3761719 • Letter: O

Question

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.

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:

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:

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.

Explanation / Answer

ImagePanel.java:

public class ImagePanel extends JPanel implements Runnable {
   private Thread runner;
   private JLabel label;
   private ImageIcon ii;
   public ImagePanel()
   {
       this.setLayout(null);
       this.label = new JLabel();
       ii = new ImageIcon("lander.png");
       label.setIcon(ii);
      
       label.setBounds(0, 800, 200, 200);
       this.add(label);
   }
   public void startAnimation() {

       if (runner == null) {
           runner = new Thread(this);
       }
       runner.start();
   }

   public void stopAnimation() {
       if (runner != null)
           runner = null;
   }

   public void run() {
       while (runner != null) {
           for(int i=500;i>200;i--)
           {
               try {
                   Thread.sleep(5);
                   label.setBounds(0, i-200, 200, i);
               } catch (InterruptedException e) {
                   System.out.println(e);
               }
           }
           for(int i=0;i<1000;i++)
           {
               try {
                   Thread.sleep(5);
                   label.setBounds(i, 0, 100+i, 200);
               } catch (InterruptedException e) {
                   System.out.println(e);
               }

           }
          
           for(int i=0;i<500;i++)
           {
               try {
                   Thread.sleep(5);
                   label.setBounds(1000, 0, 1200, 200+i);
               } catch (InterruptedException e) {
                   System.out.println(e);
               }
           }
          
       }
   }
}

//##########################################################################################

MoonLander.java:

import java.awt.*;
import javax.swing.*;

public class MoonLander extends JFrame
{
   static Container c;
  
   public MoonLander()
   {
       c = getContentPane();
       c.setLayout(new BorderLayout());  
      
       setSize(1180,850);
       setVisible(true);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
   public static void main(String[] args) {
       new MoonLander();
       ImagePanel ip = new ImagePanel();
       c.add(ip);
      
       ip.startAnimation();
      
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote