Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.

Slides:



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

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
 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.
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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
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.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
 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.
Java Programming, 2E Introductory 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.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Chapter 1: Creating Java Programs
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
The NetBeans IDE CSIS 3701: Advanced Object Oriented Programming.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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-
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.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
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.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
TCU CoSc Introduction to Programming (with Java) Java Language Overview.
Creating a Java Application and Applet
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.
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.
CSC201: Computer Programming
Java Primer 1: Types, Classes and Operators
Java Applets.
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet

Chapter 2: Creating a Java Application and Applet 2 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

Chapter 2: Creating a Java Application and Applet 3 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

Chapter 2: Creating a Java Application and Applet 4 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

Chapter 2: Creating a Java Application and Applet 5 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

Chapter 2: Creating a Java Application and Applet 6 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

Chapter 2: Creating a Java Application and Applet 7 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

Chapter 2: Creating a Java Application and Applet 8 Debugging the Solution

Chapter 2: Creating a Java Application and Applet 9 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)

Chapter 2: Creating a Java Application and Applet 10 Editing the Source Code

Chapter 2: Creating a Java Application and Applet 11 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.

Chapter 2: Creating a Java Application and Applet 12 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

Chapter 2: Creating a Java Application and Applet 13

Chapter 2: Creating a Java Application and Applet 14 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();

Chapter 2: Creating a Java Application and Applet 15 Format Output Using Escape Characters Use escape characters inside String arguments to move the output of data

Chapter 2: Creating a Java Application and Applet 16 Editing the Source Code - cont. Recompile and run the application –The bytecode should be updated after any changes to the source code

Chapter 2: Creating a Java Application and Applet 17 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

Chapter 2: Creating a Java Application and Applet 18 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

Chapter 2: Creating a Java Application and Applet 19

Chapter 2: Creating a Java Application and Applet 20 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

Chapter 2: Creating a Java Application and Applet 21 Adding the Java SDK packages Add: import java.util.Date; import java.awt.*;

Chapter 2: Creating a Java Application and Applet 22 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

Chapter 2: Creating a Java Application and Applet 23 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

Chapter 2: Creating a Java Application and Applet 24 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

Chapter 2: Creating a Java Application and Applet 25 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

Chapter 2: Creating a Java Application and Applet 26 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

Chapter 2: Creating a Java Application and Applet 27 Final Result…

Chapter 2: Creating a Java Application and Applet 28 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