Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.

Slides:



Advertisements
Similar presentations
Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
Advertisements

Internet Services and Web Authoring (CSET 226) Lecture # 5 HyperText Markup Language (HTML) 1.
Dept. of Computer Science - SSBN Java Applets Vishnuvardhan.M.
HTML. The World Wide Web Protocols Addresses HTML.
1 Chapter 9 Introduction to HTML and Applets Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 4 Marking Up With Html: A Hypertext Markup Language Primer.
Hypertext Markup Language. Platform: - Independent  This means it can be interpreted on any computer regardless of the hardware or operating system.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
COMP 14 Introduction to Programming Miguel A. Otaduy June 8, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 21, 2005.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
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 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Happy Tax Day! l Project 8 due on Thursday.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
MA10126 Introduction to HTML Gavin Shaddick
Chapter 131 Applets and HTML Chapter Reminders Project 8 due Dec 10:30 pm Project 6 regrades due by midnight tonight –Submit all files (including.
HTML Companion. Lecture Objectives Learn about HTML. Know basic HTML tags.
Chapter 20 Applets Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Developing a Basic Web Page with HTML
Applets, HTML and GUI’s Recitation – 04/11/2008 CS 180 Department of Computer Science, Purdue University.
Glencoe Digital Communication Tools Create a Web Page with HTML Chapter Contents Lesson 4.1Lesson 4.1 Get Started with HTML (85) Lesson 4.2Lesson 4.2 Format.
Chapter 4 Fluency with Information Technology L. Snyder Marking Up With HTML: A Hypertext Markup Language Primer.
Exploring Microsoft Office XP - Microsoft Word 2002 Chapter 61 Exploring Microsoft Word Chapter 6 Creating a Home Page and Web Site By Robert T. Grauer.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Chapter 16 The World Wide Web. 2 The Web An infrastructure of information combined and the network software used to access it Web page A document that.
Understanding HTML Code
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.
XP Mohammad Moizuddin Creating Web Pages with HTML Tutorial 1 1 New Perspectives on Creating Web Pages With HTML Tutorial 1: Developing a Basic Web Page.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Introduction to HTML. What is a HTML File?  HTML stands for Hyper Text Markup Language  An HTML file is a text file containing small markup tags  The.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Web Programming Basics of HTML. HTML stands for Hyper Text Mark-up Language A mark-up language is different than those that you have learned before in.
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.
HTML Hyper Text Markup Language. What is an HTML File?  HTML stands for Hyper Text Markup Language  An HTML file is a text file containing small markup.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Chapter 8 Introduction to HTML and Applets Fundamentals of Java.
1 Week 1 l HTML l Applets Applets and HTML. 2 Overview l Applets: Java programs designed to run from a document on the Internet l HTML: Hypertext Markup.
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
HTML BTEC National in Computing Section5. Create Information “HTML: defining HTML, discussing HTML uses and demonstrating HTML basics, HTML structure…..
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.
Introduction to Computers 12/6/ BEGINNING HTML DESIGNING YOUR OWN WWW PAGES.
Chapter 131 Applets and HTML Chapter Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in.
HTML Basics. HTML Coding HTML Hypertext markup language The code used to create web pages.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CS 100 Introduction to Web Page Construction and HTML.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Introduction to HTML.
Chapter 8 Introduction to HTML and Applets
Java Applets.
Java Applets.
Chapter 13 Applets and HTML HTML Applets.
Presentation transcript:

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Overview l Applets: Java programs designed to run from a document on the Internet l HTML: Hypertext Markup Language »a language used to create Internet documents l Applets run within an HTML document l HTML documents are run by a web browser, e.g. »Netscape Navigator or »Microsoft Internet Explorer

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 3 HTML: H yper T ext M arkup L anguage l Hypertext: text with items you can click with a mouse to send you to another document l Link: an item you can click with the mouse to go to another document »also called hyperlink l Page: an HTML document »it is not equal to a page of print or a single screen of display »HTML page and HTML document mean the same thing l HTML is a "suggestive formatting" language »it is not a full-blown programming language like Java »it suggests to the browser how to display text or other objects

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 4 HTML Basics l Most HTML commands are of the form some text l Some commands do not require the part l HTML is not case sensitive »i.e. the commands are not »text displays in whatever case it is in in the document l HTML files are plain text files created with any text editor l HTML file names should follow the operating system's rules and end in.html

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 5 A Few HTML Commands Page Title Heading 1- largest, most prominent heading text Heading 2 - a sub-heading, etc. Additional sub-headings Break - begin a new line ( not required) Paragraph - begin a new paragraph ( not required) Comment field - not displayed by browser text Center text horizontally on page Used for browser "book marks" (displays in browser title bar)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Outline of a Basic Page and a Few More HTML Commands begin an HTML document begin the document's header (title usually goes here) end of document's header begin stuff that will appear in the main window of the browser blank lines are ignored - use them to make code more readable (body of html document: html commands, text, applets, etc.) end stuff that displays in main window of browser date and link to the address that follows ( address of person maintaining the page) end address end the HTML document

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 7 HTML: Suggestive Formatting l Most HTML commands suggest rather than dictate how text will be displayed l For example, Heading 1 in one browser may be in Arial font but another browser may display it in Times New Roman »the font size and other attributes may differ as well l Different browsers and systems also result in different line breaks, colors, etc., so WYSIWYG (What You See Is What You Get) does not apply to either the text HTML file or what displays in your browser »getting HTML documents to display nicely in all browsers and systems is important but non-trivial »the examples used here are simple and should not be a problem, so we will not go into any detail about differences in browsers

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Inserting a Hyperlink Hyperlinks - The real power of HTML l Syntax: text to display »the path can be local - any absolute or relative path to a local file »or... It can be a URL (internet address) plus path name to a file on a remote system! l Example: Walter Savitch Displays as: Walter Savitch

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Java Club Home Page Java Club Club Purpose A major goal of the club is to encourage its members to become good programmers, The club provides a setting where people who like to program in the Java language can meet and talk with other like-minded programmers. Display 13.2 Simple HTML Document (1/2) Display 13.2 Simple HTML Document (1/2)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Meeting Times The first Wednesday of each month at 7 PM. Sun Microsystems Java Website Click here for the website January 1, 2001 Display 13.2 Simple HTML Document (2/2) Display 13.2 Simple HTML Document (2/2)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Browser View of Display 13.2

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 12 URL: Uniform Resource Locator l An internet address that uniquely identifies a location l A kind of path name for the Internet l But not just an ordinary address »the first part tells what protocol to use to make the link »a protocol is a set of rules for transferring and interpreting data »both ends of a link must implement the same protocol to have intelligible communications »http and ftp are two examples of Internet protocols »http is the name of the protocol for HTML documents

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Automatic Documentation with Java Standard Java includes a utility called javadoc that automatically generates documentation files from source code for class definitions Usage: javadoc yourFile.java javadoc produces an HTML document that can be read in a browser »it includes selected lines from the source file, e.g. comments and headers javadoc is described in an appendix

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Displaying a Picture l Although this chapter does not deal with pictures, here is the HTML command for inserting a picture in a document: For example, if the picture file you want is named mypicture.gif and it is in the images subdirectory of the directory that contains your HTML document: l Various picture formats may be used and are generally indicated by the ".xxx" extension in the file name ».gif and.jpg are two common ones

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Applets l Applet: small application l Applets are Java programs embedded in HTML documents »therefor they run in a browser that views the document They are a derived class of the class J Applet, which is derived from Applet. Applet is in turn derived from Panel. »therefor it is both a Container and Component JApplet class is not in the AWT, Swing, or basic Java »it is in the applet library, so import java.applet.*

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Placing JApplet in the Class Hierarchy Class Abstract Class Key: If there is a line between two classes, then the lower class is a derived class of the higher class. AWT Object Component Container Window Panel Frame JApplet Applet Swing JApplet is part of Swing and inherits from the Applet class. The Applet class is older than JApplet and is not part of AWT or Swing.

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Applet Basics When writing applets your list of import statements will usually look like this: import javax.swing.*; import java.awt.*; import java.awt.event.*; Applets do not normally have a main method

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Applet Basics Applets do not normally use constructors but they do use a method named init for similar purposes: »setting colors »adding buttons »adding text fields, etc. »the init method has no parameters l Th HTML document takes care of sizing the applet l Use a layout manager when defining an applet »use the setLayout method

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 19 Example: a Trivial Applet HelloApplet.java Defining an applet

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Running an Applet from an HTML Page The normal way to run an applet is from an HTML document. Syntax: Example: Note:use the compiled file extension.class, not the source extension.java

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 21 Testing a Java Applet Another way to run an applet is usually used just to test it: »put the applet in an HTML document –good practice is to name the HTML document the same as the applet, but with ".html" instead of ".java" »then run it using appletviewer »like javadoc, javac, and java, appletviewer comes with the JDK (Java Development Kit) Syntax: appletviewer Applet_File_Name.html Example: appletviewer FirstApplet.html

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 22 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class AdderApplet extends JApplet implements ActionListener { private JTextField inputOutputField; private double sum = 0; public void init() { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.gray); buttonPanel.setLayout(new FlowLayout()); JButton addButton = new JButton("Add"); addButton.addActionListener(this); buttonPanel.add(addButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); contentPane.add(buttonPanel, BorderLayout.SOUTH); JPanel textPanel = new JPanel(); textPanel.setBackground(Color.blue); textPanel.setLayout(new FlowLayout()); inputOutputField = new JTextField("Numbers go here.", 30); inputOutputField.setBackground(Color.white); textPanel.add(inputOutputField); contentPane.add(textPanel, BorderLayout.CENTER); } Display 13.6 Adder Applet (1/2) Display 13.6 Adder Applet (1/2)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 23 public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Add")) { sum = sum + stringToDouble(inputOutputField.getText()); inputOutputField.setText(Double.toString(sum)); } else if (e.getActionCommand().equals("Reset")) { sum = 0; inputOutputField.setText("0.0"); } else inputOutputField.setText("Error in adder code."); } private static double stringToDouble(String stringObject) { return Double.parseDouble(stringObject.trim()); } Display 13.6 Adder Applet (2/2) Display 13.6 Adder Applet (2/2)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 24 Resulting GUI (Using an Applet Viewer)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 25 Budget Help The Budget Help Home Page Helpful Hints for a Balanced Budget Pay off your credit cards every month. Do not spend more than you earn. Display 13.7 HTML Document with an Applet (1/2) Display 13.7 HTML Document with an Applet (1/2)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 26 Here is an adder to help you plan your budget: December 31, 2001 Display 13.7 HTML Document with an Applet (2/2) Display 13.7 HTML Document with an Applet (2/2)

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 27 Browser View of Display 13.7

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 28 Another Way to Test an Applet l Use the appletviewer in an integrated Java development environment l There are many commercial development environments that provide handy tools to write, compile, run and debug Java programs l Typically they have a menu selection something like "Run" or "Execute" to run an application »usually they will automatically create an applet viewer if you run an applet

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 29 Converting a Swing Application to an Applet Steps to convert an application using JFrame to an applet: 1. Derive the class from JApplet instead of deriving it from JFrame 2. Remove the main method. 3. Replace the constructor with a method named init. Some statements from the constructor will be removed as in the following steps. 4. Delete any invocation of AddWindowListener. 5. Delete any invocation of setTitle. 6. Delete any invocation of setSize. 7. Make an HTML page with an APPLET tag that refers to the byte- code (. class ) version of the applet.

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 30 import javax.swing.*; import java.awt.*; public class DukeApplet extends JApplet { public void init() { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); JLabel spacer = new JLabel(" "); contentPane.add(spacer, "West"); JLabel niceLabel = new JLabel("Java is fun!"); ImageIcon dukeIcon = new ImageIcon("duke_waving.gif"); niceLabel.setIcon(dukeIcon); getContentPane().add(niceLabel, BorderLayout.CENTER); } Adding Icons to an Applet Uses content pane as JFrame does Add icon to label Add label to content pane of applet

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 31 Resulting GUI

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 32 The Older Applet Class Older versions of web browsers can't run applets that use JApplet and other Swing classes. l Using the old Applet class instead of JApplet will allow your applet to run in more browsers. l To convert a JApplet program to an Applet program: »Replace: import javax.swing.*; with import java.applet.*; »Replace Swing classes that start with J (JButton, JPanel, etc.) with AWT classes that don't start with J (Button, Panel, etc.) »Remove references to the content pane of the applet.

Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 33 Summary l HTML is a language for writing documents to be read across the Internet or with a web browser. l HTML stands for Hypertext Markup Language. l Applets are Java programs designed to be placed in and run from an HTML document. Applets are derived from the class Applet and are similar to AWT GUIs derived from the class Frame.