4. Prepared By: Asst. Prof. Sejal Jadav
Q-3 Explain Applet life cycle.
• Each applet has a life cycle. So every time an applet is
initialized it goes through four states and finally after its
execution the applet is destroyed.
• At those state the applet calls a set of methods in an order.
5. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
6. Prepared By: Asst. Prof. Sejal Jadav
• These methods are init(), start(), stop() and destroy().
• Those are methods defined by the applet class and one can override
these methods as needed.
• In addition to those four methods the paint() method is another
method which is in the Component class of java.awt package.
• Those methods are described below.
7. Prepared By: Asst. Prof. Sejal Jadav
The init() method:
• The init() method is the first method called by an applet.
• In this method the initialization process in done.
• The variables, objects, background colors, fonts etc. are initialized in
this method.
• This method is called only once after an applet is created since
initialization should be one only once.
8. Prepared By: Asst. Prof. Sejal Jadav
Its syntax is:
public void init()
{
//initialized process
}
9. Prepared By: Asst. Prof. Sejal Jadav
The start() method:
• After the init() method, start() method is called.
• This method start the applet. It is also called to restart the applet if it is
stopped.
• This method can be called more than once since it needs to restart each time
it is stopped.
• In a browser if you leave the applet page, the applet is stopped and when you
again come back to that page it is restarted. Or when the applet window is
minimized, it is stopped and as it is restored, the applet is restarted.
10. Prepared By: Asst. Prof. Sejal Jadav
The syntax of start) method is:
public void start()
{
//the code to start applet
}
11. Prepared By: Asst. Prof. Sejal Jadav
The paint() method:
• The paint() method is of the component class which is in the java.awt
package.
• This method is called when the applet execution starts or the contents
of applet needs to be redrawn.
• The paint() method takes a parameter of type graphic class. This
graphic object is used to draw the text or any shapes etc. in the
applet window.
12. Prepared By: Asst. Prof. Sejal Jadav
• Its syntax is:
public void paint(Graphic g)
{
//code to display in the applet
}
13. Prepared By: Asst. Prof. Sejal Jadav
The stop() method:
• The stop() method is called when you minimize the applet window or
leave the applet page and go to another page in the browser.
• If you are working with thread then you should block or suspend the
thread when applet stops.
• After stopping an applet it can be restarted by the start() method
14. Prepared By: Asst. Prof. Sejal Jadav
• Its syntax is:
public void stop()
{
//statements……
}
15. Prepared By: Asst. Prof. Sejal Jadav
The destroy() method:
• The destroy() method is called when your applet is to be terminated
and needs to removed from the memory.
• In this method you should do the cleanup of the object and other
resource.
• The destroy() method is called after the stop() method and is
called only once.
16. Prepared By: Asst. Prof. Sejal Jadav
Its syntax is:
public void destroy()
{
//code for finalization…….
}
18. Prepared By: Asst. Prof. Sejal Jadav
Q-4 Explain Applet Class
• Applets are created using the applet class which is in java.applet
package.
• To create applet program, you must extend the applet class.
• The methods of applet class are listed below.
19. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
METHOD Description
void init() Initialize this applet
void start() Starts this applet
void stop() Stops this applet
void destroy() Destroys this applet
AppletContext getAppletContext() returns the Applet Context
String getAppletInfo() It returns a string containing the applet
information.
URL getCodeBase() It returns the URL (path) of the applet
class file.
URL getDocumentBase() It returns the URL of the HTML
document that executes the applet.
21. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
String getParameter(String paramName) It returns the value of parameter
paramName.
Boolean isActive() It returns true if the applet has been
started and returns false if the applet
has been stopped.
void resize(int width,int height) It resize the applet according to the
dimensions specified by width and
height.
void showStatus(String status) It display the status in the bar of the
browser or applet viewer. If the browser
does not support a status window,
nothing happens.
23. Prepared By: Asst. Prof. Sejal Jadav
Playing Audio Example
public interface AudioClip
• The AudioClip interface is a simple abstraction for playing a sound clip.
Multiple AudioClip items can be playing at the same time, and the
resulting sound is mixed together to produce a composite.
24. Prepared By: Asst. Prof. Sejal Jadav
There some Method Detail:
• Example : PlaySoundApplet folder
void play() Starts playing this audio clip.
the clip is restarted from the beginning.
void loop() Starts playing this audio clip in a loop.
void stop() Stops playing this audio clip.
25. Prepared By: Asst. Prof. Sejal Jadav
Explain Graphic class.
• The graphic class is in the java.awt package and it contains
several methods that can help to perform graphics related
operations in the applet.
• Those methods include drawing text, lines and many different
shapes. These methods are:
26. Prepared By: Asst. Prof. Sejal Jadav
• 1.void drawString(String msg, int x, int y)
• The msg is displayed at the specified location specified by the x and y
coordinates.
• Ex:myapplet.java
27. Prepared By: Asst. Prof. Sejal Jadav
2.void drawLine (int x1, int y1, int x2, int y2)
• This method draws a line joining the points (x1,y1) and (x2,y2).
• EX: shapeEx1.java
28. Prepared By: Asst. Prof. Sejal Jadav
3.void drawRect (int x, int y, int width, int height)
• This method draws a rectangle from the upper-left corner at the point
(x,y) of the width and height specified by width and height. The
rectangle is filled with the current color.
• Ex:shapeEx3.java
29. Prepared By: Asst. Prof. Sejal Jadav
4.void fillRect (int x, int y, int width, int height)
• This method draws a rectangle from the upper-left corner at the point
(x,y) of the width and height specified by width and height. The
rectangle is filled with the current color.
• Ex:shapeEx4.java
30. Prepared By: Asst. Prof. Sejal Jadav
5.void drawOval (int x, int y, int width, int height)
• This method draws an oval. The top-left corner of rectangle is the point
(x,y) and its width and height are specified by width and height. To
draw a circle the width and height should be same.
• Ex:shapeEx5.java
31. Prepared By: Asst. Prof. Sejal Jadav
6.void fillOval (int x, int y, int width, int height)
• It draws and fills the oval with the color.
• Ex:shapeEx6.java
32. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
7.void drawArc ( int x, int y, int width, int height, int startangle,
int endAngle )
• This method draws an arc which is bounded by the rectangle. The
rectangle’s top-left corner is point (x,y) and its width and height are
specified by width and height. The arc is drawn from the startangle
to the endAngle. Angles are is degrees.
• Ex:shapeEx7.java
33. Prepared By: Asst. Prof. Sejal Jadav
8.void fillArc ( int x, int y, int width, int height, int startAngle, int
endAngle)
• It draw and fills the arc with the color.
• Ex:shapeEx7.java
34. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
9.void drawPolygon ( int x[], int y[], int numOfPoints)
• This method draws a polygon of numOfpoint angles.
• The polygon’s endpoints are specified by the x and y arrays.
• It simply joins the points specified by the array specified by the
array elements.
• Means it joins the point ( x[0],y[0]) to the point (x[1],y[1]),then
point ( x[2],y[2]) is joined to point (x[1],y[1]) and so on.
35. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
drawPolygon ( int x[], int y[], int numOfPoints)
• Ex:shapeEx8.java
36. Prepared By: Asst. Prof. Sejal Jadav
• 10. void fillPolygon (int x[], int y[], int numOfPoints)
• It draws and fills the polygon with the color.
38. Prepared By: Asst. Prof. Sejal Jadav
Layout Managers
• The Layout manager decides how the control should be
arranged or positioned within a container or a window. It does
so by using some algorithm or you can say a method.
• In some languages like visual basic (VB) you can arrange your
controls as you like using the form designer.
39. Prepared By: Asst. Prof. Sejal Jadav
• But in Java there is no such form designer, but you have to write the
code to arrange the controls where you want them.
• Some editors allow contain the layout tool that allows you to position
your components like visual Basic. But it is necessary for us to know
how this layout manager works.
40. Prepared By: Asst. Prof. Sejal Jadav
• To set the layout following method is used.
void setLayout (LayoutManager obj)
• The obj is an object of the specified layout manager.
• If no layout manager is set by the setLayout() method, the
default layout manager is used which is the FlowLayout.
41. Prepared By: Asst. Prof. Sejal Jadav
(1)FlowLayout
• The FlowLayout is the default layout manager. i.e if no layout
manager is set by the setLayout() method, this layout is used to
arrange the controls in a container.
• In this layout manager the components are arranged from the
upper-left corner to the right side edge of the container. When
there is no space for the component it is arranged from the next
line. A little space is added between each component at all of the
four sides.
42. Prepared By: Asst. Prof. Sejal Jadav
The constructors for this class are:
• FlowLayout()
• FlowLayout(int align)
• FlowLayout(int align, hspace, vspace)
• Here the align parameter specified the alignment for the components. Like:
• FlowLayout(FlowLayout.RIGHT)
• FlowLayout(FlowLayout.CENTER)
• FlowLayout(FlowLayout.LEFT)
43. Prepared By: Asst. Prof. Sejal Jadav
• The default is FlowLayout.CENTER
• The hspace and vspace specifies the horizontal and vertical space
between the components respectively.
• The default space is 5 pixel from both horizontal and vertical.
• Example: FlowLayoutEx.java
44. Prepared By: Asst. Prof. Sejal Jadav
(2)Border Layout
• The BorderLayout arranges the components in five portions.
• These portions are the four edges of the window and the fifth is the
center part.
• These portions are known as north, south, east, west and center.
• These portions are specified by the following constants.
45. Prepared By: Asst. Prof. Sejal Jadav
• BorderLayout.NORTH: The top edge of the window.
• BorderLayout.SOUTH: The bottom edge of the window.
• BorderLayout.EAST: The right side edge of the window.
• BorderLayout.WEST: The left side edge of the window.
• BorderLayout.CENTER: The remaining center portion of the window.
46. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
47. Prepared By: Asst. Prof. Sejal Jadav
• Constructors for BorderLayout are:
• BorderLayout()
• BorderLayout(int hspace,int vspace)
• Here hspace and vspace specify the horizontal and vertical spaces between the
component.
• After creating the BorderLayout object you can add components by the following
method:
void add(Component obj , Object where)
48. Prepared By: Asst. Prof. Sejal Jadav
Method is:
• void add(Component obj , Object where)
For Example:
pane.add(new JButton("NORTH"),BorderLayout.NORTH);
pane.add(new JButton("SOUTH"),BorderLayout.SOUTH);
pane.add(new JButton("EAST"),BorderLayout.EAST);
pane.add(new JButton("WEST"),BorderLayout.WEST);
50. Prepared By: Asst. Prof. Sejal Jadav
(3)GridLayout
• The GridLayout class creates a layout which has a grid of rows and
columns.
• The constructors for GridLayout are:
• GridLayout()
• GridLayout( int rows, int columns)
• GridLayout( int rows, int columns, int hspace, int vspace)
51. Prepared By: Asst. Prof. Sejal Jadav
• The first constructor is the default constructor which will
create only one column grid.
• Here rows and columns specify the number of rows and
columns of the grid and the hspace and vspace specify the
horizontal and vertical spaces between the components.
53. Prepared By: Asst. Prof. Sejal Jadav
Introduction to BoxLayout [only Theory]
• The BoxLayout is used to arrange the components either vertically or
horizontally.
• For this purpose, BoxLayout provides four constants.
• They are as follows:
• Note: BoxLayout class is found in javax.swing package.
54. Prepared By: Asst. Prof. Sejal Jadav
• Fields of BoxLayout class:
1. public static final int X_AXIS
2. public static final int Y_AXIS
3. public static final int LINE_AXIS
4. public static final int PAGE_AXIS
55. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
• BoxLayoutExample1.java
56. Prepared By: Asst. Prof. Sejal Jadav
Introduction to SpringLayout [only Theory]
• A SpringLayout arranges the children of its associated container
according to a set of constraints. Constraints are nothing but horizontal
and vertical distance between two-component edges.
• Every constraint is represented by a SpringLayout.Constraint object.
57. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
58. Prepared By: Asst. Prof. Sejal Jadav
Introduction to GroupLayout [only Theory]
• The class GroupLayout hierarchically groups components in order to
position them in a Container
59. Prepared By: Asst. Prof. Sejal Jadav
• GroupLayout groups its components and places them in a Container
hierarchically.
• The grouping is done by instances of the Group class.
• Group is an abstract class, and two concrete classes which implement
this Group class are SequentialGroup and ParallelGroup.
60. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
61. Prepared By: Asst. Prof. Sejal Jadav
• After unit-5
• Concepts: Swing component’s
62. Prepared By: Asst. Prof. Sejal Jadav
(4)Card Layout
• The CardLayout has some special capabilities the other layout do not have.
• The CardLayout creates a layout like the playing cards.
• Assume more than one card on one another.
• Here only the card on the top is visible at a time. But you can shuffle the cards
to see other cards.
• Some way the CardLayout can switch among several panels.
63. Prepared By: Asst. Prof. Sejal Jadav
The constructors for CardLayout are:
• CardLayout()
• CardLayout(int hspace, int vspace)
• Here hspace and vspace specify the horizontal and vertical spaces
between the component.
• To add components or panels to the card Layout panel following
method is used.
64. Prepared By: Asst. Prof. Sejal Jadav
void add(Component obj,ObjectpanelName)
• Here the panelName is the name of the panel object.
• After Defining the panel object you can show or shuffle the card panels
by one or following methods.
65. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Methods Description
void show(Container panelobj, String panelName) It is show the panel specified by the
panelobj and whose name is panelName.
void first(Container panelObj) It shows the first card i.e. panel.
void last(Container panelObj) It shows the last card i.e. panel.
void next(Container panelObj) It shows the next card i.e. panel.
void previous(Container panelObj) It shows the previous card i.e. panel.
66. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav