Reputation: 163
I am beginner to Applets. Here is code for a basic applet to display string.
package firstjavaapplet;
import java.awt.Graphics; // program uses class Graphics
import javax.swing.JApplet; // program uses class JApplet
public class FirstJavaApplet extends JApplet
{
// draw text on applet’s background
@Override
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
public static void main(String[] args)
{
FirstJavaApplet obj = new FirstJavaApplet();
}
}
Following is HTML file I am using to include applet in webpage.
<body>
<applet code = "FirstJaveApplet.class" width = "300" height = "300">
</applet>
</body>
</html>
When I run Applet in appletviewer FirstJaveApplet.html , I get following:
String is not being displayed rather "Start: applet is not initialized."
Upvotes: 0
Views: 42907
Reputation: 25
i face the same, but in my case i was using jdk17 to compile and using that .class file to run on appletviewer but appletviewer is closed after jdk9 so, any .class file that was created using jdk9 or higher it can't support so, i just used jdk8 to compile my program and it run fine on that appleviewer
Upvotes: 0
Reputation: 37
I was also facing same problem because, i have changed path during installation of Older JDK (jdk 10.0.2)
to D Drive.
By doing this what happen is your main jdk-10.0.2
folder will install in C Drive's ProgramFiles folder and only copy of bin and some other folder is created in you newly mentioned path during installation.
C Drive
> Programfiles
> java
.jdk-10.0.2
(or older whichever yo have downloaded) and paste it wherever you want.jdk-10.0.2
folder -> Go to bin folder of it and write all your programs here.Upvotes: 0
Reputation: 11
I was also facing the same issue and none of the forum's solution rescued me :(
Then I realized, we need the set the size and visibility of the applet. You can include the following constructor as your code:
FirstJavaApplet()
{
setSize(500, 500);
setVisible(true);
}
Upvotes: 1
Reputation: 1
No Need to write the main method in applet and you have to extend Applet than your applet will run
public class FirstJavaApplet extends Applet
Upvotes: 0
Reputation: 1
no need to create html file.whatever line you write in html also write in java after import statement
import java.awt.Graphics; // program uses class Graphics
import javax.swing.JApplet; // program uses class JApplet
/*<applet code = "FirstJaveApplet.class" width = "300" height = "300">
</applet>*/
public class FirstJavaApplet extends JApplet
{
// draw text on applet’s background
@Override
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
public static void main(String[] args)
{
FirstJavaApplet obj = new FirstJavaApplet();
}
}
Upvotes: 0
Reputation: 1
Write the HTML code as below:
<html>
<body>
<applet code="firstjavaapplet.FirstJavaApplet" width ="300" height ="300">
</applet>
</body>
</html>
Save it as: FirstJavaApplet.html
Compile your Java Source File as:
javac -d . FirstJavaApplet.java
(You might have skipped compiling your Java Source File)
Then run your HTML file as:
appletviewer FirstJavaApplet.html
Upvotes: 0
Reputation: 151
Don't name your HTML file same as the Class File I think that will work.
Upvotes: 0
Reputation: 1
Try after placing the above java file,in a folder with name firstjavaapplet(pakage name) in the source pakagefolder.
Upvotes: 0
Reputation: 1
Change the Build path as:
On your Project JRE system Library > Build Path> Configure Build path Choose JRE 1.7 or 1.8 (if installed it will be displayed) Remove the old one from build path by right-clicking and choosing the right option
Upvotes: 0
Reputation: 3903
In the case of PApplet. On MacOS 10.10
You need to change the the JRE System Library
min to 1.7
.
In my case java.lang.UnsupportedClassVersionError: processing/core/PApplet : Unsupported major.minor version 51.0
Its throwing above. Its resolved when I'd set .JRE System Library-1.7
Hope its help you.
Upvotes: 0
Reputation: 168845
<applet code = "FirstJaveApplet.class" width = "300" height = "300">
</applet>
The code
attribute value should be the Fully Qualified Class name as opposed to the applet file name. So that should read:
<applet code = "firstjavaapplet.FirstJavaApplet" width = "300" height = "300">
</applet>
Note that the JRE will search for the class in a sub-directory of the HTML directory named firstjavaapplet
. Unless the class is present in the right place, the problem will continue.
Upvotes: 3
Reputation: 12042
Applet don't need a main method to start
Just run without main method
I think you having the problem with your package name
compile without the package name package firstjavaapplet;
public class FirstJavaApplet extends JApplet
{
@Override
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
}
Upvotes: 0