2. ADVANCED JAVA PROGRAMMING
APPLET – BASICS
Applets in Java are small and dynamic internet-based programs. A Java Applet can be only
executed within the applet framework of Java. For an easy execution of applets, a restricted
‘sandbox’ is provided by the applet framework.
Generally, the applet code is embedded within an HTML page. The applet codes are
executed when the HTML pages get loaded into the Java-compatible web browsers.
4. ADVANCED JAVA PROGRAMMING
APPLET- SKELETON
Four of these methods—init( ), start( ), stop( ), and destroy( )—are defined by Applet. Another, paint( ), is
defined by the AWT Component class.
init() : init() is the first method to be called. This is where variable are initialized. This method is called
only once during the runtime of applet.
start() : start() method is called after init(). This method is called to restart an applet after it has been
stopped.
stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
destroy() : destroy() method is called when your applet needs to be removed completely from memory.
6. ADVANCED JAVA PROGRAMMING
REQUESTING REPAINTING HTML APPLET TAG
An applet writes to its window only when its paint( ) method is called by the AWT.
Whenever your applet needs to update the information displayed in its window, it simply
calls repaint( ).
The repaint( ) method is defined by the AWT. It causes the AWT run-time system to execute
a call to your applet’s update( ) method,
7. ADVANCED JAVA PROGRAMMING
REQUESTING REPAINTING HTML APPLET TAG
1.void repaint( )
This version causes the entire window to be repainted.
2.void repaint(int left, int top, int width, int height)
This version specifies a region that will be repainted.
3. void repaint(long maxDelay)
Here, maxDelay specifies the maximum number of milliseconds that can elapse before update( ) is
called.
4.void repaint(long maxDelay, int x, int y, int width, int height)
8. ADVANCED JAVA PROGRAMMING
PASSING PARAMETERS TO APPLETS
Parameters specify extra information that can be passed to an applet from the HTML page.
Parameters are specified using the HTML’s param tag.
Param Tag
The <param> tag is a sub tag of the <applet> tag. The <param> tag contains two
attributes: name and value which are used to specify the name of the parameter and the
value of the parameter respectively.
9. ADVANCED JAVA PROGRAMMING
GRAPHICS
All graphics are drawn relative to a window. This can be the main window of an applet, a
child window of an applet, or a stand-alone application window.
The origin of each window is at the top-left Coordinates 0,0. Coordinates are specified in
pixels. All output to a window takes place through a graphics context.
A graphics context is encapsulated by the Graphics class and is obtained in two ways:
It is passed to an applet when one of its various methods, such as paint() or update(), is
called.
It is returned by the getGraphics( ) method of Component.
10. ADVANCED JAVA PROGRAMMING
GRAPHICS
Methods of Graphics class
public abstract void drawString(String str, int x, int y): is used to draw the specified string.
public void drawRect(int x, int y, int width, int height): draw a rectangle with the specified
width and height.
public abstract void fillRect(int x, int y, int width, int height): is used to fill the rectangle
with the default color and specified width and height.
public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with
the specified width and height.
11. ADVANCED JAVA PROGRAMMING
FONT
Font is a class that belongs to the java.awt package. It implements the Serializable
interface. FontUIResource is the direct known subclass of the Java Font class.
It represents the font that are used to render the text. In Java, there are two technical
terms that are used to represent font are characters and Glyphs.
Types of Fonts in Java
Physical Fonts
Logical Fonts
12. ADVANCED JAVA PROGRAMMING
COLORCLASSES
The primary purpose of AWT Color is to allow developers to create new colors using Java
code using RGB (red, green, blue), RGBA (red, green, blue, alpha), or HSB (hue, saturation,
BRI components) packages.
The class contains two values - the code of the shade and the value of
opacity/transparency.
Color defines several constants (for example, Color.black) to specify a number of common
colors. We can also create your own colors, using one of the color constructors.
13. ADVANCED JAVA PROGRAMMING
SWING-JAPPLET
JApplet is a java swing public class designed for developers usually written in Java. JApplet is
generally in the form of Java bytecode that runs with the help of a Java virtual machine
(JVM) or Applet viewer from Sun Microsystems. It was first introduced in 1995.
JApplet can also be written in other programming languages and can later be compiled to
Java byte code.
Java applets can be executed on multiple platforms which include Microsoft Windows,
UNIX, Mac OS and Linux. JApplet can also be run as an application, though this would
require a little extra coding.
14. ADVANCED JAVA PROGRAMMING
JFRAME
The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class.
JFrame works like the main window where components like labels, buttons, textfields are
added to create a GUI.
create a JFrame
JFrame class has many constructors that are used to create a new JFrame. You can create a
JFrame using these methods:
JFrame(): This helps in creating a frame which is invisible.
JFrame(String Title): Helps in creating a frame with a title.
JFrame(GraphicsConfiguration gc): Creates a frame with blank title and the graphics
configuration of screen.
15. ADVANCED JAVA PROGRAMMING
JCOMPONENT DIFFERENCES BETWEEN COMPONENT
No. Java AWT Java Swing
1)
AWT components are platform-
dependent.
Java swing components are platform-independent.
2)
AWT components are heavyweight. Swing components are lightweight.
3)
AWT doesn't support pluggable look and
feel.
Swing supports pluggable look and feel.
16. ADVANCED JAVA PROGRAMMING
CONTAINER
An applet container is the environment that runs a Java applet and provides secure applet
execution. Examples include Web browsers and the applet viewer in Java's software
development kit (SDK).
Types of Containers
The Container class, like Component is an abstract class. It has two direct subclasses:
Panel, which is only used for grouping components;
Window, which is, as its name says, for creating and handling windows.
17. ADVANCED JAVA PROGRAMMING
ICONS
Swing introduces the concept of an icon for use in a variety of components.
The Icon interface and ImageIcon class make dealing with simple images extremely easy.
The Icon interface is very simple, specifying just three methods used to determine the size
of the Icon and to display it. Implementors of this interface are free to store and display the
image in any way, providing a great deal of flexibility.
18. ADVANCED JAVA PROGRAMMING
JLabel
The object of JLabel class is a component for placing text in a container. It is used to display
a single line of read only text. The text can be changed by an application but a user cannot
edit it directly. It inherits JComponent class.
Java JTextField
The object of a JTextField class is a text component that allows the editing of a single line
text. It inherits JTextComponent class.
19. ADVANCED JAVA PROGRAMMING
Java JButton
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It
inherits AbstractButton class.
Java JCheckBox
The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off
(false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ". It
inherits JToggleButton class.
20. ADVANCED JAVA PROGRAMMING
Java JRadioButton
The JRadioButton class is used to create a radio button. It is used to choose one option
from multiple options. It is widely used in exam systems or quiz.
It should be added in ButtonGroup to select one radio button only.
Java JComboBox
The object of Choice class is used to show popup menu of choices. Choice selected by user
is shown on the top of a menu. It inherits JComponent class.