>>PREVIOUS>>
9. APPLET
Applet
An applet is a dynamic and interactive program that can run inside Web page displayed by a Java-capable browser or applet viewer.
All applets are subclasses of Applet. You should import java.applet and java.awt since all applets run in a window. Applet defines three interfaces Appletcontext, AppletStub and AudioClip.
/*
*/
Applet extends java AWT class Panel, Panel extends Container which extends Component.
The init( ) Method
This method gets called as soon an applet is started. Initialization of all variables, creation of objects, setting of parameters, etc. can be done in this method.
The start( ) method
This method is executed after the init mehod. Also used to restart the applet that was stoped.
The stop( ) method
This method is used to halt the running of an applet. This method is called when a web browser leaves the HTML document containing the applet.
The destroy( ) method
This method is used to free the memory occupied by the variables and objects initialized in the
applet. Called by the browser just before the applet is terminated.
The paint( ) method
This method helps in drawing, writing and creating a colored background or an image on to the applet. This method is called each time your applet’s output must be redrawn. It has one parameter called Graphics.
The repaint( ) method
This method is used in case an applet is to be repainted. The repaint method calls update( ) method to clear screen and paint( ) method to redraw the contents of the current frame.
resize (width, height)
Resize the applet window
showStatus (str)
Displays the string in the status window of the applet
When starting the applet init, start, paint methods and when terminating stop and destroy
methods are called.
9.a. The Graphics Class in java.awt package
drawString(message,x,y);
drawLine (x1,y1,x2,y2);
drawRect (x1,y1,width,height)
drawRoundRect (x1,y1,width,height,width1,height1)
draw3Drect (x1,y1,width,height,boolean)
drawPolygon (xs,ys,pts)
drawOval (x1,y1,width,height)
drawArc (x1,y1,widht,height,angle1,angle2)
fillRect (x1,y1,width,height)
fillRoundRect (x1,y1,width,height,width1,height1)
fillPloygon (xs,ys,pts)
fillOval (x1,y1,width,height)
fillArc (x1,y1,widht,height,angle1,angle2)
9.b. Font Class in java.awt package
Font f = new Font ("fontname", format, size);
Formats are Font.BOLD, Font.ITALIC, and Font.PLAIN
g.setFont(f)
9.c. Color Class in java.awt package
Color.grey, Color.green, Color.yellow, Color.pink, Color.red, Color.blue, Color.magenta,
Color.cyan
setColor (color)
getColor (color)
setBackground (color)
getBackground (color)
setForeground (color)
getForeground (color)
9.d. Images
getImage (URL,string)
drawImage (Image,x,y,imageObserver)
to find URL
getCodeBase ( ) can be used
To create image
createImage (width,height)
getGraphics ( )
Clipping
A technique by which the drawing area can be restricted to a small portion of the screen.
Method is clipRect( )
clipRect(x1,y1,x2,y2);
Animation
Animation is technique by the object is moved on the screen In which the original image is
clreared and placed in another place.
9.e. Events
Mouse Events methods
boolean mouseDown(event , x, y)
boolean mouseDrag(event , x, y)
boolean mouseEnter(event , x, y)
boolean mouseExit(event , x, y)
boolean mouseMove(event , x, y)
boolean mouseUp(event , x, y)
boolean mouseDown(event , x, y)
boolean mouseDown(event , x, y)
KeyBoard Events
boolean keyDown(event , x, y)
boolean keyUp(event , x, y)
Types of Event handling
a)Low Level event
Low level classes Low level event Listener
ComponentEvent ComponentListener
FocusEvent FocusListener
KeyEvent KeyListener
ContainerEvent ContainerListener
MouseEvent MouseListener
MouseMotionListener
WindowEvent WindowListener
InputEvent
b) Semantic Events
Low level classes Low level event Listener
ActionEvent ActionListener
AdjustmentEvent AdjustmentListener
ItemEvent ItemListener
TextEvent TextListener
Example:
import java.awt.*;
mport java.awt.event.*;
import java.applet.*;
public class mousetest extends Applet implements MouseListener
{
public void init( )
{
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
showStatus("Mouse clicked at " + e.getX( ) +" , " + e.getY( ));
}
// Write above code for other mouse events
}
Each component class in the AWT has one addXXXListener( ) method for each event type.
9.f. ABSTRACT WINDOW TOOLKIT (AWT)
Component Contructor Methods
Button
Button( ) setLabel(String)
Button("label") getLabel( )
Label
Label( ) getText( )
Label(String) setText(String)
Label(String, int) getAlignment( )
SetAlignment(int)
where Int is alignment. It may be Label.LEFT, Label.RIGHT, Label.CENTER
Checkbox
Checkbox( ) setLabel(string)
Checkbox(String) getLabel( )
Checkbox(String,grp,boolean) setState(boolean)
getLabel( )
Choice
Choice( ) getItem(int)
addItem(String) getItemcount( )
getSelectedItem( )
getSelectedIndex( )
TextComponent
TextField( ) getText( )
TextField(String, int) setText(String)
TextArea( )
TextArea(String, int, int) int represents rows and columns
List
List( ) getItem(int) int starts from 0
List(int,boolean) getItemCount( )
AddItem(String) select(int)
getSelectedItem( )
Scrollbar
Scrollbar( ) setValue(int)
Scrollbar(orient, value, visible, min, max) getValue( )
Layout Manger
A set of classes used to position the components in a container.
FlowLayout
BorderLayout
GridLayout First create instantiate a layout manager class and use setLayout( ) method
Flow Layout
Lays components linewise from left to right
FlowLayout( )
FlowLayout(align, hgap, vgap)
Align – FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout..CENTER
Grid Layout
Position the components in the cellf of the grid.
GridLayout(int rwo, int col)
GridLayout( int rwo, int col, int hgap, int vgap)
Border Layout
Lays components relative to the edges of the container
BorderLayout( )
BorderLayout(int hgap, int vgap)
add("direction",item);
direction may be NORTH, SOUTH,EAST , WEST or CENTER
Insets(int, int, int, int)
Used to give spacing around the container
Panel
A panel class is a non-abstract, recursively nestable container.
Panel( )
9.g. Frames, Menus and Dialogs
Frame
Inherited from Frame class
Frame( )
Frame(String)
Methods
setVisible(boolean)
setSize(Dim)
setLocation(int,int) getLocation( )
dispose()
setTitle(String) getTitle( )
Menus
Menubar( )
Menu(String)
MenuItem(String)
CheckboxMenuItem(String)
SetState(boolean) getState( )
menu.add(MenuItem)
menubar.add(menu)
Frame.setMenubar(menubar)
Dialog
Dialog(Frame,boolean)
Dialog(Frame,String,boolean)
setResizable(boolean)
isModal( )
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class fram extends Frame
{
String msg=" ";
fram()
{
super("Menu Frame");
MenuBar mb = new MenuBar();
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
MenuItem mi1 = new MenuItem("Open");
MenuItem mi2 = new MenuItem("Save");
MenuItem mi3 = new MenuItem("Copy");
MenuItem mi4 = new MenuItem("Paste");
m1.add(mi1);
m1.add(mi2);
m2.add(mi3);
m2.add(mi4);
mb.add(m1);
mb.add(m2);
setMenuBar(mb);
winadapt w = new winadapt(this);
addWindowListener(w);
mnuhandler handler = new mnuhandler(this);
mi1.addActionListener(handler);
mi2.addActionListener(handler);
mi3.addActionListener(handler);
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}
class mnuhandler implements ActionListener
{
fram f1;
public mnuhandler(fram f2)
{
f1 = f2;
}
public void actionPerformed(ActionEvent ae)
{
String msg;
String s = (String) ae.getActionCommand();
if(s.equals("Open"))
msg = "Open";
else if(s.equals("Save"))
msg ="Save";
else if(s.equals("Copy"))
msg = "Copy";
else
msg = "Paste";
msg = msg +" selected";
f1.msg=msg;
f1.repaint();
}
}
class winadapt extends WindowAdapter
{
fram f1;
public winadapt(fram f2)
{
f1 = f2;
}
public void windowClosing(WindowEvent e)
{
f1.dispose();
}
}
public class frammenu extends Applet
{
Frame f;
public void init()
{
f = new fram();
f.setSize(300,300);
f.setVisible(true);
}
}
>>>NEXT>>>
No comments:
Post a Comment