Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 COS240 Object-Oriented Languages.

Slides:



Advertisements
Similar presentations
Dept. of Computer Science - SSBN Java Applets Vishnuvardhan.M.
Advertisements

5/15/2015Assoc. Prof. Stoyan Bonev1 Assoc. Prof. Stoyan Bonev, PhD Computer Science dept. American University in Bulgaria
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
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.
 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.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
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.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
 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.
Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Introduction to Java Programming CS 21a: Introduction to Computing I First Semester,
Applets Java API.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
©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 Workshop for Teachers May 6, 2005 A Brief Look at the Java Programming Language.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1 Introduction to Applets. 2 Applications Programs that execute using the java interpreter Executes in command windows.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 INF160 IS Development Environments AUBG, COS dept Lecture 04 Title: Computer Applications (Extract from Syllabus)
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
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,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CS202 Java Object Oriented Programming GUI Programming – Applets Chengyu Sun California State University, Los Angeles.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Java Applets.
Lecture 09 Applets.
Java Applets.
Chapter 13: Advanced GUIs and Graphics
UNIT-5.
Java Applets.
Java Applets.
Java Applets.
Outline Character Strings Variables and Assignment
Advanced GUIs and Graphics
Presentation transcript:

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages AUBG, COS dept, Spring semester 2012 Reference books: Budd A., An Introduction to Object-Oriented Programming, Addison-Wesley Publ. Com., 3 rd ed Booch Grady et al, Object-Oriented Analysis and Design with Applications, Addison-Wesley Publ. Com., 3 rd ed Course lecturer: Assoc. Prof. Stoyan Bonev, PhD

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG, COS dept Lecture 20 Title: Intro to Java Applets Reference: COS240 Syllabus, Malik, ch 12

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Lecture Contents: F Learn about applets F Applet methods F Skeleton of a Java applet F Differences Between Applets and GUI Applications F Converting a GUI Application to an Applet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e4 Applets F Applet: a Java program that is embedded within a Web page (i.e. HTML document) and executed by a Web browser F Java programs called from within another application, Frequently run from a Web page –Display as rectangular area –Can respond to user-initiated events –Behaviors come from Java class named JApplet  Create an applet by extending the class JApplet  class JApplet contained in package javax.swing F Java applet refers to Java little application.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e5 Inheritance Hierarchy of GUI Classes

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e6 Members of class JApplet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e7 Members of class Japplet (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e8 Applets (continued)  No main() method  Methods init(void), start(void), paint(Graphics g) guaranteed to be invoked in sequence  Methods init(), start() have no parameters  Method’s paint() formal parameter allows the developer to use abstract class Graphics without actually creating a Graphics object F To develop an applet: –Override one or all of the methods above.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e9 Applet Methods  init() Method –Initializes variables –Gets data from user –Places various GUI components  paint() Method –Performs output –For example draws various items, including strings, in the content pane of the applet.  init(), paint() m ethods need to share common data items, so these data items are the data members of the applet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e10 Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e11 Applet Displaying Welcome Message Problem: create an applet to display welcome message. Analysis: No initialization required: no need of init() method What is must: to override the method paint() to draw the welcome message. Sometimes when you override a method, it is a good idea to invoke the corresponding method of the parent class. Whenever you override the paint() method, the first Java stmt is super.paint(g); To display the string that contains the welcome message, the drawString() method of the Graphics class is to be used (details in previous lecture on Graphics)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e12 Applet Displaying Welcome Message //Welcome Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { public void paint(Graphics g) { super.paint(g); //Line 1 g.drawString( " Welcome to Java Programming ",30,30); } }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e13 Applet Displaying Welcome Message Procedure: As with an application, an applet is compiled to produce.class file. Once the.class file is created, it is to be placed in a Web page to run the applet For example, a file with extension.html, say welcome.html as that shown on the next slide located in the same folder where the.class applet file resides

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e14 HTML to Run Applet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e15 How to run Applet F Once the HTML file is created, there are different ways to run the applet  Open the.html file with a Web browser OR F In case you use JDK, you can run an utility with a command line like appletviewer Welcome.html

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e16 More attractive applets F Ways to make applets more attractive are to vary the font type and the color scheme

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e17 class Font F Shows text in different fonts F Contained in package java.awt F For more details, see previous lecture on Graphics

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e18 Constructors and Methods of the class Font (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e19 Constructors and Methods of the class Font (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e20 class Color F Shows text in different colors F Changes background color of component  Contained in package java.awt F For more details, see previous lecture on Graphics

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e21 Constants Defined in the class Color (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e22 Constants Defined in the class Color (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e23 class Graphics F Provides methods for drawing items such as lines, ovals, and rectangles on the screen F Contains methods to set the properties of graphic elements including clipping area, fonts, and colors  Contained in the package java.awt F For more details, see previous lecture on Graphics

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e24 Constructors and Methods of the class Graphics (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e25 Constructors and Methods of the class Graphics (continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java  Java applet file GrandWelcomeFinal.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java  Java applet file GrandWelcomeFinal.java  Java applet file FontsDisplayed.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java  Java applet file GrandWelcomeFinal.java  Java applet file FontsDisplayed.java  Java applet file ColorsDisplayed.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java  Java applet file GrandWelcomeFinal.java  Java applet file FontsDisplayed.java  Java applet file ColorsDisplayed.java  Java applet file FreeDrawApplet.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java  Java applet file GrandWelcomeFinal.java  Java applet file FontsDisplayed.java  Java applet file ColorsDisplayed.java  Java applet file FreeDrawApplet.java  Java applet file OneChar.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved More Demo programs  Java applet file WelcomeApplet.java  Java applet file GrandWelcome.java  Java applet file GrandWelcomeLine.java  Java applet file GrandWelcomeCheckBox.java  Java applet file GrandWelcomeRButton.java  Java applet file GrandWelcomeFinal.java  Java applet file FontsDisplayed.java  Java applet file ColorsDisplayed.java  Java applet file FreeDrawApplet.java  Java applet file OneChar.java  Java applet file OvalRectApplet.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e37 Differences Between Applets and GUI Applications F Applets –Class extends JApplet –No main method –Uses init method –Displayed by HTML –Sets title in HTML –Size set in HTML –Applet closes when HTML doc closes F GUI applications –class extends JFrame –Invokes main method –Uses constructors –Uses method setVisible –Uses setTitle method –Uses method setSize –Closes with Exit button

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e38 Converting a GUI Application to an Applet F Change JFrame to JApplet F Change constructor to method init F Remove method calls such as setVisible, setTitle, setSize F Remove the method main F If applicable, remove Exit button and all code associated with it (e.g., action listener)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Java Programming: From Problem Analysis to Program Design, 4e39 Converting a GUI Application to an Applet  Java application file TempConversion.java  Java applet file TempConvertApplet.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Exercises F Demo programs –Malik, chap 12 F More programming exercises –Malik, chap 12, pp xxx

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Thank You for Your attention!