0

I need to write an applet for a computer science course. I need to have five song titles scroll on to the screen from the top and off the right side, one at a time on a loop. I wrote my code and it compiles but when I try to run it in the appletviewer I get a blank window that says applet not initialized. I'm assuming that it's not a problem with the main code and that it's probably something in the start() method. Here's the code:

import java.awt.*;
public class SongTitles extends java.applet.Applet implements Runnable
{
String song[] = {"Watch The Corners","Where Is My Mind","Different World","Seven Nation Army","Nowhere Man"};
int[] y = new int[] {0,0,0,0,0};
int[] x = new int[] {200,200,200,200,200};
boolean[] cycle = new boolean[5];
Thread runner;

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


public void paint (Graphics g)
{
for(int i = 0; i < 5; i++)
{
        g.drawString(song[i], x[i], y[i]);
}
}

public void run()
{
cycle[0] = true;

for(int c = 0; c < 5; c++)
{

        while(cycle[c] = true)
        {
        if (y[c] < 200)
        {
                y[c] += 2;
                repaint();
                try 
                {
                runner.sleep(50);
                }
                catch (InterruptedException e) { }
    }

        if (y[c] == 200 && x[c] < 400)
        {
                x[c] += 2;
                repaint();
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }

        if (x[c] == 400)
        {
                x[c] = 200;
            y[c] = 0;
                repaint();
        cycle[c] = false;
        cycle[(c + 1)] = true;
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }

    }

    if(c < 5)
    {
        c++;
    }

    if(c == 5)
    {
        c = 0;
    }
    }
  }
}

And here's the html file to run it:

<html>
<body>
<applet code = "SongTitles.class" width=400 height=400>
</applet>
</body>
</html> 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    It runs with no problem in my applet viewer. I am using eclipse IDE. – neoprez Jan 10 '14 at 03:57
  • 1) Please refer your teacher 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 Jan 11 '14 at 07:17

0 Answers0