About two weeks ago, I was given a simple project in Java II; code a program that displays an image, plays a sound, and has loop/play/stop buttons for the sound and zoom in/zoom out buttons for the image. I decided to go ahead in the book with this project to get into some actual web development; I wanted to be able to have a real applet that people would be able to see if they accessed it on a server; basically, instead of referencing local variables, I accessed information stored on a remote server (http://lucarioboards.com) I explained all of this to her in an email in addition to in person, but she didn't read the email or seem to understand what I was saying.

After showing her my code, she said it was impossible to reference a "website" (the links I had to the image and sound from Java), and that I would have to "code a program to access the internet". I politely asked her to run the program, which runs perfectly on my computer (Windows Vista, compiled with NetBeans, while she uses XP and TextPad), and it compiled with errors (as well as a pop-up from her firewall, which was likely the source of the errors). She asked me to substitute in the filenames for the links and send it back to her.

Unfortunately, I cannot get the program to run with local resources instead of web resources. At least not in the state it's in. The page of my book that teaches how to access local images is, of course, missing... another reason (in addition to thinking I'd impress the teacher) I skipped ahead in the book to find a harder and better way to do this.


Here's my code:

//Programmer's name:
//Program purpose: To zoom in/out of an image while playing a song, which you can play/stop/loop.


// import packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.applet.*;
//extend from applet
public class WhatTimeIsIt extends Applet{
    //declare 3 panels
    private JPanel buttonPanel, imageBorder, totalBorder;
    //declare 5 buttons
    private Button playButton, loopButton, stopButton, zoomButton, shrinkButton;
    //desclare one image
    private Image zidane;
    //declare one audio clip
    private AudioClip audioClip;
    private double scaleValue;
    //declare two integers
              int x = 450;
          int y = 600;
    public void init(){
        //declare the three panels
        totalBorder = new JPanel();
        buttonPanel = new JPanel();
        imageBorder = new JPanel();
      //add play, loop, stop, zoom in, and zoom out buttons to the buttonPanel
    playButton = new Button(" Play ");
    buttonPanel.add(playButton,BorderLayout.SOUTH);
    loopButton = new Button(" Loop ");
    buttonPanel.add(loopButton,BorderLayout.SOUTH);
    stopButton = new Button("  Stop  ");
    buttonPanel.add(stopButton,BorderLayout.SOUTH);
    zoomButton = new Button(" Zoom In ");
    shrinkButton = new Button(" Zoom Out ");
    buttonPanel.add(zoomButton,BorderLayout.SOUTH);
    buttonPanel.add(shrinkButton,BorderLayout.SOUTH);
    //get the audio clip from a remote server
    audioClip = getAudioClip(getCodeBase(), "http://lucarioboards.com/JazzPiano.mid");
    //play the audio clip
    audioClip.play();
    //get image from a remote server
    zidane = getImage(getCodeBase(),"http://lucarioboards.com/LucarioAndAaron.jpg");
        add(imageBorder,BorderLayout.NORTH);
    add(totalBorder,BorderLayout.CENTER);
    add(buttonPanel,BorderLayout.SOUTH);
    zoomButton.addActionListener(
//add new actionlistener
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
        //when zoom button is clicked, double image size
    x = x * 2;
    y = y * 2;
    //repaint
    repaint();
    //show status
    showStatus("Zoom in button is clicked");
    }});
        shrinkButton.addActionListener(
            new ActionListener(){
                    //when zoom out button is clicked, halve image size
    public void actionPerformed (ActionEvent e){
        x = x / 2;
        y = y / 2;
        //repaint
    repaint();
    //show status
    showStatus("Zoom out button is clicked");
    }});
        loopButton.addActionListener(
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
        //when loop button is clicked, loop the music
audioClip.loop();
//show status
showStatus("Loop button is clicked");
    }});
    //when play button is clicked, play the music
            playButton.addActionListener(
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
audioClip.play();
//show status
showStatus("Play button is clicked");
    }});
             stopButton.addActionListener(
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
        //when stop button is clicked, stop the music
audioClip.stop();
//show status
showStatus("Stop button is clicked");


      }
});
    }
    //paint method
      public void paint(Graphics g) {
//draw image
    g.drawImage(zidane, 0, 0, x, y, imageBorder);
              }

    }

and my HTML file:

<HTML>
<HEAD>
   <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>

<!--
*** GENERATED applet HTML launcher - DO NOT EDIT IN 'BUILD' FOLDER ***

If you need to modify this HTML launcher file (e.g., to add applet parameters), 
copy it to where your applet class is found in the SRC folder. If you do this, 
the IDE will use it when you run or debug the applet.

Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in 
the Packaging page in the Project Properties dialog.

For more information see the online help.
-->

<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

<P>
<APPLET codebase="classes" code="WhatTimeIsIt.class" width=350 height=200></APPLET>
</P>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

This was due three days ago. I fear I'm just going to be given a failing grade in it, despite going beyond the requirements, because my teacher can't understand what I did.

Please help.
Did I do something wrong I'm not seeing here? Why's it run on my computer? Is it because she's essentially compiling it in a text editor, or is it her firewall?

BTW: When I upload the HTML file to my server along with the Applet, the music plays, but the file never finishes loading. Why is this?

Thanks
~Will

Recommended Answers

All 3 Replies

As far as I know, you should be extending JApplet, not Applet, and if you are expecting this to run in a web environment, your teacher should not have to do anything other than going to the correct website with a Java enabled web browser. You'd have to embed the JApplet in an html page and on your server, the JApplet would have access to whatever resources it needed access to.

edit:

But that being said, the Applet actually works for me as is... so... no idea why your teacher can't get it to run.

Lucario, your situation intrigued me so I did a little bit of research and digging to try to figure out what it going on. What I've come up with might be helpful to you although it does not answer the question of why your teacher cannot run your project from within certain environments (keep reading).

When run in an Applet that is embedded in a web browser, I do not think your project would work as it is written. For information on why that is, you should read this link which describes how Java applications run by the user (i.e., you double click on the executable file, or you compile and run from the command line, or you hit the run button from within an IDE such as Eclipse) implicitly have a higher level of permissions than applets. Therefore you have to give Applets more permissions when run from a browser.

Secondly I was confused by your use of getCodeBase(), since getCodeBase() returns the URL of the directory which contains this Applet. I.e., getCodeBase() would be the correct thing to call if your Applet code was located on a web server, but it doesn't really make sense (even though it works) the way you called it. I think this bit of code makes more sense, maybe it will clarify what I'm saying:

URL url = null;
	try {
		url = new URL("http://lucarioboards.com/");
	} catch (MalformedURLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
    audioClip = getAudioClip(url, "./JazzPiano.mid");

The difference is that when I download your code onto my machine, getCodeBase() is going to return the Applet location as somewhere on my machine. If you were to have your Applet hosted on your server then I think calling getCodeBase() would make sense.

Anyway, as the first article I linked you to explains, none of the Socket nonsense would matter if the resources you're using (i.e. your Audio file and the Image) were stored on the same server as the Applet. So I think if you embedded your Applet in an html page on your server, then went and tested your program from various browsers (on different machines) that everything would work.

P.S. here is the html file I used

<applet code="WhatTimeIsIt.class" 
        codebase="./"
        archive="./test.jar"
        width="600" height="95">
    <param name="maxwidth" value="120">
    <param name="nimgs" value="17">
    <param name="offset" value="-57">
    <param name="img" value="images/tumble">

Your browser is completely ignoring the <applet> tag!
</applet>

And here is how I changed your code (it is barely changed)

//Programmer's name:
//Program purpose: To zoom in/out of an image while playing a song, which you can play/stop/loop.


// import packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.applet.*;
import java.net.MalformedURLException;
import java.net.URL;
//extend from applet
public class WhatTimeIsIt extends JApplet{
    //declare 3 panels
    private JPanel buttonPanel, imageBorder, totalBorder;
    //declare 5 buttons
    private Button playButton, loopButton, stopButton, zoomButton, shrinkButton;
    //desclare one image
    private Image zidane;
    //declare one audio clip
    private AudioClip audioClip;
    private double scaleValue;
    //declare two integers
              int x = 450;
          int y = 600;
    public void init(){
        //declare the three panels
        totalBorder = new JPanel();
        buttonPanel = new JPanel();
        imageBorder = new JPanel();
      //add play, loop, stop, zoom in, and zoom out buttons to the buttonPanel
    playButton = new Button(" Play ");
    buttonPanel.add(playButton,BorderLayout.SOUTH);
    loopButton = new Button(" Loop ");
    buttonPanel.add(loopButton,BorderLayout.SOUTH);
    stopButton = new Button("  Stop  ");
    buttonPanel.add(stopButton,BorderLayout.SOUTH);
    zoomButton = new Button(" Zoom In ");
    shrinkButton = new Button(" Zoom Out ");
    buttonPanel.add(zoomButton,BorderLayout.SOUTH);
    buttonPanel.add(shrinkButton,BorderLayout.SOUTH);
    //get the audio clip from a remote server
    URL url = null;
	try {
		url = new URL("http://lucarioboards.com/");
	} catch (MalformedURLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	
	URL url1 = getCodeBase();
	System.out.println(url1.toString());
    audioClip = getAudioClip(url, "./JazzPiano.mid");
    //play the audio clip
    audioClip.play();
    //get image from a remote server
    zidane = getImage(url,"./LucarioAndAaron.jpg");
        add(imageBorder,BorderLayout.NORTH);
    add(totalBorder,BorderLayout.CENTER);
    add(buttonPanel,BorderLayout.SOUTH);
    zoomButton.addActionListener(
//add new actionlistener
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
        //when zoom button is clicked, double image size
    x = x * 2;
    y = y * 2;
    //repaint
    repaint();
    //show status
    showStatus("Zoom in button is clicked");
    }});
        shrinkButton.addActionListener(
            new ActionListener(){
                    //when zoom out button is clicked, halve image size
    public void actionPerformed (ActionEvent e){
        x = x / 2;
        y = y / 2;
        //repaint
    repaint();
    //show status
    showStatus("Zoom out button is clicked");
    }});
        loopButton.addActionListener(
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
        //when loop button is clicked, loop the music
audioClip.loop();
//show status
showStatus("Loop button is clicked");
    }});
    //when play button is clicked, play the music
            playButton.addActionListener(
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
audioClip.play();
//show status
showStatus("Play button is clicked");
    }});
             stopButton.addActionListener(
            new ActionListener(){
    public void actionPerformed (ActionEvent e){
        //when stop button is clicked, stop the music
audioClip.stop();
//show status
showStatus("Stop button is clicked");


      }
});
    }
    //paint method
      public void paint(Graphics g) {
//draw image
    g.drawImage(zidane, 0, 0, x, y, imageBorder);
              }

    }

I'd be interested to see the results of testing what I said above so try it out and let me know how it goes. Of course, you would have to play with the html file somewhat (see the JApplet and Applet tutorials from java sun I linked you to earlier) depending on the exact location and name of your jar file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.