Creating a Java Application and Applet

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

IT151: Introduction to Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 1: Introduction
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 1 Introducing Java.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Java Programming, 3e Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to scripting
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
Chapter 2 How to Compile and Execute a Simple Program.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Chapter 1: Creating Java Programs
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
POS 406 Java Technology And Beginning Java Code
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Applets Yong Choi School of Business CSU, Bakersfield.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Introduction to Scripting
Java programming lecture one
C# Programming: From Problem Analysis to Program Design
Java Applets.
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Computer Programming-1 CSC 111
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

Creating a Java Application and Applet Chapter 2 Creating a Java Application and Applet

Chapter Objectives Write a simple Java application Use TextPad Understand the different types and uses of comments Use proper naming conventions for classes and files

Chapter Objectives Identify the parts of a class header and method header Code output Use the println() method Compile a Java program

Chapter Objectives Understand the common types of errors Run a Java Program Edit Java source code to insert escape characters and a system date Print source code

Chapter Objectives Differentiate between an application and an applet Create an applet from Java source code Write code to display a graphic, text, color, and the date in an applet Create an HTML host document Run a Java applet

Introduction Users enter data and instructions into a computer and receive feedback from the computer through a user interface Programmers can create many types of user interfaces in Java We will create a program with two types of user interfaces Console application Command line interface Applet Graphical user interface displayed in a browser

The Welcome to My Day Program This program will display a splash screen A splash screen is a screen that is displayed before the main program starts The screen will contain a welcome message, user’s name, and system date The console application will display text only The applet will contain text, color, and a graphic

Program Development

Analysis and Design Verify that the requirements are specific enough Design the user interface using a storyboard Design the program logic using a flowchart and event diagram

Using TextPad TextPad has several window areas Coding window Selector window Clip Library window TextPad can display line numbers Helpful for finding compiler errors TextPad has color-coding capabilities Save a document before entering code to enable Java related color-coding

Coding the Program - Comments as Documentation Purpose of comments Provides clear description when reviewing code Helps programmer think clearly when coding Placement of comments Use a comment header to identify a file and its purpose Place a comment at the beginning of code for each event and method Place comments near portions of code that need clarification

Coding the Program - Comments as Documentation

Coding the Program - The Class Header Identify how the code can be accessed with an access modifier public indicates that the code can be accessed by all objects in the program and can be extended for a subclass Specify a unique name for the class The class name at the beginning of the program must match the file name exactly Java is case-sensitive By convention, uppercase letters are used for class names and to distinguish words in class names

Coding the Program - The Class Header Use braces { } after the class header to enclose the class body

Coding the Program - The Method Header The method header contains modifiers, return value, method name, and parameters along with their data type Every stand-alone Java application must contain a main() method, which is the starting point during execution

Coding the Program - The Method Header

Coding the Program - The Method Header Modifiers set properties for a method public allows other programs to invoke this method static means this method is unique and can be invoked without creating a subclass or instance Return value is the data type of the data returned by the method If no data is returned, the keyword void is used Parameters are pieces of data received by the method to help the method perform its operation Identifiers are used to name the variable sent to the method

Coding Output Call the System.out.println() method in the SDK to display output to the monitor System is the class out is the object representing the default display println() is the method

Coding Output When calling a method, arguments are placed in parentheses String literals are placed in quotation marks Numeric literals and variables do not need quotation marks Period delimiters separate the class, object, and method Semicolons must be placed after every statement except headers and braces Braces { } enclose the body of a method

Testing the Solution Compile the source code javac.exe command In TextPad, use the Compile Java command At the command prompt, type javac filename.java A new bytecode file for each class is created with a .class extension If the compiler detects errors, fix the errors and compile again If the compilation was successful, run the program

Debugging the Solution System Errors System command is not set properly Software is installed incorrectly Location of stored files is not accessible Syntax Errors One or more violations of the syntax rules of Java Semantic Errors The code meaning is unrecognizable to the compiler Logic and Run-Time Errors Unexpected conditions during execution of a program

Debugging the Solution

Running the Application After compilation is successful, run the program to test for logic and run-time errors Use the Run Java Application command in TextPad TextPad automatically finds the class file with the same name Use the java command from the command prompt Syntax: java classname (no extension)

Editing the Source Code

Import Packages Use the import statement to access classes in the SDK The java.lang package is automatically imported Place the import statement before the class header Use an asterisk (*) after the package name and period delimiter to import all necessary classes in the package

Call a System Date Constructor Use the Date class in the java.util package to access the system date Store the Date in an object variable Declare the object variable by calling the Date constructor The constructor is a method denoted by the new keyword followed by the object type and parentheses Declaration syntax: objectType variableName = new objectType();

Format Output Using Escape Characters Use escape characters inside String arguments to move the output of data

Editing the Source Code - cont. Recompile and run the application The bytecode should be updated after any changes to the source code Print a hard copy of the source code The final step of the program development cycle is to document the solution Quit TextPad by clicking on the Close button

Moving to the Web Characteristics of an applet Applets run within a browser/viewer and are usually delivered to the client machine via the Web Applets cannot use system resources or files on the client machine Convert the application into an applet Import two packages Change the class name and extend the Applet class Include a paint method to draw text and display color and a graphic

Import Applet Packages Applet package (java.applet.*) Allows applets to inherit attributes and methods AWT package (java.awt.*) Provides access to color, draw methods, and GUI elements

Change the Class Name and Extend the Applet Class Change the class name and file name to create a new applet file Edit the comment header in the applet file Add “extends Applet” in the class header to inherit from the superclass, Applet Provides the init() method to load the applet in the browser window

The paint() Method Accepts a Graphics object as a parameter The Graphics object is commonly referred to by the variable name g The variable g is created and initialized in the init() method The variable g is a reference variable, or a specific instance of an object The return type is void

The drawString() Method Displays text in the applet window Accepts three arguments The String data If the data is not a String object, convert it to a String object using the toString() method The horizontal and vertical coordinates of the String The coordinates are measured in pixels Called by the Graphics object, g

Draw an Image Declare an Image object Use the getImage() method to load the image The getImage() method calls the getDocumentBase() method to pull the image from the current folder Use the drawImage() method to set the coordinates of the image

Set the Background Color Use the setBackground() method to change the background color of the applet window The setBackground() method does not need to be called from a reference variable

Creating an HTML Host Document A host program, such as a Web page executes the applet

Creating an HTML Host Document The Web page contains HTML tags to define a section or format A tag consists of a start tag, denoted by <> and an end tag, denoted by </> The tag, <APPLET>…</APPLET>, informs the browser of the applet The applet tag encloses the name of the bytecode applet file and the width and height of the applet window

Running an Applet An applet is run by opening the HTML host document In TextPad, use the Run Java Applet command At the command prompt, type appletviewer followed by the name of the host document Use Applet Viewer to test the applet Ignores irrelevant HTML code Uses less memory than a browser Does not have to be Java-enabled

Chapter Summary Write a simple Java application Use TextPad Understand the different types and uses of comments Use proper naming conventions for classes and files

Chapter Summary Identify the parts of a class header and method header Code output Use the println() method Compile a Java program

Chapter Summary Understand the common types of errors Run a Java Program Edit Java source code to insert escape characters and a system date Print source code

Chapter Summary Differentiate between an application and an applet Create an applet from Java source code Write code to display a graphic, text, color, and the date in an applet Create an HTML host document Run a Java applet

Chapter 2 Complete