Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.

Slides:



Advertisements
Similar presentations
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Advertisements

Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
JAPPLET.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Java applets. Outline What is Java? Java Applications Java Applets Java Applets Securities Summary.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets Yong Choi School of Business CSU, Bakersfield.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Creating a Java Application and Applet
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Lecture 09 Applets.
Ellen Walker Hiram College
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Introduction to Computing Using Java
Java applets 1/3/2019.
Java Programming COMP-417 Applet
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
G6DICP - Lecture 27 Java Applets.
APPLET PROGRAMMING.
Presentation transcript:

Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet Methods

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

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)

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)

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!

Programming and Problem Solving With Java 6 What’s an Applet?

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

Programming and Problem Solving With Java 8 HTML  Common HTML tags

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

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

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

Programming and Problem Solving With Java 12 LoanPayment Applet

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

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();

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); }

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"); }

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

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");

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.*;

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);

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); }

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; }

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"); }

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.

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!

Programming and Problem Solving With Java 26 Applet Parameters (LoanPayment)

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); }

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.)