java programming, 3e concepts and techniques chapter 2 - part 2 creating a java application and...
TRANSCRIPT
![Page 1: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/1.jpg)
Java Programming, 3eConcepts and Techniques
Chapter 2 - Part 2Creating a Java
Application and Applet
![Page 2: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/2.jpg)
2Chapter 2: Creating a Java Application and Applet
Chapter Objectives
• Code output• Use the println() method• Compile a Java program• 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
![Page 3: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/3.jpg)
3Chapter 2: Creating a Java Application and Applet
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
![Page 4: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/4.jpg)
4Chapter 2: Creating a Java Application and Applet
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
![Page 5: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/5.jpg)
5Chapter 2: Creating a Java Application and Applet
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
![Page 6: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/6.jpg)
6Chapter 2: Creating a Java Application and Applet
Testing the Solution
• Compile the source code– Clean and Build
• If the compiler detects errors, fix the errors and compile again
• If the compilation was successful, run the program
![Page 7: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/7.jpg)
7Chapter 2: Creating a Java Application and Applet
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
![Page 8: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/8.jpg)
8Chapter 2: Creating a Java Application and Applet
Debugging the Solution
![Page 9: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/9.jpg)
9Chapter 2: Creating a Java Application and Applet
Running the Application
• After compilation is successful, run the program to test for logic and run-time errors
• Use the Run command
• Use the java command from the command prompt – Syntax: java classname (no extension)
![Page 10: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/10.jpg)
10Chapter 2: Creating a Java Application and Applet
Editing the Source Code
![Page 11: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/11.jpg)
11Chapter 2: Creating a Java Application and Applet
Java Code Packages
• The Java Software Development Toolkit (SDK) provides libraries of code which can be used in programs.
• System.out.println() is a method provided in the SDK.
![Page 12: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/12.jpg)
12Chapter 2: Creating a Java Application and Applet
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
![Page 13: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/13.jpg)
13Chapter 2: Creating a Java Application and Applet
![Page 14: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/14.jpg)
14Chapter 2: Creating a Java Application and Applet
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();
![Page 15: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/15.jpg)
15Chapter 2: Creating a Java Application and Applet
Format Output Using Escape Characters• Use escape characters inside String arguments
to move the output of data
![Page 16: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/16.jpg)
16Chapter 2: Creating a Java Application and Applet
Editing the Source Code - cont.
• Recompile and run the application– The bytecode should be updated after any
changes to the source code
![Page 17: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/17.jpg)
17Chapter 2: Creating a Java Application and Applet
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
![Page 18: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/18.jpg)
18Chapter 2: Creating a Java Application and Applet
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
![Page 19: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/19.jpg)
19Chapter 2: Creating a Java Application and Applet
![Page 20: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/20.jpg)
20Chapter 2: Creating a Java Application and Applet
Creating a Java Applet Class
• Right click on the Welcome package and select New– If “Applet” is not listed click on “Other” and
choose “Applet” and click Next
• Set the Class Name to “WelcomeApplet” and the package to “welcome”
• Click Finish
![Page 21: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/21.jpg)
21Chapter 2: Creating a Java Application and Applet
Adding the Java SDK packages
• Add:import java.util.Date;
import java.awt.*;
![Page 22: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/22.jpg)
22Chapter 2: Creating a Java Application and Applet
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 a reference variable, or a
specific instance of an object
• The return type is void
![Page 23: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/23.jpg)
23Chapter 2: Creating a Java Application and Applet
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
![Page 24: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/24.jpg)
24Chapter 2: Creating a Java Application and Applet
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
![Page 25: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/25.jpg)
25Chapter 2: Creating a Java Application and Applet
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
![Page 26: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/26.jpg)
26Chapter 2: Creating a Java Application and Applet
Creating an HTML Host Document• A host program, such as a Web page executes
the applet
• If you run the file, NetBeans will build a HTML file for you in the “build” folder.– Copy the image.gif file into the folder and open the
HMTL
![Page 27: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/27.jpg)
27Chapter 2: Creating a Java Application and Applet
Final Result…
![Page 28: Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649e3b5503460f94b2d31a/html5/thumbnails/28.jpg)
28Chapter 2: Creating a Java Application and Applet
Rest of Today
• Create the Java Applet for the Welcome Program– Add your name to Applet– Pick a different color
• Show me the result when your done