So basically, I've mentioned the HTML code in the java file, But um the applet wont execute for some reason, Help me
import java.awt.*;
import java.applet.*;
/*
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
*/
class demo extends Applet {
public void paint(Graphics g)
{
String string = getParameter("txt");
g.drawString(string, 29, 40);
start();
}
}
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
That code parameter is incorrect. It should be the fully qualified class name. Or..
<applet code = "demo" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
To compile & launch it in applet viewer from the command line, do something like:
prompt> javac demo.java
prompt> appletviewer demo.java // (see Note)
Note: Yes I do mean the .java extension. AppletViewer can launch an applet from that comment embedded in the source code. See the Applet info. page (at To compile and launch:) for another example.
Questions/Comments
Debugging
Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.
Copy/paste all error & exception output the console provides.
Code
The applet code itself would be better to declare a String txt that is declared as a class attribute and initialized in the init() method, like this txt = getParameter("txt");. the paint(Graphics) method might be called many times.
Any time a paint(..) method is overridden, it should immediately call super.paint(..) (for the BG color, if nothing else).
Questions
Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
Why AWT rather than Swing? See my answer on Swing extras over AWT for many good reasons to abandon using AWT components.
you should give the class name not java file name.Go through applet tutorials for good understanding.
Try this,
import java.awt.*;
import java.applet.*;
public class demo extends Applet {
public void run(){
repaint();
}
public void paint(Graphics g)
{
String string = getParameter("txt");
g.drawString(string, 29, 40);
}
}
/*
<html>
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
</html>
*/
Related
I created a Java applet supposed to be used in an applet as applets should.
For some reason, all browsers (chrome, firefox, IE) cut it.
I tried using the repaint() and invalidate() methods. I didn't help...
But, interestingly, when I scroll the browser up or down (even a tiny scroll), it immediately fixes. Also, when I use showOptionDialog(Component, String) method, if fixes after the user clicks the OK button on the dialog. It fixes only when the component inputted is the applet itself.
I don't understand why it crops, or how to fix it.
The HTML calling the applet it is very simple. Here is an example of a short piece of HTML code which causes the problem:
<html>
<head>
<title>nispahit</title>
</head>
<body>
<center>
<APPLET CODE="Applet/AppletMain.class" archive="nnis.jar, log4j-1.2.16.jar" WIDTH=800 HEIGHT=600>
<param name="userName" value="http://www.nispahit.com/images/shalom.jpg">
<param name="ColorsContrast" value="241026555"/>
<param name="param2" value="14">
<param name="param3" value="2">
<param name="param3" value="2">
</APPLET>
</center>
</body>
</html>
A solution or workaround will help very much. Any insight would also be helpful.
Thanks.
The cropping happens outside of the applet. There is some block element around the applet (usually a <div>) which is too small to contain the whole applet.
Try to add a div with a red background around the applet so you can see quickly which part is rendered by the browser and which comes from the applet.
My class's code:
package overviewPack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonScreen extends JApplet implements ActionListener{
JButton middle = new JButton();
Container screen = getContentPane();
public void init(){
setVisible(true);
middle.addActionListener(this);
screen.add(middle);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == middle){
System.out.println("hey");
}
}
}
When I attempt to run it using html, I recieve a noclassdefFound error, with the stacktrace as ButtonScreen(Wrong name: overviewPack ButtonScreen)
Here is my html code: (I use brackets so that the code will turn up in chat as the code and not the finished product).
<HEAD>
<TITLE>
A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="overviewPack.ButtonScreen.class" codebase = "bin" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
I have tried many different formats for the html, and looked at many other people's similar, sometimes exactly the same errors, but none of the solutions proposed to other people have worked. I have also looked around on the rest of the net for a solution, but I have found none.
This error happens with all my applets, even this extremely simple applet I did above.
The html file is in the same folder as the class
The HTML file is in the same folder as the class
That is no good. You need to understand how the parameters in the applet element work.
<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>
Let us say that the HTML is located at: our.com/applets/applet1.html.
codebase = "bin" would mean the classpath starts with our.com/applets/bin/.
overviewPack.ButtonScreen.class would therefore need to be found at:
our.com/applets/bin/overviewPack/ButtonScreen.class
Note that the package overviewPack has become an inherent part of the correct path to the class file. That is where the 'wrong name' is originating from. The JRE seems to be searching the directory of the HTML, locating the class in the same directory, then loading it to discover it is in the wrong path.
Code Attribute
<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>
Note the required value is the Fully Qualified Name of the class file. That consists of the package(s) name, followed by the class name, each separated by a dot. E.G.
overviewPack.ButtonScreen
As opposed to
overviewPack.ButtonScreen.class // combination of FQN with file type
or
overviewPack/ButtonScreen.class // relative file path on server
So the opening APPLET element should best be:
<APPLET CODE="overviewPack.ButtonScreen" codebase="bin" WIDTH=150 HEIGHT=25>
Sometimes there is a problem with the .class file extension at the end of the code= attribute. Some doc I've seen says that the code= attribute has the classname in which case having the .class at the end is wrong. The classname is: overviewPack.ButtonScreen and the filename is:
ButtonScreen.class
I am experiencing a little problem reading parameters from an html page to an applet.
my code (necesssary for question):
on html page:
<PARAM NAME = "name" VALUE = "Nicholus">
in applet (init):
String strName = getParameter("name");
The applet just decides to look at me instead of getting the name value..
a few google search shows im not the only one, except I haddn't yet found the solution, so I decided to post it here in case it was already resolved.
Compare your work with the following working code or post your code if problem still not solved.
Here is the ParamDemo.java code
import java.awt.*;
import java.applet.*;
public class ParamDemo extends Applet{
String strName;
public void start()
{
strName=getParameter("name");
if(strName==null)
strName="Not Found";
}
public void paint(Graphics g)
{
g.drawString("Name :"+strName,10,20);
}
}
Then the Applet.html
<html>
<body>
<applet code="ParamDemo" width="300" height="300">
<param name='name' value="Nicholas">
</applet>
</body>
</html>
Snapshot
[Ps:You need to compile your .java before using in HTML page and make sure that there is no problem related to leter-case of parameter-names as java is case-sensitive.Also the .class and .html files must be in same root folder).
I got it solved, my negligence... my html had many applet codes, the params were not printed within the applet declaration of the one that needs the params. thanks for contributing.
I have made a GUI in Java using State Based Game, as it extends StateBasedGame and not JApplet its not a true applet, I want to put it on a website and am unsure on how to do this, I have been told that the following code allows me to make this into an applet using html and not having to edit the Java code:
<applet code="org.lwjgl.util.applet.AppletLoader"
archive="org.lwjgl.util.applet.AppletLoader"
codebase="."
width="640" height="480">
<param name="al_title" value="Ham Blaster">
<param name="al_main" value="org.newdawn.slick.AppletGameContainer">
<param name="game" value="org.javagame.Game">
<param name="al_jars" value="racegame.jar, lwjgl.jar, slick.jar">
<param name="al_windows" value="windows_natives.jar">
<param name="al_linux" value="linux_natives.jar">
<param name="al_mac" value="macosx_natives.jar">
<param name="separate_jvm" value="true">
</applet>
When I run the html code I get the following error on the centre of my screen:
An error occurred while loading the applet. Please contact support to resolve this issue. missing required applet parameter: al_main.
I have looked inside slick for the ApplerGameContainer but I can only find chrome files, I tried putting these in the same directory as the html file but I still get the same error.
If something seems very simple and may seem too obvious to ask me, please tell me because I am new to Java, my previous error with this code was because I did not have my html and lwjgl_util_applet.jar in the same directory, so please tell me anything that may help me.
Emm...
I am not pretty sure, but according to sources the error comes from init method
/*
* #see java.applet.Applet#init()
*/
public void init() {
state = STATE_INIT;
// sanity check
String[] requiredArgs = {"al_main", "al_logo", "al_progressbar", "al_jars"};
for(int i=0; i<requiredArgs.length; i++) {
if(getParameter(requiredArgs[i]) == null) {
fatalErrorOccured("missing required applet parameter: " + requiredArgs[i], null);
return;
}
//... }
}
so it seems your al_main param value is null or something :S
The problem is maybe someplace here
<param name="al_main" value="org.newdawn.slick.AppletGameContainer">
... maybe because I don't see you to init all the rest required params as "al_logo", "al_progressbar"
Anyways, it would be more helpful you to show the problem code
Report if it helps
I have a strange and fustrating problem. I loaded all my .class files into a JAR file and placed it on my local web server. The problem is when I navigated to the page with my JAR I got a big ClassNotFoundExeption. I am 100% certian this class file is in my JAR. I also know the JAR file is in the same directory as my XHTML file
Here is the XHTML source
<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
archive = "program.jar"
code = "inigui4.class"
width = "500"
height = "300"
>
<param name="cache_option" value="no">
</applet>
</center>
</body>
</html>
Very strange!
For some reason when I create a hello world applet I get the same problem (could it be with my LightTPD server?
Java source follows
import java.awt.*;
import java.applet.*;
public class inigui4 extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
}
}
Even stranger.
Now I can't load any applets on the web because I get this error. I will need to contact Oracle in the morning.
It can be caused by multiple reasons, but the most probable one is that you haven´t specified the package of the class you´re using. Look at the answer of this post.
If you are using packages you have have to prefix inigui4.class by its relative path into the jar
I think the problem is here:
<applet
archive = "program.jar"
code = "inigui4.class"
width = "500"
height = "300"
>
Here, you specify inigui4.class - but in your Java code:
public class inigui_rb extends Applet {
...
}
This is inigui_rb. Try:
<applet
archive = "program.jar"
code = "inigui_rb.class"
width = "500"
height = "300"
>
The ClassNotFoundException should go away.
Also please adhere to the Java coding conventions when coding! In this case:
Class Names Begin With Capitals!
ClassNamesDoNotContainUnderscoresInsteadTheyAreCamelCased!