Graphical User Interface (GUI) Programming
Introduction to AWT
AWT stands for Abstract Window Toolkit. It is a portable GUI library between Solaris and Windows 95/NT. The Abstract Window Toolkit provides many classes for programmers to use. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT.
The AWT helps to create graphical component as textbox, Radio button, Command button, etc. which helps for data manipulation.
Applets
An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM).
How does applet differ from application? Write are the basic steps to create applets? [2066,2060,2064] [8M]
Ans:
Applet VS Application
1. Applets are web applications & small. 1.Applications are desktop application and large
2. Do not contain main function. 2. It contains main function
3. Applets can be embedded in HTML pages and downloaded over the Internet. 3. Applications have no special support in HTML for embedding or downloading.
4. It is used over a network. 4. It is used the local system resources.
5.Applets must import ‘AWT’ and ‘applet’ package. 5. Application do not need it.
6.Applets must have GUI 6. Applications mayn’t have GUI.
7. Applet starts execution after init() method 7. Applications start execution after main() method
Steps for creating applets
1. Edit a java source file
a. Initialize an applet.
b. Start the operation
c. Perform specified activities.
d. Stop applet.
2. Compile program
3. Execute the applet viewer, specifying the name applet’s source file.
Example:-
import java.awt.*;
import jave.applet.*;
public class appletDemo extends Applet
{
String msg = “ “;
public void Paint (Graphics g)
{
g.drawString(“Welcome !”,100,200);
}
public void start()
{
msg =msg +”Inside Start”;
}
public void init()
{
Msg=msg+ “Inside init”;
}
public void stop()
{
Msg= msg+ “Inside Stop”;
}
}
Next Example
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class SimpleApplet extends Applet{
String text = "I'm a simple applet";
public void init() {
text = "I'm a simple applet";
setBackground(Color.cyan);
}
public void start() {
System.out.println("starting...");
}
public void stop() {
System.out.println("stopping...");
}
public void destroy() {
System.out.println("preparing to unload...");
}
public void paint(Graphics g){
System.out.println("Paint");
g.setColor(Color.blue);
g.drawRect(0, 0,
getSize().width -1,
getSize().height -1);
g.setColor(Color.red);
g.drawString(text, 15, 25);
}
}
HTML tags
The applet tag is used to start an applet from both an HTML document and form an applet viewer. An applet viewer will execute each Applet tag that it finds in a separate window.
<APPLET>
Codebase=(pathof.classFile)
Alt=(AlternativeText)
Name=(AppletName)
Width=(pixels)
Height=(pixels)
Vspace=(VetricalSpace)
Hspace=(HorizontalSpace) </APPLET>
Passing Parameters to Applets
Parameters are passed to applets in NAME=VALUE pairs in
tags between the opening and closing APPLET tags. Inside the applet, you read the values passed through the PARAM tags with the getParameter() method of the java.applet.Applet class.
The program below demonstrates this with a generic string drawing applet. The applet parameter "Message" is the string to be drawn.
import java.applet.*;
import java.awt.*;
public class DrawStringApplet extends Applet {
private String defaultMessage = "Hello!";
public void paint(Graphics g) {
String inputFromPage = this.getParameter("Message");
if (inputFromPage == null) inputFromPage = defaultMessage;
g.drawString(inputFromPage, 50, 25);
}
}
You also need an HTML file that references your applet. The following simple HTML file will do:
This is the applet:
Build in methods in applet(Applet activities)
i. init()
ii. start()
iii. paint()
iv. stop()
v. destroy()
Web Page with Applet
Introduction to AWT
AWT stands for Abstract Window Toolkit. It is a portable GUI library between Solaris and Windows 95/NT. The Abstract Window Toolkit provides many classes for programmers to use. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT.
The AWT helps to create graphical component as textbox, Radio button, Command button, etc. which helps for data manipulation.
Applets
An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM).
How does applet differ from application? Write are the basic steps to create applets? [2066,2060,2064] [8M]
Ans:
Applet VS Application
1. Applets are web applications & small. 1.Applications are desktop application and large
2. Do not contain main function. 2. It contains main function
3. Applets can be embedded in HTML pages and downloaded over the Internet. 3. Applications have no special support in HTML for embedding or downloading.
4. It is used over a network. 4. It is used the local system resources.
5.Applets must import ‘AWT’ and ‘applet’ package. 5. Application do not need it.
6.Applets must have GUI 6. Applications mayn’t have GUI.
7. Applet starts execution after init() method 7. Applications start execution after main() method
Steps for creating applets
1. Edit a java source file
a. Initialize an applet.
b. Start the operation
c. Perform specified activities.
d. Stop applet.
2. Compile program
3. Execute the applet viewer, specifying the name applet’s source file.
Example:-
import java.awt.*;
import jave.applet.*;
public class appletDemo extends Applet
{
String msg = “ “;
public void Paint (Graphics g)
{
g.drawString(“Welcome !”,100,200);
}
public void start()
{
msg =msg +”Inside Start”;
}
public void init()
{
Msg=msg+ “Inside init”;
}
public void stop()
{
Msg= msg+ “Inside Stop”;
}
}
Next Example
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class SimpleApplet extends Applet{
String text = "I'm a simple applet";
public void init() {
text = "I'm a simple applet";
setBackground(Color.cyan);
}
public void start() {
System.out.println("starting...");
}
public void stop() {
System.out.println("stopping...");
}
public void destroy() {
System.out.println("preparing to unload...");
}
public void paint(Graphics g){
System.out.println("Paint");
g.setColor(Color.blue);
g.drawRect(0, 0,
getSize().width -1,
getSize().height -1);
g.setColor(Color.red);
g.drawString(text, 15, 25);
}
}
HTML tags
The applet tag is used to start an applet from both an HTML document and form an applet viewer. An applet viewer will execute each Applet tag that it finds in a separate window.
<APPLET>
Codebase=(pathof.classFile)
Alt=(AlternativeText)
Name=(AppletName)
Width=(pixels)
Height=(pixels)
Vspace=(VetricalSpace)
Hspace=(HorizontalSpace) </APPLET>
Passing Parameters to Applets
Parameters are passed to applets in NAME=VALUE pairs in
tags between the opening and closing APPLET tags. Inside the applet, you read the values passed through the PARAM tags with the getParameter() method of the java.applet.Applet class.
The program below demonstrates this with a generic string drawing applet. The applet parameter "Message" is the string to be drawn.
import java.applet.*;
import java.awt.*;
public class DrawStringApplet extends Applet {
private String defaultMessage = "Hello!";
public void paint(Graphics g) {
String inputFromPage = this.getParameter("Message");
if (inputFromPage == null) inputFromPage = defaultMessage;
g.drawString(inputFromPage, 50, 25);
}
}
You also need an HTML file that references your applet. The following simple HTML file will do:
This is the applet:
Build in methods in applet(Applet activities)
i. init()
ii. start()
iii. paint()
iv. stop()
v. destroy()
Web Page with Applet