Download presentation
Presentation is loading. Please wait.
Published byChristina Banks Modified over 9 years ago
1
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet Methods
2
Programming and Problem Solving With Java 2 Applets Kinds of programs in Java Applications: standalone programs Applets: run inside a Web browser window Browser downloads and executes Java applets automatically Java applets usually stored on Web server JVM in browser executes the Java code Advantage of applets Only deploy once (on server) In 2-tier client/server, each client has copy of client code (must deploy new code to all clients) Applets have sparked revolution in computing
3
Programming and Problem Solving With Java 3 What’s an Applet? Program that runs inside a Web browser Browser contains JVM for executing Java programs The browser has the main() method -- it calls methods in the applet What about security? Can a downloaded Java program wreck your computer? Not easily -- downloaded Java program is restricted in what it can do Applet can read or write files on server Allows applet to save information to server’s database (for example)
4
Programming and Problem Solving With Java 4 What’s an Applet? Example applet Must extend the Applet class // A simple Java applet that displays the // word "Hello" import java.applet.*; import java.awt.*; public class Hello extends Applet { public void init() { this.add(new Label("Hello!")); } Compile the same way as usual (javac Hello.java)
5
Programming and Problem Solving With Java 5 What’s an Applet? Can’t run applet with java Hello Must create a Web page to run it Should be in same directory as Java applet (Hello.class) HTML Page for Testing Applets HTML Page for Testing Applets This is some ordinary HTML text, which is part of an ordinary HTML document. We can include Java applets to spice up the document. For example, here is my "Hello" applet: That's the end of the document!
6
Programming and Problem Solving With Java 6 What’s an Applet?
7
Programming and Problem Solving With Java 7 HTML Skeleton HTML document for applets Title for browser window... Text and HTML formatting tags here... HTML Formatting tags (anything between ) Beginning tag: Ending tag: ... is for document info ... is what browser displays
8
Programming and Problem Solving With Java 8 HTML Common HTML tags
9
Programming and Problem Solving With Java 9 HTML APPLET tag Example Minimum attributes CODE: tells browser name of the Java class file (should be in same directory as HTML document) WIDTH: width of the applet in pixels HEIGHT: height of the applet in pixels
10
Programming and Problem Solving With Java 10 Applet Viewer Can be tedious to load applet into HTML document in full browser Applet viewer Small program (much smaller than browser) Reads text file, looks for APPLET tag Text file can be HTML, but doesn’t need to be (just needs APPLET tag) Will start any applets (marked with APPLET tags) in the text file Example appletviewer Applets.html
11
Programming and Problem Solving With Java 11 Applet Viewer (A Little Trick...) Applet viewer reads any text file Therefore, put APPLET tag in applet! (documentation!!) Needs to be a Java comment // A simple Java applet that displays the word "Hello" /* Sample tag: */ import java.applet.*; import java.awt.*; public class Hello extends Applet { public void init() { this.add(new Label("Hello!")); } To compile and run javac Hello.java appletviewer Hello.java
12
Programming and Problem Solving With Java 12 LoanPayment Applet
13
Programming and Problem Solving With Java 13 LoanPayment Applet Very similar to writing GUI application Define buttons, labels, and text fields Choose and initialize the layout manager Connect the bought in to a listener object actionPerformed() method for handling events Differences Import java.applet.* Extend the Applet class, not Frame Initialization code goes in init(), not constructor Don't call parent class constructor Don't set the size of the applet--browser gets size from APPLET tag in HTML document No main() -- browser starts applet
14
Programming and Problem Solving With Java 14 LoanPayment Applet // An applet that computes the payment on a loan, // based on the amount of the loan, the interest rate, and the // number of months /* Sample tag: */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.text.*; public class LoanPayment extends Applet implements ActionListener { // Interface variables TextField amountTextField = new TextField(15); TextField rateTextField = new TextField(15); TextField monthsTextField = new TextField(15); Button computePaymentButton = new Button("Compute payment"); Label resultLabel = new Label();
15
Programming and Problem Solving With Java 15 LoanPayment Applet public void init() { // Choose the layout manager and initialize it this.setLayout(new GridLayout(4, 2)); // Put components on the applet this.add(new Label("Loan amount: ")); this.add(this.amountTextField); this.add(new Label("Annual interest rate: ")); this.add(this.rateTextField); this.add(new Label("Months: ")); this.add(this.monthsTextField); this.add(this.computePaymentButton); this.add(this.resultLabel); // Register the button with the listener object this.computePaymentButton.addActionListener(this); }
16
Programming and Problem Solving With Java 16 LoanPayment Applet // actionPerformed: Compute the loan payment based on the amount // of the loan, the annual interest rate, and the // months of the loan public void actionPerformed(ActionEvent event) { NumberFormat formatter = NumberFormat.getInstance(); try { double loanAmount = formatter.parse(amountTextField.getText()).doubleValue(); double interestRate = formatter.parse(rateTextField.getText()).doubleValue(); double months = formatter.parse(monthsTextField.getText()).doubleValue(); // Convert annual interest rate to monthly interestRate = interestRate / 12; double payment = loanAmount / ((1 - Math.pow(1 + interestRate, -months)) / interestRate); this.resultLabel.setText("Payment is " + payment); } catch (ParseException e) { this.resultLabel.setText("Error in values"); }
17
Programming and Problem Solving With Java 17 Applet Parameters Can pass values from HTML document to applet Applet parameter Text value in HTML Applet can read value Can make applet do different things in different contexts Reduces the number of applets we need to write Example HTML In applet String natLanguage = getParameter("naturalLanguage"); Applet can test the value (String.equals()) and act on it
18
Programming and Problem Solving With Java 18 Applet Parameters Can pass any number of parameters from HTML to applet Example HTML In applet String natLanguage = getParameter("naturalLanguage"); String userName = getParameter("userName"); String password = getParameter("password");
19
Programming and Problem Solving With Java 19 Applet Parameters (LoanPayment) Updated LoanPayment program uses applet parameter to tell whether interest rate is monthly or annual // An applet that computes the payment on a loan, // based on the amount of the loan, the interest rate, and the // number of months Takes one parameter from the browser: // monthlyRate. If true, the interest rate is per month; otherwise, // the interest rate is annual. (The differences between this // listing and Listing F.5 are highlighted.) /* Sample tag: */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.text.*;
20
Programming and Problem Solving With Java 20 Applet Parameters (LoanPayment) public class LoanPayment extends Applet implements ActionListener { // Interface variables TextField amountTextField = new TextField(15); TextField rateTextField = new TextField(15); TextField monthsTextField = new TextField(15); Button computePaymentButton = new Button("Compute payment"); Label resultLabel = new Label(); boolean monthlyRate; public void init() { // Get the monthlyRate parameter from the browser this.monthlyRate = getParameter("monthlyRate").equals("true"); // Choose the layout manager and initialize it this.setLayout(new GridLayout(4, 2)); // Put components on the applet this.add(new Label("Loan amount: ")); this.add(this.amountTextField);
21
Programming and Problem Solving With Java 21 Applet Parameters (LoanPayment) if (this.monthlyRate) { this.add(new Label("Monthly interest rate: ")); } else { this.add(new Label("Annual interest rate: ")); } this.add(this.rateTextField); this.add(new Label("Months: ")); this.add(this.monthsTextField); this.add(this.computePaymentButton); this.add(this.resultLabel); // Register the button with the listener object this.computePaymentButton.addActionListener(this); }
22
Programming and Problem Solving With Java 22 Applet Parameters (LoanPayment) // actionPerformed: Compute the loan payment based on the amount // of the loan, the annual interest rate, and the // months of the loan public void actionPerformed(ActionEvent event) { NumberFormat formatter = NumberFormat.getInstance(); try { double loanAmount = formatter.parse(amountTextField.getText()).doubleValue(); double interestRate = formatter.parse(rateTextField.getText()).doubleValue(); double months = formatter.parse(monthsTextField.getText()).doubleValue(); // Convert annual interest rate to monthly (if applicable) if (!this.monthlyRate) { interestRate = interestRate / 12; }
23
Programming and Problem Solving With Java 23 Applet Parameters (LoanPayment) double payment = loanAmount / ((1 - Math.pow(1 + interestRate, -months)) / interestRate); this.resultLabel.setText("Payment is " + payment); } catch (ParseException e) { this.resultLabel.setText("Error in values"); }
24
Programming and Problem Solving With Java 24 Applet Parameters (LoanPayment) HTML document for LoanPayment <!-- This HTML document shows how to use an applet parameter to make the same applet behave differently, depending on the parameter value. The document loads the LoanPayment applet (Listing F.6) with the monthlyRate parameter set to "false", then loads the LoanPayment applet with the monthlyRate parameter set to "true". --> Loan Payment Computation Loan Payment Computation This page contains two copies of one applet that computes the payment on a loan. The applet bases this on the amount of the loan, the interest rate, and the number of months of the loan. Use the following copy of the applet if you want to enter an annual interest rate.
25
Programming and Problem Solving With Java 25 Applet Parameters (LoanPayment) Use the following copy of the applet if you want to enter a monthly interest rate. Thank you for trying the Loan Payment Computation Web page!
26
Programming and Problem Solving With Java 26 Applet Parameters (LoanPayment)
27
Programming and Problem Solving With Java 27 Graphics in Applets Almost the same as writing graphics applications Example // Demonstration of the drawLine() method in an applet /* Sample tag: */ import java.applet.*; import java.awt.*; public class DrawLine extends Applet { // paint: Display a square with an X inside public void paint(Graphics g) { g.drawLine(40, 40, 40, 160); g.drawLine(40, 160, 160, 160); g.drawLine(160, 160, 160, 40); g.drawLine(160, 40, 40, 40); g.drawLine(40, 40, 160, 160); g.drawLine(40, 160, 160, 40); }
28
Programming and Problem Solving With Java 28 Other Applet Methods We’ve seen init(), paint(), repaint() Use init() like a constructor -- set layout manager, put components on window Use paint() to draw Use repaint() to tell system to use paint() again Other methods stop(): Called when user scrolls applet off screen. Use to stop animation. start(): Called when user scrolls applet back on screen. Use to start animation. destroy(): Called when user leaves browser page. Use to clean up (close files, etc.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.