Friday 16 June 2017

How to Create a Basic Java Applet in NetBeans

Program:

package JavaCoders;
/**
 *
 * @author Bsb
 */
import java.applet.Applet;
import java.awt.Graphics;
public class applet_example extends Applet

{
    public void paint(Graphics g)
    {   
      g.drawString("HELLO BALWANT",60,50);
      g.fillOval(100,100,50,80);
      g.fillRect(10, 50, 50, 100);
    }
    
}

In this program, you will be display some text and shapes on the applet. The first thing to remember is that you must have to import package called applet and also you have to import awt(Abstract Window Toolkit) package. After importing package of applet you have to extends the class named Applet. In applet there is a method named paint() method which is used to display data on the applet.In method you will be required to create object of Graphics. After creating object different methods can be easily called or used for drawing. There are different methods, some of which are:

1. drawString: This method is used to draw a String. String can be anything. You can type anything you want. Along with the String given you have to specify the coordinates of x and y axis.

2. drawLine: This method is used to draw a Line. You have to give coordinates of the line which sets the x and y coordinates and the length of the line.

3. drawRect: This method is used to draw a Rectangle. You also have to give coordinates of the Rectangle.

4. drawOval: This method is used to draw a Oval. You have to give coordinates of x and y and also the radius of the circle you want to draw.
5. drawArc: This method is used to draw an Arc.

6. fillRect: This method is used to draw a Rectangle that is filled with color. You have to set Color you want in your Rectangle otherwise it will fill black by default.

7. fillOval: This method is used to draw a Oval that is filled with color. You have to set Color you want in your Oval otherwise it will fill black by default.

If you copy and paste this code in your NetBeans IDE this code will run and an applet window is shown as given below. If you paste in your text editor and compile, the code will compile and if you want to run code you have to use appletviewer for displaying applet window. If you want to set color you can use g.setColor(Color.red); change color according to your need.

Output:

sCreate-a-basic-java-applet
Fig: Output showing applet in java

Displaying Applet in Browser:


If you want to display your applet on web browser, it can be possible with the help of applet tag in html code. For this you have to compile the code, than the class file obtained after compiling is to be inserted in the code attribute of applet tag. You can set the width and height of the applet to be displayed and also alt(alternate) tag can be used in applet tag. One thing to remember to display the applet on browser is that all the code i.e. class file and the html file must be contained in same folder.


<html>
<body>
<applet code="
applet_example.class" width="300" height="300">
</applet>
</body>
</html>


If you want to make form with the help of Swings, you can check:

https://wingsofm.blogspot.in/2017/05/registration-form-using-swings-in-java.html

No comments:

Post a Comment

Oracle / PLSQL: FOR LOOP

set serveroutput on; declare a1 varchar2(100); begin select count(*) into a1 from employees; for i in 1..a1 loop dbms_output.put_line...