I don't know about netbeans, but a regular java applet looks like that if you don't specify the height and width in the applet tag of the html file.
Almighty Wizard
2013-07-03 10:26:34 UTC
So instead of reading over an established tutorial, complete with examples, from, let's say, Java directly, you would rather have someone write an Applet tutorial here? *facepalm*
?
2013-07-03 10:10:30 UTC
Well, an Applet is compiled into byte-code. It is code that is not prepared for the CPU to use, so it needs a Virtual Environment to run in. Hence the JVM everyone must install to run Java programs.
So what that should lead you to ponder, is how does an HTML page, which is only a webpage sent from a web-server, launch the JVM. Well you could offer the applet as a file-download, and the user could open it manually. But that's not very user friendly, and disregards the supreme point of an applet. It should run inside "the browser". Which means you need HTML code to insert an applet into the generated webpage that the user sees.
It's supremely more satisfactory to point you towards a tutorial, but for the sake of your demands, here it is...
First, the old way of doing it for your understanding...
code="com.myorganization.mainClass" />
We create an HTML(4 or lower) applet class to render an 800x600 JVM instance, that executes the "mainClass" inside our package structure, which is compiled in a zip-like archive called "myCompiledProject.jar".
That's it. It should work on new browsers which use HTML5 and above, depending on your DOCTYPE (strict/loose) and the browser that is rendering it.
You SHOULD define a DOCTYPE, and write your HTML according to it's rules. That way you do not encounter weird issues with certain browsers and users.
Here is the new HTML5 way of doing it:
As you can see, the usage is the same. The main thing to notice is that you are using parameters in the Object structure. It's akin to loading a flash/shockwave video player.
And you can pass variables to your software applet using these params, assuming you write code to receive them.
Here is an example of Java code using the above specifications. Keep in mind the above goes into an HTML file (preferably "index.html"), and will be accompanied by the JAR file on your web server, that the following code produces when properly compiled in your IDE. Compiling a JAR package is NOT the same as compiling a class. I highly recommend opening a JAR file inside of Winrar or 7zip or something, so you visually see how it works. It's exactly as specified in code when you define a package -- in this example's case: "com.myorganization.mainClass" in the project named "myCompiledProject".
You should note that some IDE's automatically generate the HTML code for you when you specify the project type as an applet. I highly recommend Eclipse. Just an FYI.
import java.applet.Applet; // We are making an applet. We need the Applet template.
import java.awt.*; // widgets for GUI
import java.awt.event.*; // Events for the widgets (click/keypress etc)
// we define our package as follows, to avoid name-clashes with other projects in the "interwebs".
package com.myorganization.mainClass;
public class mainClass extends Applet {
// this is an override. The Applet template prescribes default behavior.
// but we want to draw our own stuff.
public void paint(Graphics g) {
// I decided to attempt and center the text within the applet.
// Notice the keyword "this", which refers to our 'mainClass', which..
// as you know, extends the Applet template -- which contains .getWidth & .getHeight methods.
And here is how it looks when pasted in a location suitable for reading: http://pastebin.com/1ikmvgdZ
Notice there is not a main method. There is not any stdargs. If you would like more help on where to go from here, or did understand my answer, you can email contact me I don't mind helping further. Good luck and enjoy. :)
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.