Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 COS240 Object-Oriented Languages."— Presentation transcript:

1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 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. 2003. Booch Grady et al, Object-Oriented Analysis and Design with Applications, Addison-Wesley Publ. Com., 3 rd ed. 2007. Course lecturer: Assoc. Prof. Stoyan Bonev, PhD

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

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 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

4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 4 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.

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

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

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

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 8 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.

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 9 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

10 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 10 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 { }

11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 11 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)

12 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 12 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); } }

13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 13 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

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

15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 15 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

16 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 16 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

17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 17 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

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

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

20 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 20 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

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

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

23 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 23 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

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

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

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

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

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

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

30 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 30 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

31 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 31 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

32 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 32 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

33 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 33 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

34 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 34 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

35 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 35 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

36 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 36 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

37 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 37 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

38 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 38 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)

39 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 39 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

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

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


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

Similar presentations


Ads by Google