10 Replies - 916 Views - Last Post: 07 June 2012 - 06:30 PM Rate Topic: -----

#1 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Songs in Array Applet

Posted 07 June 2012 - 10:53 AM

my assignment is to use arrays to create an applet that lists five of my 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
my issue i am having is finding away to put the array into an applet. any help is appreciated!

import java.applet.Applet;
import java.awt.*;

public class Songs extends Applet
{
  private String[] s1 = {"Break - Childish Gambino","Make it Nasty - 

Tyga","Drank in My Cup - Kirko Bangz","Spacejam - Timeflies","When 

the Lights Go Out - Bombs and Bottles"};

  public Songs()
  {
    int size = s1.length;
    for (int i=0; i<size; i++)
    {
      System.out.println(s1[i]);
    }
  }
public static void main(String[] s1)
  {
    new Songs();
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Songs in Array Applet

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Songs in Array Applet

Posted 07 June 2012 - 11:03 AM

Applets do not have main methods. Instead they have 4 methods that you override -

See Here

Unless I'm misunderstanding the task, you need to also override the paint method and Graphics.DrawString on varying x numbers. When the x value reaches the edge of the applet, reset with the next value in the string array.

P.S - You may want to Google backbuffering or doublebuffering to prevent flickering when you paint.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Songs in Array Applet

Posted 07 June 2012 - 11:49 AM

could you possibly clarify on that? we havent been taught about overiding . my knowledge of java is very basic. I was just hoping someone might be able to explain how to print the array in a applet. i believe i understand how to make it print one at a time, but i dont know how to get it into the applet to begin with.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Songs in Array Applet

Posted 07 June 2012 - 11:57 AM

It's weird how so many people get assignments on how to make applets when they don't know how to use them.

But anyways I suggest reading up on them. Take a look here and run through the left hand side.

Basic format -

class app extends Applet
{
   public void init() {
   }

   public void start() {
   }

   public void stop() {
   }

   public void destory() {
   }

}


For more info on what those methods are meant for, take a look at the previous link.

Anyway's applets are not like normal applications in that a System.out.println() will not show anything to the user directly. Instead you have to paint onto the applet using the paint method. Simply printing an array in an applet isn't that easy -

public void paint(Graphics g)
{

}


The graphics object has a load of built in methods to draw shapes etc.

For example -

public void paint(Graphics g)
{
   g.drawString("some string",XCoordinateOnApplet, yCoordinateOnApplet);
}

Was This Post Helpful? 0
  • +
  • -

#5 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Songs in Array Applet

Posted 07 June 2012 - 01:12 PM

We have learned a small amount about applets, but only enough to write my name, draw concentric circles, and make a snowman.
This is my moddified code, but this wont run in html, i get a : java.lang.reflect.InvocationTargetException
import java.applet.Applet;
import java.awt.*;

class Songs extends Applet
{
final int width = 500, height = 500;
   String[] s1 = {"Break - Childish Gambino","Make it Nasty - 

Tyga","Drank in My Cup - Kirko Bangz","Spacejam - Timeflies","When 

the Lights Go Out - Bombs and Bottles"};

  public Songs()
  {
  
    int size = s1.length;
    for (int i=0; i<size; i++)
    {
      System.out.println(s1[i]);
    }
  }
public static void main(String[] s1)
  {
    new Songs();
  }
   public void paint(Graphics g)  
    {  
    int xPos = 250;  
    int yPos = 0;
    int i = 0;
    g.drawString("s1[i]", xPos, yPos);  
    yPos++;  
    if(yPos > 25)  
        {  
        g.drawString(s1[i], xPos, yPos);  
        xPos++;  
        yPos = 25;  
        }  
    }  
  
}  




HTML code:
<html>
<body>
<applet code="Songs.class" width=500 Height=500>
</applet>
</body>
</html> 


Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Songs in Array Applet

Posted 07 June 2012 - 01:16 PM

Read my links. Applets do not have a main method.

They have .init() and .start() methods (which you should put your constructor code into).
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: Songs in Array Applet

Posted 07 June 2012 - 01:39 PM

Also, you should use the more modern JApplet over the antiquated AWT Applet class.
Was This Post Helpful? 0
  • +
  • -

#8 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Songs in Array Applet

Posted 07 June 2012 - 01:42 PM

ill try my best. this is an online high school course im taking.everythings outdated.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: Songs in Array Applet

Posted 07 June 2012 - 01:44 PM

Really, in terms of what you need to change, the only thing is the name of the class. Behind the scenes, JApplet is more modern and a Swing GUI Component over the antiquated AWT Applet class. So by changing it, there is a lot of behind the scenes functionality going on that is better to use.
Was This Post Helpful? 0
  • +
  • -

#10 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Songs in Array Applet

Posted 07 June 2012 - 03:40 PM

I hope this is putting me on the right track. however it only prints out Break - Childish Gambino but is not moving.
import java.awt.*;
import java.applet.Applet;
public class song1 extends Applet implements Runnable
{
    int i = 0;
    int xpos = 250;
    int ypos = 0;
    Thread runner;
      String[] s1 = {"Break - Childish Gambino","Make it Nasty - 

Tyga","Drank in My Cup - Kirko Bangz","Spacejam - Timeflies","When 

the Lights Go Out - Bombs and Bottles"};
    int size = s1.length;


    public void run()
    {
        while(true)
        {
        for (int i=0; i<size; i++)
{
             if (this.ypos!= 200)
        {
        this.ypos += 1;
        }
}
            repaint();
            try { runner.sleep(1000); }
            catch (InterruptedException e) { }
        }
    }
    public void paint(Graphics g)
    {
        g.drawString(s1[i] ,70,70);
    }
} 


This post has been edited by mrhemphill: 07 June 2012 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#11 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Songs in Array Applet

Posted 07 June 2012 - 06:30 PM

I finally have a code that seems to work well, i just need some help that will change the array index so the songs change after they roll of the screen
import java.awt.*;
import java.applet.Applet;
public class song1 extends Applet implements Runnable
{
       String[] s1 = {"Break - Childish Gambino","Make it Nasty - Tyga","Drank in My Cup - Kirko Bangz","Spacejam - Timeflies","When the Lights Go Out - Bombs and Bottles"};
    int i=0; 
    int yPos = 0;
    int xPos = 200;
    Thread runner;

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

     public void run()
    {
        while(xPos<500)
        {
           if (yPos > 250 && xPos<500)
            {
                xPos += 2;
                repaint();
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }
            
            else if (yPos <250)
            {
                yPos += 2;
                repaint();
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }
    
             else if (xPos == 500)
            {
                
                i++; 
                xPos = xPos-300;
                yPos += 2;
                repaint();
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }
        }
    }
    public void paint (Graphics g)
    {
     setBackground (Color.yellow);
    int i=0; 
    g.drawString(s1[i], xPos,yPos);
    i++;
   
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1