1 / 49

Pre-assessment Questions The two subclasses of the Throwable class are:

Pre-assessment Questions The two subclasses of the Throwable class are: Exception class and Error class Exception class and Object class Error class and RunTimeException class Exception class and RunTimeException class The Throwable class is the sub class of _______ class. Exception

oria
Download Presentation

Pre-assessment Questions The two subclasses of the Throwable class are:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pre-assessment Questions • The two subclasses of the Throwable class are: • Exception class and Error class • Exception class and Object class • Error class and RunTimeException class • Exception class and RunTimeException class • The Throwable class is the sub class of _______ class. • Exception • Error • Object • RunTimeException Programming in Java

  2. Pre-assessment Questions (Contd.) • Which exception occurs when you try to create an object of an abstract class or interface? • ClassNotFoundException • IllegalAccessException • InstantiationException • NoSuchMethodException • You use _________ as a command-line option to make the Java compiler accept the code containing assertions. • –source 1.2 • –source 1.4 • –source 1.1.2 • –source 1.1.4 Programming in Java

  3. Pre-assessment Questions (Contd.) • Consider the statements: • Statement A: The scope of the catch block is restricted to the statements in the preceding try block only. • Statement B: A try block must have at least one catch block that follows it immediately. • Which of the following options is true? • Statement A is true and statement B is false • Statement A is false and statement B is true • Both, statements A and B, are true • Both, statements A and B, are false Programming in Java

  4. Solutions to Pre-assessment • Questions • a. Exception class and Error class • c. Object • c. InstantiationException • b. –source 1.4 • c. Both, statements A and B, are true Programming in Java

  5. Objectives • In this lesson, you will learn to: • Create applets in Java • Identify various stages of an applet life cycle • Identify various graphic methods in Java • Use layout managers • Create Abstract Windowing Toolkit control components • Create Swing components • Create a Java applet Programming in Java

  6. Creating Applets in Java • AWT Package • JDK consists of a package called Abstract Window Toolkit (AWT). • AWT is an Application Programming Interface (API) that is responsible for building the Graphical User Interface (GUI) in Java. • The API of the AWT package consists of a collection of classes and methods that enables you to design and manage the Graphical User Interface (GUI) applications. • The AWT package supports applets, which help in creating containers, such as frames or panels that run in the GUI environment. • Introduction to Applets • An applet is a Java program that can be embedded in an HTML Web page. • An applet is compiled on one computer and can run on another computer through a Java enabled Web browser or an appletviewer. • Applets are developed to support the GUI in Java. Programming in Java

  7. Creating Applets in Java (Contd.) • The Applet Class • The Applet class is a member of the Java API package, java.applet. • You use the Applet class to create a Java program that displays an applet. The following figure shows the hierarchical representation of the Java classes: Programming in Java

  8. Method Function void init() Begins the execution of an applet when the Web browser or Java tool calls it. void start() Starts the execution of an applet when the Web browser or Java tool calls it. void update() Calls the paint() method to redraw the drawing area. • Creating Applets in Java (Contd.) • The Applet Class (Contd.) • The Applet class contains various methods that are used to display text and image, play an audio file, and respond when you interact with an applet. • The following table lists the various methods of the Applet class: Programming in Java

  9. Method void paint() Function Redraws the applet’s output. void destroy() Removes the applet completely from the memory. void stop() Suspends the execution of an applet when the Web browser or Java tool calls it. The execution of the applet can be resumed by using the start() method. • Creating Applets in Java (Contd.) • The Applet Class (Contd.) • The following table lists the various methods of the Applet class (Contd.): Programming in Java

  10. Creating Applets in Java (Contd.) • To create an applet, you need to follow these steps: • Create a Java program for the Applet. • Compile the Java program. • Create a Web page that contains an applet. • Run the applet. • You run an applet in an appletviewer by giving the following command on the command prompt: • appletviewer filename.html • You can also run an applet in a Web browser. Programming in Java

  11. Demonstration-Creating an Applet • Problem Statement • MoneySaver Corporation bank has opened branches worldwide and need to develop an application that can be accessed across the globe. The bank has also decided to provide online services to its customers by developing a Website. Steve, the programmer in the bank is assigned the task of creating the home page of the Website. Help Steve to develop the proposed application. Programming in Java

  12. Demonstration-Creating an Applet • (Contd.) • Solution • Steve uses Applets to design the home page of the Website. The HomeApplet class is created that displays information about the various services provided by the bank. To solve the given problem, perform the following tasks: • Code the application. • Compile and execute the application. Programming in Java

  13. Using HTML Tags in Applets • You need to include a java class file in an HTML file to load that file in a Web browser. • The APPLET tag is used to embed an applet in an HTML document. • The APPLET tag is written within the BODY tag of the HTML document. • The following syntax shows how to specify different parameters in an APPLET tag: • <HTML> <HEAD> </HEAD> • <BODY> • <APPLET • CODE • CODEBASE • HEIGHT • WIDTH Programming in Java

  14. Using HTML Tags in Applets • (Contd.) • The following syntax shows how to specify different parameters in an APPLET tag: (Contd.) • VSPACE • HSPACE • ALIGN • ALT • <PARAM NAME= parameter_name VALUE=value of the parameter> • </APPLET> • </BODY> • </HTML> Programming in Java

  15. Using HTML Tags in Applets • (Contd.) • The various attributes of an Applet tag are: • CODE and CODEBASE • The CODE attribute is used to indicate the name of the class file that holds the current Java applet. • The CODE attribute is used when both the .java file and the .html file are located in the same directory. • The CODEBASE attribute indicates the pathname where the .class file is stored. • The CODEBASE attribute is used, if you store a java file in a directory different from an HTML file. • The CODE attribute specifies the name of the class file whereas the CODEBASE attribute contains an alternate pathname where the classes are stored. Programming in Java

  16. Using HTML Tags in Applets • (Contd.) • The various attributes of an Applet tag are: (Contd.) • HSPACE and VSPACE • The HSPACE and VSPACE attributes specify the horizontal and vertical spaces between an applet and the text. • The HSPACE attribute controls the space to the left and right side of an applet. • The VSPACE attribute controls the space above and below an applet. • ALIGN • The ALIGN attribute specifies the alignment of an applet. • You can specify various values to the ALIGN attribute, such as LEFT, RIGHT, BOTTOM, TOP, BASELINE, MIDDLE, TEXTTOP, ABSBOTTOM, and ABSMIDDLE. Programming in Java

  17. Using HTML Tags in Applets • (Contd.) • The ALT attribute specifies the alternate text to be displayed if the browser does not support the applet. • Passing Parameters to Applets • You need to pass parameters to an applet whenever you want to send the details to a Java file through an HTML file. • You pass parameters to an applet by using the <PARAM> tag. The PARAM tag contains the NAME and VALUE attributes. • The NAME attribute specifies the name of the parameter passed to an applet, and the VALUE attribute specifies the value of the variable to be passed. • The following syntax sets the value of the color as red: • <APPLET CODE=”MyFirstApplet.class” HEIGHT = 20 WIDTH = 20> • <PARAM NAME= “color” VALUE= “Red”> • </APPLET> Programming in Java

  18. Using HTML Tags in Applets • (Contd.) • Getting Parameters in Applets • You retrieve the parameters passed to an applet by using the getParameter() method in the Java file. • This method accepts a String argument that indicates the name of the parameter. • The getParameter() method returns a String value that specifies the corresponding value of that parameter. Programming in Java

  19. Identifying the Various Stages of an • Applet Life Cycle • The life cycle of an applet describes the sequence of stages, which begin when an applet is loaded in an appletviewer or a Web browser and ends when the applet is destroyed. • An applet inherits the properties and methods of the Applet class. • Java provides init(), start(), stop(), paint(),and destroy() as the basic applet methods to control the execution of an applet. • The different stages of an applet life cycle are: • Initializing an applet • Starting the applet • Stopping the applet • Destroying the applet Programming in Java

  20. Identifying the Various Stages of an • Applet Life Cycle (Contd.) • The different stages of an applet life cycle are: (Contd.) • Initializing an Applet • The init() method initializes an applet when the applet is loaded for the first time. • It defines the objects and variables that are required to execute an applet. • You apply the settings for fonts, colors, and initial parameters, such as variables and constants in the init() method. • The init() method is also used to add components, such as buttons and check boxes to an applet. • The following syntax shows how to define the init() method: • public void init() • { } Programming in Java

  21. Identifying the Various Stages of an • Applet Life Cycle (Contd.) • The different stages of an applet life cycle are: (Contd.) • Starting the Applet • The start() method is called to start the execution of an applet after it’s initialized with the init() method. • The start() method can be called more than once in an applet. • The following syntax shows how to define the start() method: • public void start() • { • /* start() method definitions. */ • } Programming in Java

  22. Identifying the Various Stages of an • Applet Life Cycle (Contd.) • The different stages of an applet life cycle are: (Contd.) • Stopping the Applet • The stop() method suspends the execution of an applet. • The stop() method is called when either an end user stops an applet or an applet loses the focus. • You can use the stop() method to reset the variables and stop a running applet. • The following syntax shows how to define the stop() method: • public void stop() • { • /* stop() method definitions. */ • } Programming in Java

  23. Identifying the Various Stages of an • Applet Life Cycle (Contd.) • The different stages of an applet life cycle are: (Contd.) • Destroying the Applet • The destroy() method is called when an applet is destroyed. • When you want to exit from the Web browser or appletviewer of Java, an applet calls this method to release the resources, such as parameters and images. • This method occurs only once in the life cycle of an applet. • The following syntax shows how to define thedestroy()method: • public void destroy() • { • /* destroy() method definitions. */ • } Programming in Java

  24. Identifying the Various Stages of an • Applet Life Cycle (Contd.) • The following figure shows the life cycle of an applet: Programming in Java

  25. Various Graphic Methods in • Java • AWT supports various graphics methods that enable you to draw shapes, such as line, arc, ellipse, circle, and rectangle in an applet. • Drawing Lines, Rectangles, and Polygons • You need to draw lines, rectangles, and polygons in an applet for creating drawings in an applet. • The drawLine() method is used to draw lines in an applet. • The following syntax shows how to define thedrawLine()method: • void drawLine(int x1, int y1, int x2, int y2) • In the preceding syntax, the x1 and y1 arguments are the starting coordinates of a line. The x2 and y2 arguments are the ending coordinates of the line. Programming in Java

  26. Various Graphic Methods in • Java (Contd.) • Drawing Lines, Rectangles, and Polygons (Contd.) • The drawRect() is used to draw a rectangle. • The following syntax shows how to define thedrawRect()method: • void drawRect(int x, int y, int width, int length) • In the preceding syntax, the x and y arguments specify the top left coordinates of a rectangle. The width and length arguments specify the dimensions of the rectangle. • You can also draw arbitrary shapes, such as polygons using the drawPolygon() and fillPolygon() methods. • The following syntax shows how to use thedrawPolygon()method: • (int x[], int y[], int num) • In the preceding syntax, the x and y arguments specify an array of integers representing the x and y coordinates of a polygon. The num argument specifies the total number of points of the polygon. Programming in Java

  27. Various Graphic Methods in • Java (Contd.) • Drawing Arcs, Circles, and Ellipses • You can draw an arc using the drawArc() method. • The following syntax shows how to define thedrawArc()method: • void drawArc(int x, int y, int width, int height, int startAngle, int sweepAngle) • In the preceding syntax, the x and y arguments represent the top left end coordinates of a bounding rectangle. The width and height arguments represent the dimensions of the rectangle. The startAngle argument represents the angle at which an arc starts and the sweepAngle argument represents the angular distance covered by the arc. • The drawOval() method is used for drawing circles and ellipses. • The following syntax shows how to define the drawOval() method: • void drawOval(int x, int y, int width, int height) • In the preceding syntax, the x and y arguments are the top left endpoints of a rectangle enclosing a circle or an ellipse. Programming in Java

  28. Various Graphic Methods in • Java (Contd.) • Painting Various Graphic Objects • The fillPolygon() method is used to draw a filled polygon in an applet. • The following syntax shows how to define the fillPolygon() method: • void fillPolygon(int x[], int y[], int num) • In the preceding syntax, the x and y arguments specify an array of integers representing the x and y coordinates of a polygon. The num argument specifies the total number of points of the polygon. • The fillOval()method is used to draw filled circles and ellipses. • The following syntax shows how to define the fillOval() method: • void fillOval(int x, int y, int width, int height) • In the preceding syntax, the x and y arguments are the top left endpoints of an oval. Programming in Java

  29. Various Graphic Methods in • Java (Contd.) • Painting Various Graphic Objects (Contd.) • You can draw filled arcs using the fillArc() method. • The following syntax shows how to define the fillArc() method: • void fillArc(int x, int y, int width, int height, int startAngle, int sweepAngle) • In the preceding syntax, the x and y arguments represent the top left end coordinates of a rectangle. The width and height arguments represent the dimensions of the rectangle. The startAngle argument represents the angle at which an arc starts and the sweepAngle argument represents the angular distance covered by the arc. Programming in Java

  30. Various Graphic Methods in • Java (Contd.) • Painting Various Graphic Objects (Contd.) • You can also draw a filled rectangle in an applet using the fillRect() method. The following syntax shows how to define the fillRect() method: • void fillrect(int x, int y, int width, int length) • In the preceding syntax, the x and y arguments specify the top left corner coordinates of a rectangle. The width and length arguments specify the dimensions of the rectangle. Programming in Java

  31. Demonstration-Using Various Graphic • Methods in Java • Problem Statement • Certified Carriers is an institute that conducts various computer courses. John, the instructor at the institute teaches Java language to students. After the completion of the Java course, John gives a home assignment to the students to create a Java program that draws a figure using various shapes taught in the lecture. Help students to create the application. Programming in Java

  32. Demonstration-Using Various Graphic • Methods in Java (Contd.) • Solution • Students will use Applets to create a program that displays various shapes in an applet. The Smiley class is created that draws a smiley in an applet. To solve the given problem, perform the following tasks: • Code the application. • Compile and execute the application. Programming in Java

  33. The AWT Control Components • Java provides the AWT control components, which contains classes that enable you to create standard components, such as buttons, labels, and text fields in Java. • A Java component enables you to accept input from an end user. • You can position AWT components in containers using the different layout managers. • Using Various AWT Components • An AWT control is a component that enables end users to interact with applications created in Java. • All AWT controls in Java are subclasses of the Component class. • The Component class provides the add() method to add AWT components to containers, such as an applet or a window. • TextField: User interface components that accept text input from an end user. A text field enables you to type text in a single line. An instance of the TextField class is used to create a text field. Programming in Java

  34. The AWT Control Components(Contd.) • Using Various AWT Components (Contd.) • TextArea: Used to accept text input from an end user, but it enables you to type text in multiple lines. An instance of the TextArea class is used to create a text area. • Button: Used for handling events. Java provides the Button class to create AWT button components. • List: Is a scrollable list of text items that enables you to select either one item or multiple items. You create a list using the List class in Java. • CheckBox: Exist in dual state, checked and unchecked. You can change the state of a check box by clicking the check box. You create a check box using the CheckBox class. • Choice: Used to create combination boxes that are also known as drop-down menus using the Choice class in Java. You create a drop-down menu using the Choice class that enables you to select a single item from the menu. • Labels: Used for displaying a single line of text in a container. You create a label using the Label class. Programming in Java

  35. The Swing Components • Swing components are a collection of lightweight visual components that provide a replacement for the heavyweight AWT components. • Swing components contain the Pluggable Look and Feel (PL&F) feature that allows applications to have the same behavior on various platforms. • Identifying the Swing Component Class Hierarchy • The JComponent class is the root of the Swing hierarchy, which is an extension of the AWT container class. • The class hierarchy of the Swing components is categorized into: • Top-level Swing Containers: Acts as a container for placing the intermediate-level and atomic swing components, such as panels, frames, buttons, and check boxes. • Intermediate-level Swing Containers: Placed on the top-level containers and contains atomic components. • Atomic Components: Placed on the intermediate-level swing containers. Atomic components are used to accept input from a user. Programming in Java

  36. The Swing Components (Contd.) • Using the Top-level Swing Containers • JApplet • The JApplet class is an extension of the AWT applet class. • The Swing components that contain an applet need to extend the JApplet class. • TheJApplet()constructor enables you to create a swing applet instance when you create an instance of the JApplet class. • JFrame • The JFrame class is an extension of the AWT Frame class. • You cannot add components directly to JFrame. Programming in Java

  37. The Swing Components (Contd.) • Using the Top-level Swing Containers (Contd.) • JDialog • The JDialog class is an extension of the AWT java.awt.Dialog class. • The JDialog class is used to create modal and non-modal dialog boxes. • Using the Intermediate Level Swing Containers • JPanel • JPanel class is an extension of the JComponent class that provides a replacement for the AWT Panel class. • You create a panel and add various components to it. • The panel is further added to the content pane, which represents the display area of a window, a dialog, or a frame. • JPanel supports all layout managers of AWT. • By default, JPanel applies the flow layout manager. Programming in Java

  38. The Swing Components (Contd.) • Using the Intermediate Level Swing Containers (Contd.) • JTabbedPane • The JTabbedPane class is used to create a tabbed pane component that enables you to switch between groups of components by clicking a tab with a given label. • Tabs are added to the JTabbedPane object by using the addTab() method. • The JTabbedPane class enables you to add multiple components but it displays only a single component at a time. • Using the Atomic Components • JButton • JTextField • JCheckBox • JComboBox • JLabel • JRadioButton Programming in Java

  39. Using Layout Managers • The layout managers are used to position the components, such as an applet, a panel, or a frame in a container. • The layout managers implement the java.awt.LayoutManager interface. • A layout manager is an instance of the LayoutManager interface in Java. • You can use the following method to apply the desired layout to the components: • void setLayout(layoutManager obj) • In the preceding syntax, obj is the reference to the desired layout manager. • Java has various predefined classes of layout managers. • All layout managers make use of the setLayout() method to set the layout of a container. • If the setLayout() method is not used, then the default layout of the container is set. Programming in Java

  40. Using Layout Managers (Contd.) • The different types of layout managers are: • FlowLayout Manager • The flow layout is the default layout manager used for the Applet class. • In the flow layout manager, the components are placed in a container window in a sequence one after the other in rows. • Java provides the FlowLayout class to apply flow layout to the various components that you are inserting in an applet. • You can use the following constructors to create an instance of the FlowLayout class: • FlowLayout() • FlowLayout(int align) • FlowLayout(int align, int hgap,int vgap) Programming in Java

  41. Using Layout Managers (Contd.) • The different types of layout managers are: • BorderLayout Manager • BorderLayout is the default layout of the Frame class. • The BorderLayout layout manager divides the container into north, south, east, west, and centre regions. • You can place five components or controls in each part. • Java provides the BorderLayout class to apply the border layout to the components. • The setLayout() method is used for applying border layout to a container. • You specify the directions for the BorderLayout using the BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, and BorderLayout.CENTER constants. • You can use the following constructors to create an instance of the BorderLayout class: • BorderLayout() • BorderLayout(int h, int v) Programming in Java

  42. Using Layout Managers (Contd.) • The different types of layout managers are: (Contd.) • GridLayout Manager • The grid layout is the layout that divides the container into rows and columns. • The intersection of a row and a column of the grid layout is called cell. • The GridLayout class of Java enables you to create a grid layout. • All the components in a grid are of the same size. • You can use the following constructors to create an instance of the GridLayout class: • GridLayout() • GridLayout(int r, int c) • GridLayout(int r, int c, int h, int v) Programming in Java

  43. Using Layout Managers (Contd.) • The different types of layout managers are: (Contd.) • CardLayout Manager • The CardLayout class is used to implement an area that contains different components at different times. • The CardLayout is often controlled by a combo box, and the state of the combo box determines which panel (group of components) CardLayout displays. • You can use the following constructors to create an instance of the CardLayout class: • CardLayout() • CardLayout(int hgap, int vgap) Programming in Java

  44. Using Layout Managers (Contd.) • The different types of layout managers are: (Contd.) • GridBagLayout Manager • GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. • GridBagLayout places components in rectangles (cells) in a grid and uses the components' preferred sizes to determine the size of the cells. • You can use the following constructor to create an instance of the GridBagLayout class: • GridBagLayout() Programming in Java

  45. Demonstration-Using Layout • Managers • Problem Statement • MoneySaver Corporation is a bank providing various services to its customers. The bank has opened various branches in different countries. The bank has developed a Website that enables customers to access the bank services online. Steve, the bank manager wants to add a feedback page in the Website so that the customers can give their feedback about the bank. Mary, the programmer in the bank is assigned the task of adding a feedback page in the Website. Help Mary to develop the proposed application. Programming in Java

  46. Demonstration-Using Layout Managers • (Contd.) • Solution • Mary can use Applets and Swings to design the feedback page. • Using the Applets • The BankApplet class is created that displays various fields included in the feedback page. To solve the given problem, perform the following tasks: • Code the application. • Compile and execute the application. • Using the Swings • The BankSwing class is created that displays various fields included in the feedback page. To solve the given problem, perform the following tasks: • Code the application. • Compile and execute the application. Programming in Java

  47. Summary • In this lesson, you learned: • An Applet is a Java program that you can embed in a HTML page and access on a Web browser. • Java Application Program Interface (API) includes a package java.applet, which contains the Applet class. • Applet has limited access to system resources and prohibited access to other systems on the network. • All the applets are subclasses of the Applet class. Most classes in Java extend other classes. • The life cycle of an applet consists of the following methods: • init() • start() • stop() • destroy() • You can run an applet in an appletviewer or a Web browser. Programming in Java

  48. Summary (Contd.) • To run an applet in appletviewer, include an HTML tag as a comment. Compile the applet using javac command and run the applet giving the following command: • % appletviewer className.java • The parameters of an applet HTML tag are: • Code • Hspace and Vspace • <PARAM> tag • The various drawing methods in Java are: • drawLine() • drawRect() • draw3Drect() • drawOval() • drawArc() • fillOval() • fillArc() Programming in Java

  49. Summary (Contd.) • The various layout Managers in Java are: • BorderLayout • FlowLayout • GridLayout • CardLayout • GridBagLayout • The various Swing Components are: • JApplet • JPanel • JButton • JComboBox • JCheckBox • JTextField • JRadioButton • JLabel Programming in Java

More Related