Sunteți pe pagina 1din 5

Applet is a program to run on the browser and it is embedded on the web page.

This
program is not system level program but it is a network level program. The Applet class
is a super class of any applet. Applet viewer is used to view or test the applet whether
the applet is running properly or not.

In this program we will see how to draw the different types of shapes like line, circle and
rectangle. There are different types of methods for the Graphics class of the java.awt.*;
package have been used to draw the appropriate shape. Explanation of the methods used
in the program is given just ahead :

Graphics.drawLine() :
The drawLine() method has been used in the program to draw the line in the applet. Here
is the syntax for the drawLine() method :

drawLine(int X_from_coordinate, int Y_from_coordinate, int X_to_coordinate, int


Y_to_coordinate);

Graphics.drawString() :
The drawSring() method draws the given string as the parameter. Here is the syntax of the
drawString() method :

drawString(String string, int X_coordinate, int Y_coordinate);

Graphics.drawOval() :
The drawOval() method draws the circle. Here is the syntax of the drawOval() method :

g.drawOval(int X_coordinate, int Y_coordinate, int Wdth, int height);

Graphics.drawRect() :
The drawRect() method draws the rectangle. Here is the syntax of the
drawRect() method :

g.drawRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

Here is the java code of the program :.

import java.applet.*;
import java.awt.*;

public class CircleLine extends Applet{


int x=300,y=100,r=50;

public void paint(Graphics g){


g.drawLine(3,300,200,10);
g.drawString("Line",100,100);
g.drawOval(x-r,y-r,100,100);
g.drawString("Circle",275,100);
g.drawRect(400,50,200,100);
g.drawString("Rectangel",450,100);
}
}

Here is the HTML code of the program:

<HTML>
<HEAD>
</HEAD>
<BODY>
<div align="center">
<APPLET CODE="CircleLine.class" WIDTH="800" HEIGHT="500"></APPLET>
</div>
</BODY>

In this program we will show you how to draw the different types of colorful shapes. This
example show the different types of colorful shapes like line, circle and the rectangle. It
also displays the different - different colors such as red, magenta and yellow to be used in
those shapes.

In this program there are several types of methods have been used to fill or set the color
of line, text, circle background and rectangle background. These are given just ahead :

Graphics.setColor() :
This is the setColor() method which is the Graphics class method imported by the
java.awt.*; package. This method sets the color for the object by specified color. Here is
the syntax of the setColor() method :

g.setColor(Color.color_name);

Graphics.fillOval() :
This is the fillOval() method used to fill the color inside the oval by specified color. Here
is the syntax of the fillColor() method :

g.fillColor(Color.color_name);

Graphics.fillRect() :
This is the fillRect() method used to fill the color inside the rectangle by specified color.
Here is the syntax of the fillRect() method :

g.fillRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

Here is the java code of the program :.


import java.applet.*;
import java.awt.*;

public class ShapColor extends Applet{


int x=300,y=100,r=50;

public void paint(Graphics g){


g.setColor(Color.red); //Drawing line color is red
g.drawLine(3,300,200,10);
g.setColor(Color.magenta);
g.drawString("Line",100,100);

g.drawOval(x-r,y-r,100,100);
g.setColor(Color.yellow); //Fill the yellow color in circle
g.fillOval( x-r,y-r, 100, 100 );
g.setColor(Color.magenta);
g.drawString("Circle",275,100);

g.drawRect(400,50,200,100);
g.setColor(Color.yellow); //Fill the yellow color in rectangel
g.fillRect( 400, 50, 200, 100 );
g.setColor(Color.magenta);
g.drawString("Rectangel",450,100);
}
}

Here is the HTML code of the program:

<HTML>
<HEAD>
</HEAD>
<BODY>
<div align="center">
<APPLET ALIGN = "CENTER" CODE = "ShapColor.class" WIDTH = "800">
HEIGHT = "500"></APPLET>
</div>
</BODY>
</HTML>

Java applet has the feature of retrieving the parameter values passed from the html page.
So, you can pass the parameters from your html page to the applet embedded in your
page. The param tag(<parma name="" value=""></param>) is used to pass the
parameters to an applet. For the illustration about the concept of applet and passing
parameter in applet, a example is given below.

In this example, we will see what has to be done in the applet code to retrieve the value
from parameters. Value of a parameter passed to an applet can be retrieved using
getParameter() function. E.g. code:
String strParameter = this.getParameter("Message");

Printing the value:

Then in the function paint (Graphics g), we prints the parameter value to test the value
passed from html page. Applet will display "Hello! Java Applet" if no parameter is
passed to the applet else it will display the value passed as parameter. In our case applet
should display "Welcome in Passing parameter in java applet example." message.

Here is the code for the Java Program :

import java.applet.*;
import java.awt.*;

public class appletParameter extends Applet {


private String strDefault = "Hello! Java Applet.";
public void paint(Graphics g) {
String strParameter = this.getParameter("Message");
if (strParameter == null)
strParameter = strDefault;
g.drawString(strParameter, 50, 25);
}
}

Here is the code for the html program :

<HTML>
<HEAD>
<TITLE>Passing Parameter in Java Applet</TITLE>
</HEAD>
<BODY>
This is the applet:<P>
<APPLET code="appletParameter.class" width="800" height="100">
<PARAM name="message" value="Welcome in Passing parameter in java applet
example.">
</APPLET>
</BODY>
</HTML>

This program illustrates you to display image in an applet which has been done in this
example. In this program you will see that how many methods, classes, packages and it's
properties have been used to display the image in an applet.

In this program only one function paint(Graphics g) has used. Function paint(Graphics
g) is also a part of the life cycle of an applet in which anything you can draw in your
applet to appear on the browser. In this function MediaTracker class of the java.awt.*;
package, has been used. MediaTracker is a utility class that tracks the status of a number
of media objects. And this type of object can include images and audio clips. In this
program only the explanation about the adding images has been given. MediaTracker
class is used after creating the instance for that and calling the addImage() of the
MediaTracker class which is used in this program as you can see. In this program
method getImage() is used to return the image for the object ( img ) of the Image class
taking two arguments, first is getCodeBase() and another is image name. Then the
addImage() method of the MediaTracker has been used. Syntax of the addImage()
function is MediaTracker.addImage(img, x, y, x1, y1). Arguments of addImage()
function is explained below :

img - image name type of Image.


x - lower X - Coordinate type of int.
y - lower Y - Coordinate type of int.
x1 - upper X - Coordinate type of int.
y1 - upper Y - Coordinate type of int.

Here is the code of the program :

import java.applet.*;
import java.awt.*;

public class appletImage extends Applet{


Image img;
MediaTracker tr;
public void paint(Graphics g) {
tr = new MediaTracker(this);
img = getImage(getCodeBase(), "freelinuxcds.gif");
tr.addImage(img,0);
g.drawImage(img, 0, 0, this);
}
}

Try online this example.

S-ar putea să vă placă și