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!