 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.

Slides:



Advertisements
Similar presentations
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Advertisements

PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
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 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
 2002 Prentice Hall. All rights reserved. 1 Week 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
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.
A Simple Applet.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 24 - Introduction to Java Applications and Applets.
1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software Development Kit The TicTacToe Applet.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
JAPPLET.
1 Introduction to Applets. 2 Applications Programs that execute using the java interpreter Executes in command windows.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
 Pearson Education, Inc. All rights reserved Introduction to Java Applets.
Chapter 3 Introduction to Java Applets 1 3 There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run.
Java I--Copyright © Tom Hunter. Chapter 3 Introduction to Java Applets.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Review of CIS 120 Concepts: What you said you want….
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Applets Yong Choi School of Business CSU, Bakersfield.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
 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.
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 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
 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.
Lecture 09 Applets.
Object Oriented Programming
Introduction to Java Applets
Chapter 3 - Introduction to Java Applets
Java I.
Chapter 24 - Introduction to Java Applications and Applets
Java Applets.
Chapter 3 - Introduction to Java Applets
Applet in Java.
Handout-14 Applets and Graphics
Outline Character Strings Variables and Assignment
Presentation transcript:

 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 Software Development Kit 3.3 Simple Java Applet: Drawing a String 3.4 Drawing Strings and Lines

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String Now, create applets of our own –Take a while before we can write applets like in the demos –Cover many of same techniques Upcoming program –Create an applet to display "Welcome to Java!!" –Show applet and HTML file, then discuss them line by line

3 import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } extends allows us to inherit the capabilities of class Applet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above.  2003 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Import predefined classes grouped into packages When you create applets, import Applet class (in the package java.applet) import the Graphics class (package java.awt ) to draw graphics –Can draw lines, rectangles ovals, strings of characters import specifies directory structure import java.awt.*; // import package with class Graphics import javax.applet.*; // import class Applet Modified by Evan Korth

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Applets have at least one class declaration (like applications) Rarely create classes from scratch –Use pieces of existing classes –Inheritance - create new classes from old ones (will cover soon) –Begins class declaration for class WelcomeApplet Keyword class then class name –extends followed by class name Indicates class to extend ( Applet ) –Applet : superclass (base class) –WelcomeApplet : subclass (derived class) WelcomeApplet now has methods and data of Applet public class WelcomeApplet extends Applet { Modified by Evan Korth

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Class Applet defined for us Someone else defined "what it means to be an applet" –Applets require over 200 methods! extends Applet –Inherit methods, do not have to declare them all Do not need to know every detail of class Applet public class WelcomeApplet extends Applet { Modified by Evan Korth

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Class WelcomeApplet is a blueprint appletviewer or browser creates an object of class WelcomeApplet –Keyword public required –File can only have one public class –public class name must be file name public class WelcomeApplet extends Applet {

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Our class inherits method paint from Applet By default, paint has empty body Override (redefine) paint in our class –Methods init, start and paint. Guaranteed to be called automatically Our applet gets "free" version of these by inheriting from Applet –Free versions have empty body (do nothing) –Every applet does not need all three methods Override the ones you need –Applet container “draws itself” by calling method paint public void paint( Graphics g ) Modified by Evan Korth

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Method paint Draws graphics on screen void indicates paint returns nothing when finishes task Parenthesis define parameter list - where methods receive data to perform tasks –Normally, data passed by programmer, as in JOptionPane.showMessageDialog paint gets parameters automatically –Graphics object used by paint Mimic paint 's first line public void paint( Graphics g )

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Body of paint Method drawString (of class Graphics ) Called using Graphics object g and dot (. ) Method name, then parenthesis with arguments –First argument: String to draw –Second: x coordinate (in pixels) location –Third: y coordinate (in pixels) location –Java coordinate system Measured in pixels (picture elements) Upper left is ( 0, 0 ) g.drawString( "Welcome to Java Programming!", 25, 25 );

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String Running the applet –Compile As you would a regular file in JCreator –or javac WelcomeApplet.java (from the command line) If no errors, bytecodes stored in WelcomeApplet.class –Create an HTML file Loads the applet into appletviewer or a browser Ends in.htm or.html –To execute an applet Create an HTML file indicating which applet the browser (or appletviewer ) should load and execute Modified by Evan Korth

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –Simple HTML file ( WelcomeApplet.html ) Usually in same directory as.class file Remember,.class file created after compilation –HTML codes (tags) Usually come in pairs Begin with –Lines 1 and 4 - begin and end the HTML tags –Line 2 - begins tag Specifies code to use for applet Specifies width and height of display area in pixels –Line 3 - ends tag

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String –appletviewer only understands tags Ignores everything else Minimal browser –Executing the applet appletviewer WelcomeApplet.html Perform in directory containing.class file

 2003 Prentice Hall, Inc. All rights reserved Simple Java Applet: Drawing a String Running the applet in a Web browser

 2003 Prentice Hall, Inc. All rights reserved Drawing Strings and Lines More applets –First example Display two lines of text Use drawString to simulate a new line with two drawString statements –Second example Method g.drawLine(x1, y1, x2, y2 ) –Draws a line from ( x1, y1 ) to ( x2, y2 ) –Remember that ( 0, 0 ) is upper left Use drawLine to draw a line beneath and above a string

16 import java.awt.*; import java.applet.*; public class WelcomeApplet2 extends Applet { public void init() { } public void paint(Graphics g) { g.drawString( "Welcome to", 25, 25 ); g.drawString( "Java Programming!", 25, 40 ); } } The two drawString statements simulate a newline. In fact, the concept of lines of text does not exist when drawing strings. Modified by Evan Korth  2003 Prentice Hall, Inc. All rights reserved.

17 HTML file Program Output  2003 Prentice Hall, Inc. All rights reserved.

18 WelcomeLines.ja va 2. Class WelcomeLines ( extends Applet ) 3. paint 3.1 drawLine 3.2 drawLine 3.3 drawString Program Output import java.awt.*; import java.applet.*; public class WelcomeApplet3 extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); g.drawLine (15, 10, 210, 10); g.drawLine (15, 30, 210, 30); } Draw horizontal lines with drawLine (endpoints have same y coordinate). Modified by Evan Korth  2003 Prentice Hall, Inc. All rights reserved.

19 HTML file  2003 Prentice Hall, Inc. All rights reserved.

20 3.4Drawing Strings and Lines Method drawLine of class Graphics –Takes as arguments Graphics object and line’s end points –X and y coordinate of first endpoint –X and y coordinate of second endpoint