Java AWT (Abstract Windowing Toolkit) Is An API To Develop GUI or
Java AWT (Abstract Windowing Toolkit) Is An API To Develop GUI or
Java AWT (Abstract Windowing Toolkit) Is An API To Develop GUI or
Container
The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc. The classes that
extends Container class are known as container such as Frame,
Dialog and Panel.
Window
The window is the container that have no borders and menu bars.
You must use frame, dialog or another window for creating a
window.
Panel
The Panel is the container that does contain title bar and menu
bars. It can have other components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have
menu bars. It can have other components like button, textfield
etc.
Useful Methods of Component class
Method
Description
public
void
width,int height)
setSize(int
public
void
setLayout(LayoutManager m)
import java.awt.*;
class First2{
First2(){
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Next Top
Java Applet
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.
Advantage of Applet
There are many advantages of applet. They are as follows:
o
Secured
Drawback of Applet
o
Do You Know
Hierarchy of Applet
As displayed in the above diagram, Applet class extends Panel. Panel class extends
Container which is the subclass of Component.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle
methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized.
It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop
or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics
class object that can be used for drawing oval, rectangle, arc etc.
//First.java
2.
import java.applet.Applet;
3.
import java.awt.Graphics;
4.
5.
6.
7.
g.drawString("welcome",150,150);
8.
9.
10.
Note: class must be public because its object is created by Java Plugin software that
resides on the browser.
myapplet.html
1.
<html>
2.
<body>
3.
4.
</applet>
5.
</body>
6.
</html>
//First.java
2.
import java.applet.Applet;
3.
import java.awt.Graphics;
4.
5.
6.
7.
g.drawString("welcome to applet",150,150);
8.
9.
10.
11.
/*
12.
13.
</applet>
14.
*/
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac First.java
c:\>appletviewer First.java
Next TopicGraphics in applet
prevnext
Displaying
import java.applet.Applet;
2.
import java.awt.*;
3.
4.
5.
6.
7.
g.setColor(Color.red);
8.
g.drawString("Welcome",50, 50);
9.
g.drawLine(20,30,20,300);
10.
g.drawRect(70,100,30,30);
11.
g.fillRect(170,100,30,30);
12.
g.drawOval(70,200,30,30);
13.
14.
g.setColor(Color.pink);
15.
g.fillOval(170,200,30,30);
16.
g.drawArc(90,150,30,30,30,270);
17.
g.fillArc(270,150,30,30,0,180);
18.
19.
20.
myapplet.html
1.
<html>
2.
<body>
3.
4.
</applet>
5.
</body>
6.
</html>