How to Create a Java Applet in Netbeans
Netbeans is an IDE for Java,
PHP and Apache. IDE, which stands for Integrated Development
Environment, is basically a fancy way of saying "editor." Using
Netbeans, you can write Java code and run Java programs quickly and
easily. This is in contrast to running your Java programs from the
Terminal and writing them in Notepad, which can become tedious and can
be quite complicated. If you do not have Netbeans, you can install it
using the link in the Resources section below.
Instructions
-
-
1
In Netbeans, create a new project by going to "File > New Project." In the "Choose Project" window, click "Java Application" and click "Next." Name the project "FirstApplet" and click "Finish."
-
2
Under "Source Packages" double-click the "firstapplet" package (with the brown box icon) and click "New > Java Class." Name the class "GreenLines" and click "Finish."
-
3
Under the line "package firstapplet;" type the following two lines of Java code:
import java.applet.*;
import java.awt.*;
This imports the Applet and Graphics Java libraries, which you will use in your code. -
4
Between "public class GreenLines" and the "{" type "extends Applet" to tell Java that the GreenLines class is going to be an Applet.
-
5
Inside the GreenLines class, initialize the height and width variables for your applet by typing the line:
int m_height, m_width; -
6
Create the following two methods ("init" and "paint) to have your Java code initialize the Applet with a height, width and background color, then "paint" 10 lines across the screen:
public void paint(Graphics m) {
m.setColor(Color.black);
for (int i = 0; i < 10; ++i) m.drawLine(m_width, m_height, i * m_width / 10, 0);
}
public void init() {
m_width = getSize().width;
m_height = getSize().height;
setBackground(Color.green);
} -
7
Click "Run" from the top menu of Netbeans, then click "Run File." Your Java Applet's window will pop up and you will see 10 lines drawn across the screen.
-
1