0

I'm currently working on an assignment for Java applets. This is the assignment:

Use arrays to create an applet that lists five of your favorite songs. The applet should:

  • Scroll the list of song titles, one at a time.
  • Each song title should start at the top of the applet and scroll to the middle then, scroll off the right hand side.
  • Each new song title should scroll in a different colour.
  • The applet should loop, that is, when it gets to the end of the list, start over again at the beginning of the list.

So far, my code is as follows:

import java.applet.Applet;
import java.awt.*;
public class SongList extends Applet implements Runnable
{
  String[] list = {"A - B", "C - D", "E - F", "G - H", "I - J"};
  int counter = 0, xPos = 100, yPos = 0;
  Color text_color = getRandomColor();
  Thread runner;

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

  public void run()
  {
  while (xPos < 220)
  {
      if (yPos < 100)
         {
        yPos += 2;
        repaint();
        try
        {
           runner.sleep(50);
        }
        catch (InterruptedException e) { }
         }

      else if (yPos == 100 && xPos != 220)
         {
        xPos += 2;
        repaint();
        try
        {
           runner.sleep(50);
        }
        catch (InterruptedException e) { }
         }

      else if (xPos == 220)
         {
         counter++;
         xPos = 100;
         yPos = 0;
         }
   }
   }

   public void paint(Graphics gr)
    {
   setBackground (Color.yellow);
   gr.drawString(list[counter], xPos, yPos);
    }
}

HTML file:

<html>
<body>
<applet code = "SongList.class" width = 200 height = 200>
</applet>
</body>
</html>

The code compiles fine but the I can't find a way to change to the next song after the previous one scrolls out of the screen.

How can I achieve that?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Thanks for the edit Amal. Sorry for being obnoxious – user3529827 Jun 01 '14 at 03:19
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jun 01 '14 at 09:30

1 Answers1

0
  1. else if (xPos == 220) is useless. :) because you are using loop upto 219 as xPos<220 so change it to xPos<=220
  2. catch (InterruptedException e) { } you are swallowing Exception so that it compiles properly but you won't determine the Exception so use

    catch (InterruptedException e) { e.printStackTrace();}

  3. and because of 1. gr.drawString(list[counter], xPos, yPos); can't get proper counter in public void paint(Graphics gr).

akash
  • 22,664
  • 11
  • 59
  • 87
  • Thanks for the answer! I think I fixed my problem by changing the while (xPos < 220) to while(true). But after doing a full loop, my command prompt glitched out. Not sure how to solve this issue. -- I added in an if statement to increment the counter and fixed the issue. thanks! – user3529827 Jun 01 '14 at 03:26
  • `while(true)` will solve your issue too but be more specific as I said `(xPos <= 220)` will also do the same. – akash Jun 01 '14 at 03:31