Java Manual 1,2and 6
Java Manual 1,2and 6
(310347)
1
Experiment No :
Date of Performance :
AIM: Write a program to demonstrate status of key on an Applet window such as KeyPressed,
KeyReleased, KeyUp, KeyDown
SOFTWARE: Windows Operating System, any IDE supporting java such as Netbeans etc.
THEORY:
Java Applet:
An applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works at client side. An applet is embedded in an HTML page using the APPLET
or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
Important points :
1. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run within either a web browser or an
applet viewer. JDK provides a standard applet viewer tool called applet viewer.
3. In general, execution of an applet does not begin at main() method.
4. Output of an applet window is not performed by System.out.println(). Rather it is handled
with various AWT methods, such as drawString().
It is important to understand the order in which the various methods shown in the above image
are called. When an applet begins, the following methods are called, in this sequence:
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of method calls takes place:
1. stop( )
2. destroy( )
Let’s look more closely at these methods.
1. init( ) : The init( ) method is the first method to be called. This is where you should
initialize variables. This method is called only once during the run time of your applet
.
2. start( ) : The start( ) method is called after init( ). It is also called to restart an applet after
it has been stopped. Note that init( ) is called once i.e. when the first time an applet is
loaded whereas start( ) is called each time an applet’s HTML document is displayed
onscreen. So, if a user leaves a web page and comes back, the applet resumes execution
at start( ).
3. paint( ) : The paint( ) method is called each time an AWT-based applet’s output must be
redrawn. This situation can occur for several reasons. For example, the window in which
the applet is running may be overwritten by another window and then uncovered. Or the
applet window may be minimized and then restored.
paint( ) is also called when the applet begins execution. Whatever the cause, whenever the
applet must redraw its output, paint() is called.
The paint( ) method has one parameter of type Graphics. This parameter will contain the
graphics context, which describes the graphics environment in which the applet is running.
This context is used whenever output to the applet is required.
4. stop( ) : The stop( ) method is called when a web browser leaves the HTML document
containing the applet—when it goes to another page, for example. When stop( ) is called, the
applet is probably running. You should use stop( ) to suspend threads that don’t need to run
when the applet is not visible. You can restart them when start( ) is called if the user returns to
the page.
5. destroy( ) : The destroy( ) method is called when the environment determines that your
applet needs to be removed completely from memory. At this point, you should free up any
resources the applet may be using. The stop( ) method is always called before destroy( ).
Changing the state of an object is known as an event. For example, click on button, dragging
mouse etc. The java.awt.event package provides many event classes and Listener interfaces for
event handling.
Flow of Event Handling
1. User Interaction with a component is required to generate an event.
2. The object of the respective event class is created automatically after event generation, and
it holds all information of the event source.
3. The newly created object is passed to the methods of the registered listener.
4. The method executes and returns the result.
The Java Key Listener is notified whenever you change the state of key. It is notified
against KeyEvent. The KeyListener interface is found in java.awt.event package, and it has
three methods. Interface declaration:
Following is the declaration for java.awt.event.KeyListener interface:
CONCLUSION:
2
Experiment No :
Date of Performance :
EXPERIMRNT N0 2
AIM: Write a program to create a frame using AWT. Implement mouseClicked, mouseEntered()
and mouseExited() events. Frame should become visible when the mouse enters it.
SOFTWARE REQUIRED: Windows Operating System, any IDE supporting java such as
Netbeans etc
THEORY:
Events:
an event is an object that describes a state change in a source. Among other causes, an event can
be generated as a consequence of a person interacting with the elements in a graphical user
interface. Some of the activities that cause events to be generated are pressing a button, entering
a character via the keyboard, selecting an item in a list, and clicking the mouse.
Event Sources :
A source is an object that generates an event. This occurs when the internal state of that object
changes in some way. Sources may generate more than one type of event.
A source must register listeners in order for the listeners to receive notifications about a specific
type of event. Each type of event has its own registration method.
The method that registers a mouse motion listener is called addMouseMotionListener( ). When
an event occurs, all registered listeners are notified and receive a copy of the event object. This is
known as multicasting the event. In all cases, notifications are sent only to listeners that register
to receive them.
Event Listeners:
A listener is an object that is notified when an event occurs.
It has two major requirements. First, it must have been registered with one or more sources to
receive notifications about specific types of events. Second, it must implement methods to
receive and process these notifications. In other words, the listener must supply the event
handlers. The methods that receive and process events are defined in a set of interfaces, such as
those found in java.awt.event. For example, the MouseMotionListener interface defines two
methods to receive notifications when the mouse is dragged or moved. Any object may handle
one or both of these events if it provides an implementation of this interface
CONCLUSION:
Experiment No : 6
Date of Performance :
EXPERIMENT NO 6
AIM: Write a program to demonstrate the use of InetAddress class and its factory methods.
SOFTWARE REQUIRED: Windows Operating System, any IDE supporting java such as
Netbeans etc
THEORY:
Java InetAddress class
An IP address is an address having information about how to reach a specific host which is a
32-bit unique address number having an address space of 2^32. InetAddress class is a
representation of an IP address. It represents both the 32-bit IPv4 address and the 128-bit IPv6
address. It is the superclass of Inet6Address and Inet4Address classes. An instance of this class
consists of an IP address and usually a hostname depending on whether hostname resolution
was performed during the creation
Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods
to get the IP of any host name for example www.javatpoint.com, www.google.com,
www.facebook.com, etc.
1. IPv4
IPv4 is the primary Internet protocol. It is the first version of IP deployed for production in the
ARAPNET in 1983. It is a widely used IP version to differentiate devices on network using an
addressing scheme. A 32-bit addressing scheme is used to store 232 addresses that is more than 4
million addresses.
Features of IPv4:
o It is a connectionless protocol.
o It utilizes less memory and the addresses can be remembered easily with the class based
addressing scheme.
PES’s Modern College of Engineering,
Department of Electronics & Computer Engineering. 10
Advanced Java Programming Lab T E (ECE)
(310347)
o It also offers video conferencing and libraries.
IPv6
IPv6 is the latest version of Internet protocol. It aims at fulfilling the need of more internet
addresses. It provides solutions for the problems present in IPv4. It provides 128-bit address space
that can be used to form a network of 340 undecillionth unique IP addresses. IPv6 is also identified
with a name IPng (Internet Protocol next generation).
Features of IPv6:
TCP/IP Protocol
o TCP/IP is a communication protocol model used connect devices over a network via
internet.
o TCP/IP helps in the process of addressing, transmitting, routing and receiving the data
packets over the internet.
o The two main protocols used in this communication model are:
1. TCP i.e. Transmission Control Protocol. TCP provides the way to create a
communication channel across the network. It also helps in transmission of packets
at sender end as well as receiver end.
2. IP i.e. Internet Protocol. IP provides the address to the nodes connected on the
internet. It uses a gateway computer to check whether the IP address is correct and
the message is forwarded correctly or not.
CONCLUSION: