Creating a Java Application and Applet - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Creating a Java Application and Applet

Description:

A splash screen is a screen that is displayed before the main program starts ... Save a document before entering code to enable Java related color-coding ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 55
Provided by: steve1722
Category:

less

Transcript and Presenter's Notes

Title: Creating a Java Application and Applet


1
Chapter 2
  • Creating a Java Application and Applet

2
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

3
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, users
    name, and system date
  • The console application will display text only
  • The applet will contain text, color, and a graphic

4
(No Transcript)
5
Program Development

6
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

7
(No Transcript)
8

9

10
Using TextPad
  • TextPad has several window areas
  • Coding window
  • Selector window

11
Using TextPad
  • TextPad can display line numbers
  • Helpful for finding compiler errors

Line Numbers
12
Using TextPad
  • TextPad has color-coding capabilities
  • Save a document before entering code to enable
    Java related color-coding
  • Save files as .java type
  • Document name is then displayed in title bar

13
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

14
Coding the Program -Comments as Documentation

15
Comments
  • Open TextPad, save file as Welcome.java
  • Type the following comments1 /2 Chapter 2
    Welcome to My Day
  • 3 Programmer your name here
  • 4 Date todays date here
  • 5 Filename Welcome.java
  • 6 Purpose This project displays a
    welcome message, the user's
  • 7 name, and the system date in a
    console application.
  • 8 /9

16
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

17
Coding the Program - The Class Header
  • Use braces after the class header to enclose
    the class body

18
Coding the Program - The Class Header
  • Enter the following line, on line 10 10
    public class Welcome 11 12

19
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
  • Enter the following

20
Coding the Program -The Method Header

21
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

22
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
  • Also type the two closing braces for the headers

23
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 System.out.println()
  • Semicolons must be placed after every statement
    except headers and braces
  • Braces enclose the body of a method

24
Testing the Solution
  • Compile the source code
  • javac.exe command
  • In TextPad, use the Compile Java command

25
Testing the Solution
  • 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

26
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

27
Debugging the Solution

28
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

29
Editing the Source Code

30
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

31

32
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()

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

34
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
  • Can quit TextPad by clicking on the Close button

35
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

36
Moving to the Web
  • Use Save As to save the Welcome.java application
    as WelcomeApplet.java

37
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

38

Update line 5
Add three import statements
39
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

40
  • Change the Class Name and Extend the Applet Class

41

42
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

43
The paint() Method

44
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

45
The drawString() Method

46
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

47
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

48
Completed program Save all

49
Completed program
  • Compile java applet the same way as compiling
    java application
  • Fix any errors, save and compile again
  • Do not run applet yet

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

51
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 ltgt and
    an end tag, denoted by lt/gt
  • The tag, ltAPPLETgtlt/APPLETgt, 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

52
HTML Host Document
  • Using TextPad, create a new document
  • Save as WelcomeApplet.html
  • Type the followingltHTMLgt
  • ltAPPLET CODE "WelcomeApplet.class" WIDTH
    "400" HEIGHT "200"gt
  • lt/APPLETgt
  • lt/HTMLgt

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

54
Chapter 2 Homework
  • Short Answer, page 117
  • 1 12,
  • Only type the answers, not the questions
  • All answers must be typed and printed out
  • Debugging Assignment, page 119
  • Programming Assignments
  • Pages 122 - 126
  • 3, 4, 6, 10
Write a Comment
User Comments (0)
About PowerShow.com