Lecture 06: Demonstration-II
Introduction to Java Applet Programming
Overview of Java Applets
- The lecture begins with a discussion on Java applet programming, highlighting its unique and enjoyable aspects compared to traditional Java programming.
- A quick demonstration is planned to illustrate how to write a simple Java applet, including various configurations such as background color and input handling through HTML.
Writing the First Simple Java Applet
- The instructor introduces the "Hello World" applet as an example for today's demo, emphasizing the simplicity of creating an applet program.
- The session will cover writing the first basic Java applet program using specific packages necessary for input and graphics.
Java Packages and Class Structure
Required Packages for Applets
- Two essential packages must be imported:
java.applet.Appletfor applet functionality andjava.awtfor graphical components.
- The class file name should reflect the applet's purpose; in this case, it will be named
demonstration_21, extending from the Applet class.
Class File Naming Convention
- The naming convention requires that the file be saved as a
.javafile, specificallydemonstration_21.java.
- This simple applet includes a method to display text at specified coordinates within the applet window.
Compiling and Running the Applet
Compilation Process
- After writing the code, it needs to be compiled using
javac, which generates a corresponding.classfile nameddemonstration_21.class.
- To run the applet, an HTML file must also be created that references this compiled class file.
Creating an HTML File
- A standard HTML structure is required; key tags include
<applet>which defines dimensions (300x300 pixels).
- The HTML file can be saved with any name; in this instance, it's suggested to use
<ID1>.html.
Running and Modifying the Applet
Viewing the Applet
- Using an applet viewer tool allows you to run your created HTML file (
HTML_21.html) which displays your newly created applet.
Making Changes to Code
- If modifications are made in the program (e.g., changing text), recompilation is necessary while keeping existing HTML files unchanged.
Final Adjustments and Testing
Adjusting Dimensions
- Changing dimensions from 300x300 pixels to 200x200 pixels demonstrates how easy it is to modify properties without needing extensive changes elsewhere.
Understanding Applet Programming in Java
Modifying Applet Dimensions
- The text discusses the importance of being cautious when changing the dimensions of an applet, specifically mentioning a change to 500 x 500 pixels. This adjustment is crucial for understanding how it affects the display and overall functionality of the applet.
Drawing Strings in Applets
- The program utilizes
g.drawStringto print text within the applet. A demonstration involves printing another string, "Welcome," at specific coordinates (50, 20), showcasing how to manipulate text placement on the screen.
Compiling and Running Applets
- After modifying the applet's code, it is necessary to compile it again. The speaker emphasizes that no changes are needed in the HTML file after compilation, indicating a streamlined process for running updated applets.
Displaying Multiple Text Elements
- Two different texts are displayed: "Kharagpur" at coordinates (50, 20) and "NPTEL Java" at (150, 150). This illustrates how multiple strings can be rendered simultaneously within an applet environment.
Introduction to HTML Integration
- The discussion transitions into integrating HTML with Java applets. It highlights that modifications should primarily occur in the HTML file rather than directly within the applet code itself, simplifying updates and maintenance processes.
Exploring Basic Applet Features
Changing Titles Dynamically
- The title tag in HTML can be modified without altering any other part of the applet program itself; this flexibility allows for dynamic content presentation based on user interaction or other factors.
Running Demonstration Programs
- A reference is made to running a simple Java program named
demonstration_22.class, which serves as an example of how basic functionalities can be executed through an applet viewer integrated with HTML files. This reinforces practical application alongside theoretical knowledge.
Understanding Methods in Applets
Overview of Init() Method Usage
- The init() method plays a critical role in initializing parameters for an applet before it runs; this includes setting dimensions like width and height through predefined values such as 300 x 300 pixels during setup procedures.
Resizing Mechanism Explained
- An explanation follows regarding resizing methods available within the applet class that allow developers to adjust dimensions dynamically based on requirements or user input without needing extensive code alterations each time adjustments are made.
This structured approach provides clarity on key concepts related to Java applets while ensuring easy navigation through timestamps linked directly to relevant sections of content discussed in detail throughout the transcript.
Introduction to Applet Initialization
Overview of the init() Method
- The
init()method is essential for starting your applet, setting up its initial state.
- The background color defaults to white; however, it can be changed using the
setBackgroundmethod from the AWT package.
- In this program, the background color is set to yellow using
color.yellow, which modifies the applet's appearance.
- After resizing the applet to 400x400 pixels, recompilation is necessary due to changes in code structure.
Changing Background Color
- The
init()method allows for various configurations, including changing background colors dynamically.
- To change colors effectively, one must compile and run the applet after making adjustments in the class file.
Handling User Input in Applets
Input Mechanism via HTML
- Applets cannot directly take keyboard input or any external data sources; they rely on HTML for user interaction.
- An example program demonstrates how an applet can receive input through HTML forms and display messages based on that input.
Variable Declaration and Parsing
- Two variables, x and y, are declared as integers to store values read from HTML parameters.
- The message variable captures string data from HTML and displays it at specified coordinates (x,y).
HTML Structure for Applets
Defining Parameters in HTML
- The HTML file must specify three parameters: message, x position, and y position with their respective values for proper functioning of the applet.
Understanding Applet Programming in HTML
Introduction to Applet Values
- The discussion begins with the concept of values that can be sent to an applet through an HTML file, emphasizing the importance of class files in this process.
- An example program is introduced, highlighting how changing input values affects the output without needing to recompile the applet program.
Modifying Input and Output
- The speaker demonstrates how different messages can be printed based on modified input values, showcasing real-time changes from the HTML file.
- It is noted that if a program is run without specifying a complete filename, it will not execute properly due to identification issues.
Importance of Correct Filenames
- The necessity for accurate filenames when calling applets is stressed; mismatches can lead to execution failures.
- The speaker clarifies that while it's preferable for the HTML file name to match the intended class file name, discrepancies are manageable.
Understanding Initialization and Execution
- A brief overview of the
init()method's role in applet programming is provided, along with its relationship to necessary HTML files.
- The conversation shifts towards how Java programs interact with applet viewers and interpreters during runtime.
Compilation and Execution Process
- The compilation process creates a class file (e.g.,
demonstration_26.class), which must be executed correctly within an HTML context for successful output.