Download presentation
Presentation is loading. Please wait.
Published byBeryl Rich Modified over 9 years ago
1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 COS240 O-O Languages AUBG, COS dept Lecture 20 Title: Intro to Java Applets Reference: COS240 Syllabus, Malik, ch 12
2
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 2 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
3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 Java Programming: From Problem Analysis to Program Design, 4e3 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.
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 Inheritance Hierarchy of GUI Classes
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 Members of class JApplet
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 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.
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 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
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 Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { }
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 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)
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 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); } }
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 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
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 HTML to Run Applet
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 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
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 More attractive applets F Ways to make applets more attractive are to vary the font type and the color scheme
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 class Font F Shows text in different fonts F Contained in package java.awt F For more details, see previous lecture on Graphics
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 Constructors and Methods of the class Font (continued)
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 Constructors and Methods of the class Font (continued)
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 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
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 Constants Defined in the class Color (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 Constants Defined in the class Color (continued)
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 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
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 Constructors and Methods of the class Graphics (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 Constructors and Methods of the class Graphics (continued)
24
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 24 More Demo programs Java applet file WelcomeApplet.java
25
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 25 More Demo programs Java applet file WelcomeApplet.java Java applet file GrandWelcome.java
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 Java applet file GrandWelcome.java Java applet file GrandWelcomeLine.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 Java applet file GrandWelcomeLine.java Java applet file GrandWelcomeCheckBox.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 Java applet file GrandWelcomeCheckBox.java Java applet file GrandWelcomeRButton.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 Java applet file GrandWelcomeRButton.java Java applet file GrandWelcomeFinal.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 Java applet file GrandWelcomeFinal.java Java applet file FontsDisplayed.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 Java applet file FontsDisplayed.java Java applet file ColorsDisplayed.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 Java applet file ColorsDisplayed.java Java applet file FreeDrawApplet.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 Java applet file FreeDrawApplet.java Java applet file OneChar.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 Java applet file OneChar.java Java applet file OvalRectApplet.java
35
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 35 Java Programming: From Problem Analysis to Program Design, 4e35 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
36
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 36 Java Programming: From Problem Analysis to Program Design, 4e36 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)
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 Converting a GUI Application to an Applet Java application file TempConversion.java Java applet file TempConvertApplet.java
38
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080738 Thank You for Your attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.