Outline: 2003 Prentice Hall, Inc. All Rights Reserved
Outline: 2003 Prentice Hall, Inc. All Rights Reserved
3.1 Introduction
• Applet
– Program that runs in
• appletviewer (test utility for applets)
• Web browser (IE, Communicator)
– Executes when HTML (Hypertext Markup Language)
document containing applet is opened and downloaded
– Applications run in command windows
• Notes
– Mimic several features of Chapter 2 to reinforce them
– Focus on fundamental programming concepts first
• Explanations will come later
Program Output
– Comments
• Name of source code and description of applet
5 import java.awt.Graphics; // import class Graphics
6 import javax.swing.JApplet; // import class JApplet
– Method paint
• Lines 11-19 are the declaration of 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
– 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)
• 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
Program Output
Program Output
2003 Prentice Hall, Inc.
All rights reserved.
25
1 <html> Outline
2 <applet code = "WelcomeLines.class" width = "300" height = "40">
3 </applet>
4 </html> HTML file
• Next applet
– Mimics application for adding two integers (Fig 2.9)
• This time, use floating point numbers (numbers with a decimal
point)
– Using primitive types
• double – double precision floating-point numbers
• float – single precision floating-point numbers
– Show program, then discuss
1 <html>
2 <applet code = "AdditionApplet.class" width = "300" height = "65">
3 </applet>
4 </html>
Program Output
– Field declaration
• Each object of class gets own copy of the field
• Declared in body of class, but not inside methods
– Variables declared in methods are local variables
– Can only be used in body of method
• Fields can be used anywhere in class
• Have default value (0.0 in this case)
– Method init
• Normally initializes fields and applet class
• Guaranteed to be first method called in applet
• First line must always appear as above
– Returns nothing (void), takes no arguments
13 {
• Method JOptionPane.showInputDialog
• Prompts user for input with string
• Enter value in text field, click OK
– If not of correct type, error occurs
– In Chapter 15 learn how to deal with this
• Returns string user inputs
• Assignment statement to string
– Lines 25-26: As above, assigns input to secondNumber
– Assignment statement
• sum an field, can use anywhere in class
– Not defined in init but still used