1 / 22

Introduction to Java Applet Programming

Chapter 11. Introduction to Java Applet Programming. Barry Sosinsky Valda Hilley Programming the Web. Learning Objectives. What is a Java applet Java applet structure Create a Java applet Embed an applet in an HTML document How to execute a Java applet. Getting to Know Java.

tiva
Télécharger la présentation

Introduction to Java Applet Programming

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. Chapter 11 Introduction to Java Applet Programming Barry SosinskyValda Hilley Programming the Web

  2. Learning Objectives • What is a Java applet • Java applet structure • Create a Java applet • Embed an applet in an HTML document • How to execute a Java applet

  3. Getting to Know Java • The Java programming language is a modern, evolutionary computing language that combines an elegant language design with powerful features that were previously available only in specialty languages. • The Java programming language is a true object-oriented (OO) programming language. Object-oriented languages provide a framework for designing programs that represent real-world entities like cars, employees, or insurance policies. • Java is not JavaScript. Java is not related to JavaScript in any way.

  4. Java Applications • There are 2 basic types of Java applications: • Standalone: These run as a normal program on the computer. They may be a simple console application or a windowed application. These programs have the same capabilities of any program on the system. For example, they may read and write files. • Applets: These run inside a web browser. They must be windowed and have limited power. They run in a restricted JVM (Java Virtual Machine) called the sandbox from which file I/O and printing are impossible.

  5. // HelloWorld.java class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); } } Initial class statement When the main method is called it does only one thing: print "Hello World" to the standard output, generally a terminal monitor or console window Java Syntax Main method

  6. Compiling and Running Java Applications

  7. Between the Blocks and Braces • In Java a source code file is broken up into parts separated by opening and closing braces, i.e. the { and } characters. Everything between { and } is a block and exists independently of everything outside of the braces. • The braces are used to group related statements together. Everything between matching braces executes as one statement (though depending on the code logic not everything may execute every time). • Blocks can be hierarchical. One block can contain one or more subsidiary blocks.

  8. Data and Variables The program now says hello back to Valda. This was done by creating a String variable called "name" and storing the name Valda as the value. // This is the Hello Reader program in Java class HelloReader { public static void main (String args[]) { // I'm going to replace "Reader" with my name String name = "Valda"; /* Now, say hello */ System.out.print("Hello "); System.out.println(name); } }

  9. Command Line Arguments You can also have the program automatically change or personalize the greeting at runtime rather than at compile time by using command-line arguments: // This is the Hello program in Java class Hello { public static void main (String args[]) { /* Now let's say hello */ System.out.print("Hello "); System.out.println(args[0]); } }

  10. If Statements All programming languages have some form of an if statement that allows you to test conditions. Here, we test the length of the args array: // This is the Hello program in Java class Hello { public static void main (String args[]) { /* Now say hello */ System.out.print("Hello "); if (args.length > 0) { System.out.println(args[0]); } } }

  11. Else Statements What we need is an else statement that will catch any result other than the one we hope for, and luckily Java provides exactly that: // This is the Hello program in Java class Hello { public static void main (String args[]) { /* Now say hello */ System.out.print("Hello "); if (args.length > 0) { System.out.println(args[0]); } else { System.out.println("whomever you are"); } } }

  12. This second class is a completely independent program Classes and Objects Everything in Java is either a class, a part of a class, or describes how a class behaves. Methods are defined inside the classes they belong to. class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); } } class GoodbyeWorld { public static void main (String args[]) { System.out.println("Goodbye World!"); } }

  13. Methods • As programs grow in size, it begins to make sense to break them into parts. Each part can perform a particular calculation and possibly return a value. This is especially useful when the calculation needs to be repeated at several different places in the program. It also helps to define a clearer picture of the flow of the program, much like an outline shows the flow of a book. • Each calculation part of a program is called a method. • You can write and call your own methods too.

  14. Methods (2) Methods break up a program into logically separate algorithms and calculations. In still larger programs, it’s necessary to break up the data as well. The data can be separated into different classes and the methods attached to the classes they operate on. static long factorial (int n) { int i; long result=1; for (i=1; i <= n; i++) { result *= i; } return result; }

  15. Arrays • An array is a group of variables that share the same name and are ordered sequentially from zero to one less than the number of variables in the array. • The number of variables that can be stored in an array is called the array’s dimension. • Each variable in the array is called an element of the array. • There are three steps to creating an array: • declaring it • allocating it • initializing it

  16. Declaring Arrays • Like all other variables in Java, an array must be declared. When you declare an array variable you suffix the type with [] to indicate that this variable is an array. Here are some examples: int[] k; float[] yt; String[] names; • You declare an array like you’d declare any other variable, except you append brackets to the end of the variable type.

  17. Allocating Arrays • When we create an array we need to tell the compiler how many elements will be stored in it k = new int[3]; yt = new float[7]; names = new String[50]; • The numbers in the brackets specify the dimension of the array; i.e. how many slots it has to hold values. This is commonly called allocating the array since this step actually sets aside the memory in RAM that the array requires.

  18. Initializing Arrays • Individual elements of the array are referenced by the array name and by an integer which represents their position in the array. The numbers we use to identify them are called subscripts or indexes into the array. k[0] = 2; k[1] = 5; k[2] = -2; Yt[6] = 7.5f; names[4] = "Fred"; • This step is called initializing the array or, more precisely, initializing the elements of the array.

  19. Shortcuts • Java has shorthand for declaring, dimensioning and strong values in arrays. We can declare and allocate an array at the same time like this: int[] k = new int[3]; float[] yt = new float[7]; String[] names = new String[50]; • We can even declare, allocate, and initialize an array at the same time providing a list of the initial values inside brackets like so: int[] k = {1, 2, 3}; float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};

  20. Hello World Applet HelloWorldApplet.class import java.applet.Applet; import java.awt.Graphics; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } } HelloWorldApplet.html <HTML> <HEAD> <TITLE> Hello World </TITLE> </HEAD> <BODY> This is the applet:<P> <APPLET codebase="classes" code="HelloWorldApplet.class" width=200 height=200 ></APPLET> </BODY> </HTML>

  21. Passing Parameters to Applets DrawStringApplet.class import java.applet.Applet; import java.awt.Graphics; public class DrawStringApplet extends Applet { String input_from_page; public void init() { input_from_page = getParameter("String"); } public void paint(Graphics g) { g.drawString(input_from_page, 50, 25); } } DrawStringApplet.html <HTML> <HEAD> <TITLE> Draw String </TITLE> </HEAD> <BODY> This is the applet:<P> <APPLET codebase="classes" code="DrawStringApplet.class" width=200 height=200><PARAM name="String" value="Howdy, there!"></APPLET> </BODY> </HTML>

  22. The End

More Related