943,981 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1106
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 10th, 2009
1

Very Simple Applet Problem

Expand Post »
Ok, well I am JUST beginning to learn Java, and I have run into a little stumbling block. I am attempting to write a little applet, but it doesn't seem to want to run.

My Java code:
java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class TestApplet extends Applet {
  5. public void paint(Graphics g) {
  6. g.drawString("Please work my little applet!", 20, 30);
  7. }
  8. }

My HTML:
html Syntax (Toggle Plain Text)
  1. <HTML>
  2. <HEAD><TITLE>Test Applet</TITLE></HEAD>
  3. <BODY>
  4. <applet code="TestApplet.class" width="300" height ="300">
  5. </BODY>
  6. </HTML>

So...I'm not quite sure where I am going wrong. I receive no errors while compiling and there isn't much code for me to mess up.

Could anybody help me with this problem?

Thanks in advance.
Similar Threads
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008
Oct 10th, 2009
0
Re: Very Simple Applet Problem
Read the Java tutorial on applets, here. You are missing a key method every applet needs.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Oct 10th, 2009
0
Re: Very Simple Applet Problem
Thanks for the link, except that example is not even working. I copied and pasted the code verbatim and I still get a little gray box of nothing.

Thanks again.

EDIT: Well, this is odd. I am able to view my applet within an IDE but not through a browser (Firefox 3.5). Any ideas to why this is?
Last edited by SoulMazer; Oct 10th, 2009 at 8:56 pm.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008
Oct 11th, 2009
0
Re: Very Simple Applet Problem
Click to Expand / Collapse  Quote originally posted by SoulMazer ...
EDIT: Well, this is odd. I am able to view my applet within an IDE but not through a browser (Firefox 3.5). Any ideas to why this is?
Not really lol. I never had to integrate them with webpages, I just had to make some for a class once. I'm sure someone else around here will know.
Last edited by jasimp; Oct 11th, 2009 at 12:57 am.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Oct 11th, 2009
2
Re: Very Simple Applet Problem
There are a couple of things you can check for this problem:
1. Is your browser set to "Enable Java" ? (Go to Tools > Options > Content Panel
2. Ensure that your applet file (class file) have the appropriate file-access permission and the browser should be able to read it (read/write access to files)
3. Read on security policies that go with your applets when deploying it to a browser.
2. Lastly, Did you get any error messages inside the browser?

hope that helps
Reputation Points: 30
Solved Threads: 7
Junior Poster
Jocamps is offline Offline
120 posts
since Jun 2007
Oct 11th, 2009
0
Re: Very Simple Applet Problem
1. Yes, Java is enabled.
2. Both of the files have read/write/execute access.
3. Sorry, could you explain this a little bit?
4. No, I received no error messages in Linux or Windows.

If you would like to see this for yourself, (in no way am I advertising my website) you can see it here: http://patbriggs.net/mytest.html

Thanks for the ideas.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008
Oct 12th, 2009
0
Re: Very Simple Applet Problem
Hello! Most Applets now extend the JApplet class now. (JApplet is part of the javax.swing package) So you should probably be extending that instead. also, all applets make a call to init(), start(), destroy(), and stop(). The paint() method is located in the Container class. anyways, that's just some background info for applets. You should add in the above methods to your code, because they're called by the browser to start the applet. hope this helps =)
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Oct 12th, 2009
0
Re: Very Simple Applet Problem
Actually, when I opened your link there was an error message displayed in the java console. If you are using Mozilla you can go to Tools > Java Console. If you are using Internet Explorer , right click on the applet area and select Java Console.

The error was a 'java.lang.NoClassDefFoundError' which is caused when your browser couldn't locate the *.class file of your applet. There are two options you can go about solving this:
1. make sure all your *.class files are in the same directory as to your html file; or
2. use CODEBASE attribute of the APPLET tag in your html file(point the codebase to the directory of where your *.class files of the applet are located)

About the security thing read it here:

http://java.sun.com/developer/online...ok/signed.html

hope that helps
Last edited by Jocamps; Oct 12th, 2009 at 1:01 am.
Reputation Points: 30
Solved Threads: 7
Junior Poster
Jocamps is offline Offline
120 posts
since Jun 2007
Oct 12th, 2009
0
Re: Very Simple Applet Problem
Ok, well it's rather hard for me to test as whenever I visit the page with my applet on it, my browser crashes (so all I know is that it doesn't work).

And both my .class file and my .html file are already in the same directory. I have also added a "codebase" element to my applet tag.

Updated code:
html Syntax (Toggle Plain Text)
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Test Applet</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <applet codebase="/" code="TestApplet.class" width="300" height ="300">
  7. </applet>
  8. </BODY>
  9. </HTML>

Thanks for all the great help. But have any more ideas?
Last edited by SoulMazer; Oct 12th, 2009 at 1:18 am.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008
Oct 26th, 2009
0
Re: Very Simple Applet Problem
Okay, I would like to start by say I'm sorry if this forum has a strict "no-bump after x days" etiquette, but I found a solution to my problem and I thought I would share. Although I did successfully have the latest JRE installed, I guess I didn't have a Java plugin installed for my browser. Being on Linux, I have no idea why my package manager would install only the JRE and not the browser plugin...oh well.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Rock, Paper, Scissor Game Help
Next Thread in Java Forum Timeline: String value not found





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC