Dr. D. Y.
Patil Unitech Society’s
         Dr. D. Y. Patil Institute of Technology
                             Pimpri, Pune
Department of Electronics and Telecommunication Engineering
                    Unit I: Applet
                304195 (C): Advanced JAVA Programming
                                    Third Year Semester II
                                                   Elective II
                                                  AY 2023-24
                                   Faculty: Armaan Shaikh
    Scheme and Structure                                                                                                      2
Course Objectives: Make the                                 Course Outcomes:
learner to:                                                  •    CO1: Design and develop GUI
                                                                  applications using Applets.
• Design and develop GUI                                     •    CO2: Apply relevant AWT/ swing
  applications using Abstract                                     components to handle the given event.
                                                                                                                  Credits: 03
  Windowing Toolkit (AWT), Swing                             •    CO3: Design and develop GUI
  and Event Handling.                                             applications using Abstract Windowing
                                                                  Toolkit (AWT), Swing and Event Handling.
• Design and develop Web
                                                             •    CO4: Learn to access database through       In-Sem (Th.): 30 marks
  applications                                                    Java programs, using Java Database
• Designing Enterprise based                                      Connectivity (JDBC)
  applications by encapsulating an                           •    CO5: Invoke the remote methods in an
                                                                  application using Remote Method            End-Sem (Th.): 70 marks
  application’s business logic.                                   Invocation (RMI)
• Designing applications using                               •    CO6: Develop program for client /server
  pre-built frameworks                                            communication using Java Networking        Companion Course:
                                                                  classes.
                                                                                                             Advanced JAVA Programming
                                                                                                             Lab
         Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri                  Armaan Shaikh (E&TC)
Course Contents                                                                                            3
1.   Unit     I: Applets
2.   Unit     II: Event Handling using AWT/Swing components
3.   Unit     III: GUI Programming
4.   Unit     IV: Database Programming using JDBC
5.   Unit     V: Remote Method Invocation (RMI)
6.   Unit     VI: Networking
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Reference Material                                                                                       4
• Text Books:
  1.    Herbert Schildt, “Java: The complete reference”, Tata McGraw Hill, 7th
        Edition
  2.    Jim Keogh, “Complete Reference J2EE” , Enterpr
  3.    E. Balaguruswamy, “Programming with JAVA: A Primer” McGraw Hill
        Education, India, 5th Edition.
• Reference Books:
  1.    “Java 6 Programming”, Black Book, Dreamtech
  2.    “Java Server Programming, Java EE6 (J2EE 1.6)”, Black Book, Dreamtech
  3.    M.T. Savaliya,“Advanced Java Technology”, Dreamtech
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
MOOC/NPTEL Courses                                                                                         5
1.   NPTEL Course “Programming in Java”
     • Link of the Course: https://nptel.ac.in/courses/106/105/106105191/
2.   Udemy course “Advanced Java Programming”
     • Link of the Course:
       https://www.udemy.com/course/advanced-java-programming
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
What is Applet?                                                      6
• Applet is a small program which-
   • can be placed on a web page
   • is run using the Applet Viewer or a Web
     Browser
   • can be transported over the Internet
   • can do things like performing arithmetic
     operations, display graphics, play sounds,
     accept user input, create animation, play
     interactive games, etc.
   Electronics and Telecomunication Engineering, DIT, Pimpri   Armaan Shaikh (E&TC)
  Types of Applet                                                                                           7
      Local Applet                                                    Remote Applet
• An applet developed                                   • An applet developed by
  locally and stored in                                   someone else and
  local system is known as                                stored in remote
  local applet.                                           computer is known as
                                                          remote applet.
• It does not require                                   • Requires downloading
  Internet.                                               and hence, Internet.
      Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
How Applets differ from Applications                                                                       8
1.   Applets are not full-featured application programs
2.   Usually designed for use on the Internet
3.   Applets do not use the main() method
4.   Applets cannot read from or write to the files in the local computer
5.   Applets cannot communicate with other servers on the network
6.   Applets cannot run independently. They are run from inside a Web page
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
      Life cycle of an Applet                                                                                    9
 The class Applet provides a set of methods to control and supervise the smooth execution of applets. These
 methods (also called life cycle methods) are called in a specific order during an applet’s entire life cycle.
1.   Instantiated or
     born
2.   Running
3.   Idle or
     Stopped
4.   Dead or
     destroyed
           Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Building an applet code                                                                                   10
• The applet code uses the services of two classes, namely, Applet and
  Graphics
• The Applet class which is contained in the java.applet package provides
  lifecycle methods for the applet
• The paint() method, when called, displays the result of applet code on
  screen. The output may be text, graphics or sound.
• The paint() method requires Graphics object as an argument which is
  contained in java.awt package
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Some Graphics methods                                                                                            11
• g.drawString("Hello", 20, 20);                                                                   Hello
• g.drawRect(x, y, width, height);
• g.fillRect(x, y, width, height);
• g.drawOval(x, y, width, height);
• g.fillOval(x, y, width, height);
• g.setColor(Color.red);
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri           Armaan Shaikh (E&TC)
Running applet in HTML                                                                                   12
• A Web page is basically made up of text and HTML tags that can be
  interpreted by a Web browser
• A Web page is marked by an opening HTML tag <HTML> and a closing
  </HTML> tag
• Two HTML tags are used to work with applets: <applet> and <param>
   • An applet is typically embedded in an HTML document using the <applet> tag.
   • The <param> tag is used to pass parameters to an applet
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
  Running applet in HTML                                                                                     13
• The <applet> tag takes three mandatory attributes: code, width, and
  height
• The code attribute specifies the class file for the applet to be initiated
<!-- HelloWorld.html -->
<html>
   <head><title>Applet Demo</title></head>
   <body>
       <applet code="HelloWorld" height=50 width=150 >
       </applet>
   </body>
</html>
       Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Running applet in HTML                                                                                     14
 • The <applet> tag takes several other optional attributes. It takes the
   following general form:
1. <appletcode='appletClassFile'
2. [codebase='URLOfCodeBase']
3. [alt='altervativeText']
4. [name='NameOfApplet']
5. width='width of Applet window in pixels'
6. height='height of Applet window in pixels'
7. [align='alignment']
8. [vspace='pixels']
9. [hspace='pixels']>
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Utility Methods                                                                                            15
The Applet class provides a set of useful methods (in addition to the
lifecycle methods)
 • isActive()
 • resize(int width,int height)
 • showStatus(String message)
 • getDocumentBase()
 • getCodeBase()
 • getImage(URL url, String name)
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
getCodeBase(), getDocumentBase()                                                                         16
• The getCodeBase() method returns the complete URL of the .class file
  that contains the applet
• The getDocumentBase() method returns the complete URL of the .html
  file that loaded the applet.
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Passing Parameters                                                                                       17
• We can supply user-defined parameters to an applet using <PARAM...> tags.
• Each tag has a name attribute such as color, and a value attribute such as
  red.
• To set up and handle parameters, we need to do two things:
  1.    Include appropriate <PARAM...> tags in the HTML document.
  2.    Provide Code in the applet to parse these parameters.
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Retrieving Parameters                                                                                    18
• Parameters are passed on an applet when it is loaded.
• We can define the init( ) method in the applet to get hold of the
  parameters defined in the <PARAM> tags.
• This is done using the getParameter( ) method
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Summary: Adding an Applet in HTML                                                                       19
  Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
JApplet                                                                                                   20
• As Swing is preferred to AWT, we can use JApplet that can have all the
  controls of swing. The JApplet class extends the Applet class.
   javax.swing.Japplet
• Note that the class, javax.swing.JApplet is a subclass of the Applet
 class
   • java.applet.Applet
         • javax.swing.JApplet
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Basic GUI Components                                                                                    21
  Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
TextField                                                                                                22
• A text field (represented by TextField
  class) is a basic component where user can
  type/edit a single line of text.
add(new Label("Name"));
add(new TextField());
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
TextArea                                                                                                  23
• A TextArea component has a display area
  that can show multi-line text.
• Another advantage of text area over text
  field is that when the text in the text area
  becomes larger than the viewable area,
  scroll bars can be enabled
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Button                                                                                                     24
• One of the common components found in a GUI is a
  push button which is represented by Button class.
• When a button is pushed/clicked typically an action is
  taken
• The Button class has two constructors.
public Button(String label);
string public Button();
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
ComboBox                                                                                                 25
• A JComboBox object represents a
  pop-up menu of items, one of
  which may be chosen at any time.
• The current choice is displayed as
  the title of the menu.
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Checkbox                                                                                                   26
• A Checkbox object can be in either of
  the two states: “on” (true) or “off”
  (false).
• It has a small rectangular area which
  remains empty for “off” state and shows
  a small tick mark (√) for “on” state.
• Clicking on a check box toggles its state.
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
JTabbedPane                                                                                              27
• JTabbedPane encapsulates a
  tabbed pane.
• It manages a set of components
  by linking them with tabs.
• Selecting a tab causes the
  component associated with that
  tab to come to the forefront.
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
  JScrollPane                                                                                                28
• JScrollPane is a lightweight container that
  automatically handles the scrolling of another
  component.
• The component being scrolled can be either an
  individual component or a group of components
• Here are the steps to follow to use a scroll pane:
  1.   Create the component to be scrolled.
  2.   Create an instance of JScrollPane, passing to it the object
       to scroll.
  3.   Add the scroll pane to the content pane.
       Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
 JTree                                                                                                     29
• A JTree is a sophisticated component that
  can display data hierarchy using tree.
• A JTree consists of nodes. The top-most
  node is called ‘root’. A node may have
  children nodes which, in turn, may have
  further children nodes and so on.
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
JTable                                                                                                   30
• The JTable class lets us create
  a tabular grid of data.
• JTable is a component that
  displays rows and columns of
  data
• It is also possible to select a
  row, column, or cell within the
  table, and to change the data
  within a cell
   Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
 ImageIcon                                                                                                 31
• Label Constructor:
   JLabel(String str, Icon icon, int align)
• ImageIcon implements Icon and encapsulates an
  image. Thus, an object of type ImageIcon can be
  passed as an argument to the Icon parameter of
  JLabel’s constructor.
     Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Excercise                                                                                                            32
                                                                                                  • Hint:
                                                                                                  g.drawOval
                                                                                                  g.fillOval
                                                                                                  g.setColor
  Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri                Armaan Shaikh (E&TC)
Quiz                                                                                                      33
• Which one of the following is a valid declaration of an applet?
(a) class AnApplet implements Applet {
(b) public class AnApplet extends applet implements Runnable {
(c) abstract class AnApplet extends java. applet.Applet {
(d) public class AnApplet extends java.applet. Applet {
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      34
• What is the difference between a Java Applet and a Java
  application program?
(a) All the methods in an applet class are private.
(b) Applets can create GUI, applications cannot.
(c) Applets are run in web browsers but applications are not.
(d) An application is a small program, whereas an applet is large.
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      35
• Which of the following packages must be imported to get the class
  Applet?
(a) java.applet.Applet
(b) java.util.*
(c) java.awt.*
(d) javax.swing.*
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
  Quiz                                                                                                      36
• Why does an applet have no main() method?
(a) The browser acts as the main. The applet provides methods for the browser.
(b) The paint() method is like the main method for an applet.
(c) Programs that do graphics do not need a main.
(d) Only simple programs need a main
      Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      37
• What is the function of the Graphics object?
(a) It represents the canvas of the applet and provides drawing methods.
(b) It represents the status bar.
(c) It represents the entire screen of the computer monitor.
(d) It represents the applet background.
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      38
• Which of the following sequences is the correct order of method
  call?
(a) start(), init(), paint(), stop(), destroy()
(b) init(), paint(), start(), stop(), destroy()
(c) init(), start(), paint(), stop(), destroy()
(d) start(), init(), paint(), destroy(), stop()
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      39
• Which of the following must be imported to get the graphics
  components?
(a) java.util.*
(b) java.Graphics
(c) java.awt.*
(d) java.lang.*
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      40
• Which of the following codes is used to display the string “Hello World!”
  at X=20 Y=50 location? Assume that g holds a Graphics object reference.
(a) g.println( “Hello World!” );
(b) drawString( “Hello World!”, 20, 50 );
(c) g.drawString( 20, 50, “Hello World!” );
(d) g.drawString( “Hello World!”, 20, 50 );
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      41
• Can the source code for your applet be compiled by the usual javac
  compiler?
(a) No, because applets have no main() method.
(b) Yes, if you are going to run it from the DOS prompt.
(c) Yes, an applet is just another class as far as the compiler is concerned.
(d) No, the web browser compiles the code.
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      42
• Which of the following tags is used to pass parameters to applets?
(a) <parameter>
(b) <argc>
(c) <para>
(d) <param>
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      43
• If you are using the Graphics object g and wish to change the pen
  color to blue, what should you do?
(a) g.setPen(Color.blue)
(b) setBackground(Color.blue)
(c) g.setColor(Color.blue)
(d) g.setBlue( )
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
Quiz                                                                                                      44
• Which of the following statements is true?
(a) Applets have main() function.
(b) Applets cannot use System.out.print() method.
(c) Applets are JavaScript programs.
(d) Applets run within a web browser.
    Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri   Armaan Shaikh (E&TC)
                                                                                                45
THANK YOU
Electronics and Telecomunication Engineering, Dr. D. Y. Patil Institute of Technology, Pimpri