Java - Applets & Graphics Programming
EXPLAIN : APPLET IN JAVA
LIST : METHODS OF APPLET
- public void init() : This Method is called for whatever initialization is needed for the Applet (It is invoked only once, It is invoked when the Application is launched using the "appletviewer" or a browser).
EXPLAIN : STEPS OF "CREATING" AN APPLET
- Import "java.applet.*;" (For the Applet Class from the "java.applet" Package)
EXPLAIN : STEPS OF "EXECUTING" AN APPLET
- Compile the Java code using "javac.exe" on console / command prompt (javac abc.java).
EXPLAIN : <APPLET> TAG
- The "<APPLET>" tag specifies an Applet. It is used for embedding a Java applet within an HTML document. This tells the Browser to load the Applet & How much space the Applet requires.
EXPLAIN : ATTRIBUTES OF "<APPLET>" TAG
- Code Base: It indicates the URL of the applet if the code attribute is relative.
EXPLAIN : PASSING PARAMETERS TO APPLETS
- Java allows the users to pass user defined parameters to an Applet with the help of "<PARAM>" tags.
<APPLET
EXAMPLE : PASSING PARAMETERS TO APPLETS
JAVA File : DemoParam.java
EXPLAIN : DRAWLINE() METHOD
- The "drawLine()" of "java.awt.Graphics" Method is used to draw a line from x1y1 to x2y2 & Fills it with the current / default color.
EXPLAIN : DRAWRECT() & FILLRECT() METHOD
- An applet is a Java program that runs in a Web Browser, Which is transportable over a network from one system to another.
- An Applet is a Java Class that extends the "java.applet" class.
- A "main()" method is not invoked on an Applet. Thus, An Applet will not define the "main()" method.
- A "J.V.M" is required to view an Applet. The Java Virtual Machine can be a plug-in for the Web Browser or can be a separate Run-Time Environment.
COMPARE : APPLET V/S APPLICATION
- It is a small program. / It is a large program.
- It is executed on a Client Browser. / It is executed on a stand alone computer system.
- Applets are portable and can be executed on any "Java" supported system. / Applications need J.D.K, J.R.E & J.V.M installed on the Client's Machine.
- Applets are created by extending the "java.applet.*;". / Applications are created by the "public static void main (String args[])" method.
- Applets have 5 Built Methods which are automatically invoked on the occurrence or a specific event. / Applications have a single start point which is also referred as the main method.
- Example: public void start() {...} / Example: public static void main (String args[])
LIST : METHODS OF APPLET
- public void init()
- public void start()
- public void stop()
- public void destroy()
- public void paint (Graphics g)
EXPLAIN : METHODS OF APPLET
- public void init() : This Method is called for whatever initialization is needed for the Applet (It is invoked only once, It is invoked when the Application is launched using the "appletviewer" or a browser).
- public void start() : This Method is called after the Browser calls the "init()" method (It is invoked when the Application is maximized, The default application state is in a maximized window. Thus, It will also be invoked on the launching)
- public void stop() : This Method is called when the User moves off the page on which the Applet sits (It is invoked when the application is minimized, It is also invoked when the Window is terminated while it's maximized state)
- public void destroy() : This Method is only called when the Browser shuts down normally (It is invoked when the Application is about to terminate, It can also be invoked when the Application is closed).
- public void paint(Graphics g) : This Method is invoked immediately after the "start()" method, And also any time when the Applet needs to re-paint itself in the Browser (It is invoked when the application is to be refreshed in terms of the GUI, It can also be invoked when we start, move or re-size an Applet)
EXPLAIN : STEPS OF "CREATING" AN APPLET
- Import "java.applet.*;" (For the Applet Class from the "java.applet" Package)
- Create a Public Class (The Class name and the File name must be the same in case of a Public Class)
- Extend the Class from the Base Class Applet.
- Do not write the main method.
- Write the Applet Life Cycle methods (i.e: init, start, stop, destroy & paint)
EXPLAIN : STEPS OF "EXECUTING" AN APPLET
- Compile the Java code using "javac.exe" on console / command prompt (javac abc.java).
- To run the generated Class file, We have to write the "<APPLET>" tag in an additional file.
EXPLAIN : <APPLET> TAG
- The "<APPLET>" tag specifies an Applet. It is used for embedding a Java applet within an HTML document. This tells the Browser to load the Applet & How much space the Applet requires.
EXPLAIN : ATTRIBUTES OF "<APPLET>" TAG
- Code Base: It indicates the URL of the applet if the code attribute is relative.
- Code: The URL indicates to the Class of the Applet.
- Name: It defines a Unique Name for the Applet.
- Title: It displays the additional information to be displayed in the Tool Tip of the Mouse.
- Height: Defines the Height of the Applet.
- Width: Defines the Width of the Applet.
- VSpace: It describes the amount of White Space to be inserted above and below the Object.
- HSpace: It describes the amount of White Space to be inserted at the left & right of the Object.
EXPLAIN : PASSING PARAMETERS TO APPLETS
- Java allows the users to pass user defined parameters to an Applet with the help of "<PARAM>" tags.
- The <PARAM> tag has a NAME attribute which defines the name of the parameter and a VALUE attribute which specifies the value of the parameter.
- <PARAM> tags must be included between the <APPLET> and </APPLET> tags.
SYNTAX : APPLET
SYNTAX : APPLET
<APPLET
[CODEBASE = Code_Base_URL]
CODE = Applet_File_Name.class
[ALT = Alternate_Text]
[NAME = Applet_Instance_Name]
WIDTH = Pixels
HEIGHT = Pixels
[ALIGN = Alignment]
[VSPACE = Pixels]
[HSPACE = Pixels]
>
[<PARAM NAME = Name1 VALUE = Value1>]
[<PARAM NAME = Name2 VALUE = Value2>]
[<PARAM NAME = Name2 VALUE = Value2>]
. . . . . . .
[<PARAM NAME = NameN VALUE = ValueN>]
</APPLET>
EXAMPLE : PASSING PARAMETERS TO APPLETS
JAVA File : DemoParam.java
import java.applet.Applet;
import java.awt.Graphics;
public class DemoParam extends Applet
{
public void paint (Graphics g)
{
String str = getParameter("Hello, World!");
g.drawString(str, 100, 100);
}
}
HTML File : DemoParam.html
<APPLET
CODE = "DemoParam.class"
WIDTH = "300"
HEIGHT = "300"
>
<PARAM NAME = "Hello, World!" VALUE = "Welcome!">
</APPLET>
EXPLAIN : DRAWLINE() METHOD
- The "drawLine()" of "java.awt.Graphics" Method is used to draw a line from x1y1 to x2y2 & Fills it with the current / default color.
EXPLAIN : DRAWRECT() & FILLRECT() METHOD
- The "drawRect()" Method is used to draw a Rectangle of Width & Height whose upper left corner is x, y.
- The "fillRect()" Method is used to draw a Rectangle of Width & Height whose upper left corner is x, y & Fills it with the current / default color.
- obj.drawRect(x, y, width, height);
- obj.fillRect(x, y, width, height);
EXPLAIN : DRAWROUNDRECT() & FILLROUNDRECT() METHOD
- The "drawRoundRect()" Method is used to draw Rounded Rectangles with Rounded Edges, These 2 methods are similar to the "drawRect()" Method.
- The "fillRoundRect()" Method is used to draw Rounded Rectangles with Rounded Edges, It fills the Rectangles with current / default color.
- obj.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
- obj.drawFillRect(x, y, width, height, arcWidth, arcHeight);
EXPLAIN : DRAWOVAL() & FILLOVAL() METHOD
- The "drawOval()" Method draws a Circle or Oval to fit in a box of Width & Height whose upper left is x, y.
- The "fillOval()" Method draws a Circle or Oval to fit in a box of Width & Height whose upper left is x, y & Fills with the current / default color.
- The "fillOval()" Method draws a Circle or Oval to fit in a box of Width & Height whose upper left is x, y & Fills with the current / default color.
- obj.drawOval(x, y, width, height);
- obj.fillOval(x, y, width, height);
- obj.fillOval(x, y, width, height);
EXPLAIN : DRAWARC() & FILLARC() METHOD
- The "drawArc()" Method is used to draw Arcs using 6 arguments. The first 4 arguments are similar to the "drawOval()" Method and the last 2 represent the Starting Angle of the Arc & the number of Degrees around the Arc.
- Whereas, The "fillArc()" draws a Solid Arc with the same number of arguments.
- obj.drawArc(x, y, width, height, startAngle, arcAngle)
- obj.fillArc(x, y, width, height, startAngle, arcAngle)
EXPLAIN : DRAWPOLYGON()
- The "drawPolygon()" Method is used to draw a Polygon using the X & Y Co-ordinates
EXPLAIN : DRAWSTRING()
- The "drawString()" Method is used to write a Text of a String starting at the specified x, y location.
- obj.drawString(stringName, x, y);
- Different types of Fonts / Font Styles can be used to modify the String according to the User's needs.