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

Similar documents
Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

GUI, Events and Applets from Applications, Part III

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Part 3: Graphical User Interface (GUI) & Java Applets

Window Interfaces Using Swing Objects

CSC 1214: Object-Oriented Programming

Window Interfaces Using Swing Objects

Introduction. Introduction

Chapter 12 Advanced GUIs and Graphics

Introduction to the JAVA UI classes Advanced HCI IAT351

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

CS 251 Intermediate Programming GUIs: Components and Layout

Chapter 3 - Introduction to Java Applets

G51PRG: Introduction to Programming Second semester Applets and graphics

Swing from A to Z Some Simple Components. Preface

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

More Swing. CS180 Recitation 12/(04,05)/08

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

1. What is Jav a? simple

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

JFrame In Swing, a JFrame is similar to a window in your operating system

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Introduction to Computer Science I

PROGRAMMING LANGUAGE 2

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Swing. By Iqtidar Ali

Programming graphics

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Creating Web Pages Using Netscape Composer AGENDA FOR THIS WORKSHOP. 1. How does it all work? 2. What do I need to get started at Fairfield?

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Graphical User Interfaces (GUIs)

Summary. 962 Chapter 23 Applets and Java Web Start

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

GoLive will first ask you if your new site will be for one individual or a work group; select for a Single User, and click Next.

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphical User Interfaces. Comp 152

Graphics User Defined Forms, Part I

Java. GUI building with the AWT

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Chapter 14. More Swing

SELF-STUDY. Glossary

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Graphical User Interfaces. Swing. Jose Jesus García Rueda

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

PIC 20A GUI with swing

Packages: Putting Classes Together

GETTING STARTED. The longest journey begins with a single step. In this chapter, you will learn about: Compiling and Running a Java Program Page 2

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Netscape Composer Tutorial

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Review Ch. 17 Creating Online Pages and Sites. 2010, 2006 South-Western, Cengage Learning

ADOBE DREAMWEAVER CS4 BASICS

FCKEditor v1.0 Basic Formatting Create Links Insert Tables

HTML OBJECTIVES WHAT IS HTML? BY FAITH BRENNER AN INTRODUCTION

Core JAVA Training Syllabus FEE: RS. 8000/-

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

1.264 Lecture 12. HTML Introduction to FrontPage

A Brief Introduction to HTML

Html basics Course Outline

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

First Name: AITI 2004: Exam 2 July 19, 2004

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

CS Exam 1 Review Suggestions

Java Applets This is not a Java course! (You re supposed to know about Java.)

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Authoring World Wide Web Pages with Dreamweaver

First Name: AITI 2004: Exam 2 July 19, 2004

Java Help Files. by Peter Lavin. May 22, 2004

encompass a group of features for building Graphical User Interfaces (GUI).

c122jan2714.notebook January 27, 2014

request HTML Document send HTML Document

m ac romed ia D r e a mw e av e r Curriculum Guide

Java IDE Programming-I

FIT 100 LAB Activity 3: Constructing HTML Documents

Part I: Learn Common Graphics Components

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

FrontPage 2000 Tutorial -- Advanced

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

CS 3331 Advanced Object-Oriented Programming Final Exam

Web Design and Development ACS-1809

HTML. Hypertext Markup Language. Code used to create web pages

Original GUIs. IntroGUI 1

More About Objects and Methods

Web Page Creation Part I. CS27101 Introduction to Web Interface Design Prof. Angela Guercio

CS2 Advanced Programming in Java note 8

DREAMWEAVER QUICK START TABLE OF CONTENT

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Hypertext Markup Language, or HTML, is a markup

Transcription:

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

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

Applets Applet: small application 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 JApplet, which is derived from Applet. Applet is in turn derived from Panel.» therefor it is both a Container and Component JApplet class is in the Swing library so import javax.swing.* Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 3

Placing JApplet in the Class Hierarchy AWT Object Component 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. Key: Container Window Panel Frame Applet Swing JApplet Class Abstract Class If there is a line between two classes, then the lower class is a derived class of the higher class. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 4

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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 5

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 Th HTML document takes care of sizing the applet Use a layout manager when defining an applet» use the setlayout method Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 6

Example: a Trivial Applet HelloApplet.java import javax.swing.*; import java.awt.*; // for Container class public class HelloApplet extends JApplet { } public void init(); { Container contentpane = getcontentpane(); contentpane.setlayout(new FlowLayout()); } Defining an applet JLabel myfirstlabel = new JLabel("Hello out there!"); contentpane.add(myfirstlabel); Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 7

Running an Applet One way to run an applet:» 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 8

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 bytecode (.class) version of the applet. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 9

Icons and the Class ImageIcon An icon is a small picture» not really required to be small in Java Class ImageIcon is used to convert a picture file to a Swing icon Syntax: ImageIcon Name_Of_ImageIcon = new ImageIcon(Picture_File_Name);» Picture_File_Name is a string giving a path name to the picture file Example: ImageIcon SmileyFaceIcon = new ImageIcon("smiley.gif"); Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 10

import javax.swing.*; import java.awt.*; Adding Icons to an Applet public class DukeApplet extends JApplet { public void init() Uses content pane as { JFrame does 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); Add icon to label getcontentpane().add(nicelabel, BorderLayout.CENTER); } Add label to content pane of applet Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 11

HTML: HyperText Markup Language Hypertext: text with items you can click with a mouse to send you to another document Link: an item you can click with the mouse to go to another document» also called hyperlink 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 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 12

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

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

Outline of a Basic Page and a Few More HTML Commands <HTML> <HEAD> begin an HTML document begin the document's header (title usually goes here) </HEAD> end of document's header <BODY> 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.) </BODY> end stuff that displays in main window of browser <ADDRESS> date and link to the email address that follows (email address of person maintaining the page) </ADDRESS> end email address </HTML> end the HTML document Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 15

HTML: Suggestive Formatting Most HTML commands suggest rather than dictate how text will be displayed 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 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 16

Inserting a Hyperlink Hyperlinks - The real power of HTML Syntax: <A HREF="Path_To_Document"> text to display</a>» 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! Example: <A HREF="http://www-cse.ucsd.edu/users/Savitch"> Walter Savitch</A> Displays as: Walter Savitch Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 17

URL: Uniform Resource Locator An internet address that uniquely identifies a location A kind of path name for the Internet 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 18

Gotcha: Not Using Your Reload (Refresh) Button Browsers keep copies of most recently used HTML pages» runs faster because it can recover pages much faster» might not show changes that you make to a page Click Reload, Refresh (or similar) button» browser will reload page and show you the latest version Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 19

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 Appendix 9 Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 20

Displaying a Picture Here is the HTML command for inserting a picture in a document: <IMG SRC="Picture_File"> 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: <IMG SRC="images/mypicture.gif> 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 21

HTML Is a Low-Level Language HTML for web browser is analogous to assembly language for a computer» many details that designer must take care of Most web page designers now use a high-level design language that translates into HTML Three examples» Dreamweaver (Macromedia, Inc.)» FrontPage (Microsoft Corporation)» GoLive (Adobe Systems Inc.) Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 22

Running an Applet from an HTML Page The normal way to run an applet is from an HTML document. Syntax: <APPLET CODE="Name_Of_.class_File" WIDTH=Integer HEIGHT=Integer> </APPLET> Example: <APPLET CODE="FirstApplet.class" WIDTH=300 HEIGHT=200> </APPLET> Note:use the compiled file extension.class, not the source extension.java Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 23

Another Way to Test an Applet Use the appletviewer in an integrated Java development environment There are many commercial development environments that provide handy tools to write, compile, run and debug Java programs 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 24

Gotcha: Using an Old Web Browser Updates to browsers are usually much later than updates to core Java language.» Web browsers don't use the same Java interpreter that runs Java applications. Test your applets in different browsers and in different versions of browsers.» Can't expect everyone to have most recent browser.» Might need to use older Applet class if you want your applet to run in older browsers. Can run applets in appletviewer, but if they're not going to be part of an HTML page you might as well make them applications. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 25

The Older Applet Class Older versions of web browsers can't run applets that use JApplet and other Swing classes. Using the old Applet class instead of JApplet will allow your applet to run in more browsers. 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 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 26

Applets and Security Applets are someone else's program running on your computer. Security issues:» viruses» reading confidential information» corrupting operating system Security features of applets to help avoid these problems:» Applets cannot run any programs on your computer.» Applets cannot read or write to files on your computer (unless the applet originated on your computer). Other programs that run through your browser might cause security problems. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 27

Summary HTML is a language for writing documents to be read across the Internet or with a web browser. HTML stands for Hypertext Markup Language. 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. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 28