0

I want to display information about the musical composition in the browser via Java applet. I use the library beaglebuddy_mp3.jar for id3 tags. A folder with files looks like this:

applet
 - index.html
 - FirstApplet.class
 - beaglebuddy_mp3.jar

In index.html I connect an applet:

<applet code="FirstApplet.class" archive="beaglebuddy_mp3.jar" width="500" height="500"></applet>

FirstApplet.class contains the following code:

import java.applet.Applet;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;

import com.beaglebuddy.mp3.MP3;

public class FirstApplet extends Applet{

public void paint(Graphics g){
    try {
        MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
        g.drawString(mp3.getBand() +" "+mp3.getTitle(), 20, 20);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
   } 
}

After starting the index.html file dialog box appears with a warning stating that I run the application at your own risk. Then I click "Run", instantly appears and disappears gray square. On that nothing is displayed.

murash
  • 211
  • 3
  • 14
  • Where is your [`start()`](http://docs.oracle.com/javase/7/docs/api/java/applet/Applet.html#start()) method? I think you should work through [Java Applets](http://docs.oracle.com/javase/tutorial/deployment/applet/) tutorial first. – PM 77-1 Oct 01 '14 at 04:48
  • `MP3 mp3 = new MP3("D:\\Music\\abc.mp3");` 1) Should be declared in the `init()` method, which is only called a single time per applet launch. 2) Should most likely use an URL instead of a `String` representing a file path. Unless the end user has an identically named and located MP3, it could not possibly work. 3) better to add labels to display the band and title, rather than override paint. – Andrew Thompson Oct 02 '14 at 06:12
  • 1) Why code an applet? If it is due to the teacher specifying it, 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 use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) Be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at default level, raise it. – Andrew Thompson Oct 02 '14 at 06:14

1 Answers1

0

Try the following:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.io.File;
import java.io.IOException;
import com.beaglebuddy.mp3.MP3;


public class FirstApplet extends JApplet {

    public void init() {

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {

                    MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
                    JLabel label = new JLabel(mp3.getBand() +" "+mp3.getTitle());
                    add(label);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

And secondly you have to sign your applet code with an official certificate to be able to run it in your web browser.

Lonzak
  • 9,334
  • 5
  • 57
  • 88
  • @Andrew Thompson wrote about the output, may be this is the reason that the data does not appear to tell me how to configure this? PS I am a novice in Java. http://i59.tinypic.com/qn0dqs.jpg – murash Oct 06 '14 at 05:28
  • Did you sign your applet with an official certificate? Otherwise it won't work in the browser. (You can alternatively lower your security bar in the java system settings) – Lonzak Oct 06 '14 at 07:27