1 Recitation 09. Applications and Applets Java applications Those who took CS100J and CS100M in fall 2003 may not have had much experience with Java applications.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 CS100J 12 April 2007 Applications and Applets Read Chapter 16 of the text I never let my schooling interfere with my education. Mark Twain Learning makes.
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. 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.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
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.
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.
Applets, HTML and GUI’s Recitation – 04/11/2008 CS 180 Department of Computer Science, Purdue University.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets, AWTS CompSci 230 Software Construction.
Web Design Using HTML Codes. WHAT DO I NEED TO BEGIN DESIGNING A HOME PAGE? 1.YOU NEED A FOLDER (also called a DIRECTORY) You should set up a folder or.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Essential HTML coding By Fadi Safieddine (Week 2)
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.
Internet Software Development Applets Paul J Krause.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1 Introduction to Applets. 2 Applications Programs that execute using the java interpreter Executes in command windows.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
GUI programming Graphical user interface-based programming.
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
POS 406 Java Technology And Beginning Java Code
1 CS100J 2 December 2008 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from.
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.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
1 CS Nov 2011 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from a real-life.
Chapter 6 Applets and HTML  Overview  HTML tags for Applets  Applet Life Cycle  Applet Class  JAR files.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
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 GUI programming Graphical user interface-based programming Chapter G1 (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.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
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 CS100J 08 November 2005: Applications and Applets Java application; How to produce a stand-alone application, to be run without using DrJava or any such.
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 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
We also look at html, since we need it to use applets.
Lecture 09 Applets.
We also look at html, since we need it to use applets.
Graphical user interface-based programming
UNIT-5.
Introduction to Computing Using Java
Java Applets.
Applet in Java.
We also look at html, since we need it to use applets.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
G6DICP - Lecture 27 Java Applets.
CS100J 03 May 2005 Applications and Applets (Chapter 16 of the text)
Presentation transcript:

1 Recitation 09. Applications and Applets Java applications Those who took CS100J and CS100M in fall 2003 may not have had much experience with Java applications. A Java application is simply a collection of classes one of which contains one method with this header: public static void main(String[] pars) In a terminal or DOS window, one executes an appli- cation whose method main is in class C (file C.java) by navigating to the directory containing the program files and executing java C You can type this in the DrJava Interactions pane, too. The argument to main can be an array of Strings, and method main can use these as input to it. We don’t handle that here. jar files (Java Archive files) A program may consist of many classes, generally in the same directory. If you want to give someone your program, you have to give them all the.class files —zip them up into a.zip file— and the someone must then unzip them. That's messy. To make things easier, you can make a jar file of the classes. Jar stands for Java ARchive; after tar files (TapeARchives) on Unix systems. To make a jar file, get into a DOS or command-line window and navigate to the directory where the.java and.class files are. Then, type in this command: jar -cf file-name.jar *.class The c is for create. The f is for file and indicates that the name of the file to create follows: file-name.jar. The *.class is expanded to name all the.class files in the directory. So, this command makes up a jar file named file-name.jar that contains all the.class files in the directory. You still have to insert into the jar file something that tells it which class has method main. Suppose it is class MainClass. Then do the following: (a)Make up a file x.mf that contains one line of text in it: Main-class: MainClass The suffix on x.mf stands for manifest. You actually don't have to use the suffix. Make sure you hit the enter key after typing the text; there must be a carriage-return or line-feed in it. You can create this file in wordpad or notepad or DrJava or any editor you want. Make sure the file is in the same directory as file file-name.jar. (b) Type this command in the window: jar -umf x.mf file-name.jar The u stands for update, the m for manifest, and the f for file. Since the m comes before the f, the manifest file name, x.mf, comes before the file name. This command inserts into jar file file-name.jar the fact that method main appears in class MainClass. You can do both steps together —insert the classes and the main-class indication— by using the command jar -cmf x.mf file-name.jar *.class You can now file file-name.jar to anyone, and they can run it on their computer, whether it has a Unix, Macintosh, or Windows system, as long as their system has Java in it. To execute the program in the jar file, type this line (yes, include the extension.jar): java -jar file-name.jar In some systems, you will also be able to run the program just by double-clicking on the jar file. If you want to see what is in jar file file-name.jar, then type this: jar tvf file-name.jar You can find out more about the jar command by typing simply jar and hitting the return/enter key.

2 Recitation 09. Applications and Applets 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 suitable applet tag. Here, we explore the difference between applications (the kind of programs we have been writing) and applets. Classes Applet and JApplet. Any Java program that is to be executed by a browser has to be a subclass of either Applet (in package java.awt) or JApplet (in the newer Swing package, javax.swing).(Class JApplet is a subclass of Applet). Here is a tutorial on writing Japplets: An application is started by calling static method main, right? An applet, instead, has five methods, all of which are nonstatic, that are called by the browser at various times. Som of these should be overridden for the applet to do its job. The inherited versions of these method do nothing (but very fast). Method init() is called to tell the Java program that it has been loaded into the system. It is always called be- fore the first time that method start is called. Override this to perform initialization (e.g. start a thread).. Method start() is called by the browser to inform this applet that it should start its execution. It is called after method init and each time the applet is revisited in a Web page. (E.g. if the applet window is hidden, method stop is called, which could stop anything that the applet is doing, like providing an animation. When the window becomes visible again, method start is called, and it can continue execution). Method stop() is called when the applet should stop whatever it is doing because the applet is no longer visible --see the discussion of method start. Method destroy is called just before the applet is terminated by the browser. In method destroy, the applet can kill its resources --e.g. threads that were started in method init. Method paint(Graphics g). An applet occupies a space in the window for the html page. It can be painted, just like any Panel. Our applets will not use start, stop, and destroy. An html file resides on one computer and is sent to your computer, where the browser loads it and shows it in a window. The applet typically is on the same computer (in the same folder) as the html page. It, too, is sent to your computer, where your browser executes it. You know about tags like and on html pages. Consider these two tags <applet codebase="Java Classes" code="TrivialApplet.class" width=200 height=200> These tell the browser to start a Java applet, which is in TrivialApplet.class. The applet is a 200x200 pixel square. The argument codebase=“Java Classes” tells the browser that the.class files for this program are in folder “Java Classes”, which must be in the same folder as the html page that contains the tag. If there are a lot of class files, the above can be inefficient, because each file is individually retrieved from the place where the html file resides and brought to your compueter. You can have CodeWarrior store all the.class files in compressed form in a single file called a “jar” file, so that only one file has to be retrieved. To do this, in Codewarrior, click on the target setting icon, select “Java output”, and then set the “Output type” to “Jar File”. Then, Codewarrior will produce a jaf files instead of a folder of.class files. Then, use these tags instead of the ones above:

3 Here’s a simple applet that puts some text and a horizontal line on the web page. public class Apple extends JApplet { // Initialize the applet public void init() { } public void paint(Graphics g) { g.drawString( “Hello World!”, 30, 30 ); g.drawLine(30-2, 30+2, 30+70, 30+2); } This applet puts a clock in the window. /* An applet that puts a clock in the window */ import java.awt.*; import java.applet.Applet; import java.util.*; import java.text.*; public class TrivialApplet extends Applet implements Runnable { private final static int SIZE= 8; // No chars in clock private final static int DELAY= 1000; // sleep // time: 1 second private TextField clock; // The clock itself private Thread timer; // Thread that runs the clock public void start(); { timer= new Thread(this); timer.start(); } public void run() { Date time; // The current time SimpleDateFormat f= new SimpleDateFormat("h:mm:ss a"); Thread thisThread = Thread.currentThread(); while (timer != null) { time= new Date(); clock.setText(f.format(time)); try { Thread.currentThread().sleep(DELAY); } catch (InterruptedException e) {} } public void stop() { timer= null; } public void init() { clock= new TextField(SIZE); add(clock); } } As discussed in lecture, a class that implements Runnable has to have method run(). Every time start is called by the browser, it creates a Thread that is attached to this instance. Then, calling timer.start results in method run being called. Method init is called by the system. Init creates a TextField, which will contain the time on the clock, and adds it to the applet (much like one adds labels and buttons, etc., to a Frame). The browser calls method init, and then start, which starts the thread going. When the applet window be- comes hidden, the browser calls method stop, which set timer to null. Method run, and thus the thread, continues until timer becomes null, which means that stop was called. Then, method run, and thus the thread. stops. An instance of class Date contains the time at which it was created. An instance of class SimpleDateFormat describes how to present the date as a String. Here, “h stands for hour, “m” for minute, “s” for second, and “a” for AM-PM. This is the clock applet, viewed in appletviewer

4 HyperTextMarkup Language (HMTL) FacultyApplet This is an Applet! <applet archive="AppletClasses.jar” code="FacultyApplet.class” width=800 height=550> tags start an html page start the “heading” the title for the page start the body, content of the page begin a paragraph begin boldface begin italics start a Java applet line break (no end tag)