Вы находитесь на странице: 1из 8

Applet->An applet is a java program that runs in a web browser.

An applet can be a fully functional java application


note->(1)Every applet should be public
(2)it should extend java.applet.Applet
(3)An Applet does not contain man function
ex-1>
import java.awt.*;
import java.applet.Applet;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 100, 100);
}
}
awt->it is a package that contains classes and interfaces for
creating gui application in java.
*Life Cycle of Applet->there are five method of life cycle of Applet
---------------------- All these method are optional
(1)public void init()->it is used for inetialization of Applet.it is
very first method that is invoked whenever an
applet is loaded.
(2)public void start()->it is also used for inetialization , whenever
user click on refresh , reload execution start
from start.
(3)public void paint(Graphics g)->it is used for drawing text,shapes and
image on applet
(4)public void stop()->this method is executed whenever user click on
stop button
(5)public void destroy()->this method is executed whenever user close
it browser.
*Graphics class->this class comes from java.awt package.this class contain
method for drawing text,shapes and images in Applet
the object of graphics class receives as an argument within pai
nt
-->drawLine(int x,int y,int x1,int y1);
-->drawRect(int x,int y,int width,int height);
-->drawArc(int x,int y,int width,int height,int startangle,int arcangle)

ex.2->import java.awt.*;
import java.applet.Applet;
public class ColorApplet extends Applet

{
Font f;
Font f1;
Font f2;
public void init()
{
f = new Font("Times Roman",Font.BOLD,20);
f1 = new Font("Courier",Font.ITALIC,20);
f2 = new Font("Helvetica",Font.PLAIN,20);
}
public void paint(Graphics g)
{
g.setColor(Color.gray);
g.setFont(f);
g.drawString(" Be happy . Be hopeful",30,30);
g.setColor(Color.blue);
g.setFont(f1);
g.drawString(" Be happy . Be hopeful",30,80);
g.setColor(Color.pink);
g.setFont(f2);
g.drawString(" Be happy . Be hopeful",30,110);
setBackground(Color.cyan);
}
}

ex.3->
import java.awt.*;
import java.applet.Applet;
public class DrawShapes extends Applet
{
public void paint(Graphics g)
{
g.drawString(" Some of the drawing objects ",40,20);
g.drawLine(40,30,200,30);
g.drawRect(40,60,70,40);
g.fillRect(140,60,70,40);
g.drawRoundRect(240,60,70,40,10,20);
g.fillRoundRect(40,120,70,40,10,20);
setBackground(Color.yellow);
setForeground(Color.red);
g.draw3DRect(140,120,70,40,true);
g.drawOval(240,120,70,40);
g.fillOval(40,180,70,40);
g.drawArc(140,180,70,40,0,180);
g.fillArc(240,180,70,40,0,-180);
}
}
ex.4->
import java.awt.*;
public class SmileApplet extends java.applet.Applet
{
public void paint(Graphics g)
{

Font f = new Font("Helvetica",Font.BOLD,20);


g.setFont(f);
g.drawString("Always keep smiling !!",50,30);
g.drawOval(60,60,200,200);
g.fillOval(90,120,50,20);
g.fillOval(190,120,50,20);
g.drawLine(165,125,165,175);
g.drawArc(110,130,95,95,0,-180);
g.drawLine(165,175,150,160);
}
}
-----------------------------------------------------------------------------Event handling in java->
-----------------------User
|
!
Action
|
!
Event ->Every Event in java is a class whenever user
|
take action an object of this class is created
|
!
Listener->Every Listener in java is an interface
|
!
Handlers->Handlers are the method declared by
Listener interface
Note->All event related classes comes from "java.awt.event" package.

Action perform in Applet->


-------------------------ex.1->import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.Rectangle;
public class Event2 extends Applet
{
String str=" ";
Rectangle c, r;
Polygon p;
int x,y;
int xs[] = {50,90,100,50};
int ys[]={150,150,200,200};
public void init()
{
c=new Rectangle(10,30,50,50);
r=new Rectangle(100,30,50,50);
p=new Polygon(xs,ys,4);
addMouseListener(new A());

}
class A extends MouseAdapter
{
public void mouseClicked(MouseEvent m)
{
x=m.getX();
y=m.getY();
int p=getShape(x,y);
System.out.println(" "+ x+ " "+y+" "+p);
if(p==1)
str="Circle";
else if(p==2)
str="Rectangle";
else if(p==3)
str="Polygon";
else
str="Blank Area";
repaint();
}
}
public int getShape(int x,int y)
{
if(c.contains(x,y))
return 1;
else if(r.contains(x,y))
return 2;
else if(p.contains(x,y))
return 3;
return (-1);
}
public void paint(Graphics g)
{
g.drawOval(10,30,50,50);
g.drawRect(100,30,50,50);
g.drawPolygon(xs,ys,4);
Font f1=new Font("MS Serif",Font.BOLD,20);
g.setFont(f1);
g.setColor(Color.red);
g.drawString(str,x,y);
}
}
----------------------------------------------------------------------------------------user interface->they act as a mediator b/w user and application
--------------- purpose->taking input/producing output
user interface can be classified as:
1->component
2->container
Component->these are child window that are placed over Container.
---------- All these Component are derived from "Component" class
these Component include->

1-Label
2-Button
3-TextField
4-TextArea
5-List
6-Scrollbar
7-CheckBox
Containers->these are the parent window that contains the Component
-----------the Container include
1-Applet
2-Frame
How to perform Action->
---------------------User
|
!
Click
|
!
Action
|
|
!
Action
|
!
public

on Button
Event

Listener->it is an interface
void actionPerformed(ActionEvent ae)

ex.2->
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Event2 extends Applet implements ActionListener
{
Label lbl1,lbl2,lbl3;
Button btn;
TextField tf1,tf2,tf3;
public void init()
{
lbl1 = new Label("Number 1:");
lbl2 = new Label("Number 2:");
lbl3 = new Label("Result");
btn = new Button("Sum");
btn.addActionListener(this);
tf1 = new TextField();
tf2 = new TextField();
tf3 = new TextField();
setLayout(null);
lbl1.setBounds(10,10,100,25);
lbl2.setBounds(10,40,100,25);
lbl3.setBounds(10,110,100,25);

tf1.setBounds(130,10,100,25);
tf2.setBounds(130,40,100,25);
tf3.setBounds(130,110,100,25);
btn.setBounds(10,70,100,25);
add(lbl1);
add(tf1);
add(lbl2);
add(tf2);
add(btn);
add(lbl3);
add(tf3);
}
public void actionPerformed(ActionEvent ae)
{
int num = Integer.parseInt(tf1.getText());
int num1 =Integer.parseInt(tf2.getText());
int num2 = num + num1;
Integer i = new Integer(num2);
tf3.setText(i.toString());
}
}
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class MyCheck extends Applet implements ActionListener
{
Checkbox c1,c2,c3,c4,c5,c6,c7,c8;
Label lbl1,lbl2;
CheckboxGroup fr;
TextArea ta;
Button btn;
public void init()
{
lbl1 = new Label("Vegetables");
c1 = new Checkbox("Lady Finger",true);
c2 = new Checkbox("Potato");
c3 = new Checkbox("Green Chilly");
c4 = new Checkbox("Ginger");
lbl2 = new Label("Fruits");
fr = new CheckboxGroup();
c5 = new Checkbox("Mango",true,fr);
c6 = new Checkbox("Apple",false,fr);
c7 = new Checkbox("Banana",false,fr);
c8 = new Checkbox("Grapes",false,fr);
ta = new TextArea(5,30);
btn= new Button("Result");
btn.addActionListener(this);
add(lbl1);
add(c1);
add(c2);
add(c3);
add(c4);
add(lbl2);
add(c5);
add(c6);

add(c7);
add(c8);
add(btn);
add(ta);
}
public void actionPerformed(ActionEvent ae)
{
String str=new String();
str = "Vegetables are :";
if(c1.getState())
str+="Lady Finger,";
if(c2.getState())
str+="Potato";
if(c3.getState())
str+="Green Chilly";
if(c4.getState())
str+="Ginger";
str+="\n";
str+="Fruit is :";
if(c5.getState())
str+="Mango";
else if(c6.getState())
str+="Apple";
else if(c7.getState())
str+="Banana";
else
str+="Grapes";
ta.setText(str);
}
}
*swing->(1)swing is a package for creating GUI for java application
(2)Swing was developed to provide a more sophisticated set of GUI compon
ents
than the earlier Abstract Window Toolkit (AWT).
(3)when we want to access the swing package we have to write such as
import javax.swing.*;
(4)swing is a part of JFC(java foundation classes) thats why all
component start with J.
ex->JButton , JLabel ,JTextField , JTextArea .....etc
(5)Swing, which is an extension library to the AWT, includes new and
improved components that enhance the look and functionality of GUIs
(6)Swing Java consists of
(A) Look and feel
(B) Accessibility
(c) Java 2D
(D) Drag and Drop, etc
(E) light weight component
ex->
import javax.swing.*;
import java.applet.Applet;
public class Event2 extends Applet

{
JLabel lbl1,lbl2,lbl3;
JButton btn;
JTextField tf1,tf2,tf3;
public void init()
{
lbl1 = new JLabel("Number 1:");
lbl2 = new JLabel("Number 2:");
lbl3 = new JLabel("Result");
btn = new JButton("Sum");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
setLayout(null);
lbl1.setBounds(10,10,100,25);
lbl2.setBounds(10,40,100,25);
lbl3.setBounds(10,110,100,25);
tf1.setBounds(130,10,100,25);
tf2.setBounds(130,40,100,25);
tf3.setBounds(130,110,100,25);
btn.setBounds(10,70,100,25);
add(lbl1);
add(tf1);
add(lbl2);
add(tf2);
add(btn);
add(lbl3);
add(tf3);
}
}
*diff b/w awt and swing->
(1)awt stands for abstract window toolkit
swing is also called JFC(java foundation classes)
(2)awt component are called heavy weight component
swing component are called light weight component
(3)awt component require java.awt package
swing component require javax.swing package
(4)awt component are plateform(OS) dependent
swing component are plateform independent
(5)look and feel feature is not supported in awt
swing has different look and feel
(6)awt has common dialog box.
swing has common and custom diialog box.

Вам также может понравиться