CS4273: Distributed System Technologies and Programming I Lecture 3: Java Applets and Animations.

Slides:



Advertisements
Similar presentations
1 Applets Programming Enabling Application Delivery Via the Web.
Advertisements

Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 L45 Multimedia: Applets and Applications. 2 OBJECTIVES  How to get and display images.  To create animations from sequences of images.  To create.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
Lecture12 Java Media Framework I. Streaming Media Steaming media simply means we have a stream of media coming through some kind of a media channel. Some.
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.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
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 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,
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
 Pearson Education, Inc. All rights reserved Multimedia: Applets and Applications.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Computer Science [3] Java Programming II - Laboratory Course Lab 7: Multimedia: Applets and Applications Faculty of Engineering & IT Software Engineering.
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.
Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical.
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
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.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
JAPPLET.
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example.
COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java.
PROGARMMING THROUGH JAVA Presentation on IMAGES Group No:3 Presented By: Anthony Narzary (DC2012MCA0002) Chandra Gupta Bora (DC2012MCA0009) Dipankar Saikia.
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.
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.
Field Trip #19 Animations with Java By Keith Lynn.
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,
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Chapter 6 Applets and HTML  Overview  HTML tags for Applets  Applet Life Cycle  Applet Class  JAR files.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()
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.
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
Chapter 8 (Horstmann’s Book) Frameworks Hwajung Lee.
Chapter 8 Frameworks. Frameworks Framework is a set of cooperating classes and interface types that structures the essential mechanisms of a particular.
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.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Chapter 12: Accessing the Web URL (Uniform Resource Locator) class Applet methods –for audio clips –for images –context interface.
Copyright © Curt Hill Applets A different type of program.
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.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
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.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java applets 1/3/2019.
Applet in Java.
Java Programming COMP-417 Applet
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
Presentation transcript:

CS4273: Distributed System Technologies and Programming I Lecture 3: Java Applets and Animations

2 Java Applet Applet is a special kind of Java application programs that are embedded into web pages. It must run in a web-browser (or appletviewer). It is downloaded from the network, and executed locally. It is the idea of develop once and run anywhere. Applet tag in HTML: <APPLET CODEBASE = “DirOrURL” CODE = “Local.class” NAME = “ThisAppletName” WIDTH=pixels HEIGHT=pixels ALIGN = AlignmentValue// LEFT, RIGHT, TOP, …… VSPACE=pixels HSPACE=pixels ALT = “AlternativeTextIfAppletNotWorking” >...

3 Passing Parameters to Applets Applets get parameters from fields: Code of getting the parameter value in the applet: String GreetingWords = getParameter("Greeting");

4 Applet Methods Applets do not have a main routine. Applet class has many methods. You need to override some of them to let applet perform your task. The Applet methods are listed below in the order they are called: init(): called only once when the applet is loaded. It initializes variables and initial screen display. start(): called after init() and every time returned to the page containing the applet after gone off. It can be called repeatedly. paint(Graphics g): it is also called by repaint(). stop(): called when moving off the page. When coming back, start() method is called. It is used to stop time-consuming activities (animation threads) when not seen. destroy(): called when the browser shuts down. It deletes the applet and recalls all resources.

5 Example of an Applet public class music extends JApplet { AudioClip musicOnce, musicLoop; public void init() { add (playBtn = new Button("Play")); playBtn.addActionListener(new BtnAdapter (0)); add (loopBtn = new Button("Loop")); add (stopBtn = new Button("Stop")); stopBtn.addActionListener( new BtnAdapter (2)); musicOnce = getAudioClip(getDocumentBase(), “TAM.au"); musicLoop = getAudioClip(getDocumentBase(), "deng0.au"); } public void start() { musicLoop.loop(); } public void stop() { musicLoop.stop(); } class BtnAdapter implements ActionListener { private int id; BtnAdapter( int buttonID) { id = buttonID; } public void actionPerformed (ActionEvent e) { switch (id) { case 0: musicOnce.play(); break; case 1: musicLoop.loop(); break; case 2: musicLoop.stop(); }

6 paint, repaint & paintComponent Whenever you need to update the display, call repaint() (you cannot call paint() directly). repaint() invokes paint(). In paint(), you usually start with super.paint(), which performs some preparation work for you, e.g., clear the display area. Without calling super.paint(), the paint() will paint on top of the existing drawings. JComponent has method paintComponent(). For painting a sub-region of applet’s display area (i.e., Jcomponent), you need to override paintComponent(), instead of paint(). paint() invokes paintComponent().

7 JApplet is a subclass of JPanel. Create a component and add it to an applet in the same way as adding it to a container, e.g., add(new button(“start”)), add(new Canvas()), etc. Applets and Applications Object Component Container WindowJPanel FrameJApplet

8 Play Audio Files in Applets When playing music, you need to get an AudioClip object and then play it. Get an audio clip by: –AudioClip getAudioClip(URL url, String name) Three methods on AudioClip objects: –play(): it plays the audio file once and stop. –loop(): it plays the audio file repeatedly until the stop method is called. –stop(): it stops a loop play of an audio clip.

9 Example of Playing AudioClips public class audioPlayer extends JApplet { AudioClip musicOnce, musicLoop; public void init() { add (playBtn = new Button("Play")); playBtn.addActionListener(new BtnAdapter (0)); add (loopBtn = new Button("Loop")); add (stopBtn = new Button("Stop")); stopBtn.addActionListener( new BtnAdapter (2)); musicOnce = getAudioClip(getDocumentBase(), “sun.au"); musicLoop = getAudioClip(getDocumentBase(), "deng.au"); } public void start() { musicLoop.loop(); } public void stop() { musicLoop.stop(); } class BtnAdapter implements ActionListener { private int id; BtnAdapter( int buttonID) { id = buttonID; } public void actionPerformed (ActionEvent e) { switch (id) { case 0: musicOnce.play(); break; case 1: musicLoop.loop(); break; case 2: musicLoop.stop(); }

10 Play Video in Applets Java Media Framework (JMF) API is used to play and edit media files. You need to: 1)download JMF package to IE to enable JMF functions and, 2)compile programs with JMF library. The main JMF API includes: A Manager class contains methods for playing and manipulating media clips. Create a player object out of URL of a video clip by using Manager method: Player player = Manager.createRealizedPlayer(mediaURL) Get a video display panel and a control panel, and add them to applet: videoPanel = player.getVisualComponent(); controlPanel = player.getControlPanelComponent(); add(“center”, videoPanel); add(“south”, controlPanel); Control methods on the player object: player.start(); player.stop(); player.close()

11 Example of Playing Video in Applets public class videoPlayer extends JApplet { Player player; public void init() { URL mediaURL = new URL(getDocumentBase(),"bailey.mpg"); player = Manager.createRealizedPlayer(mediaURL); Component video = player.getVisualComponent(); Component controls = player.getControlPanelComponent(); add("Center", video); add("South", controls); } public void start() { if (player != null) player.start(); } public void stop() { if (player != null) player.stop(); }

12 Display Images get an Image by: Image getImage(URL url, String name) paint the image by: drawImage(image, x, y, observer) An example: public class ImageTest extends Applet { { Image theImage = null; public void init() { theImage = getImage(getDocumentBase(), "pg9.gif"); } public void paint(Graphics g) { g.drawImage(theImage, 0, 0, this); }

13 Eample of Animation using Thread public class animation extends JApplet implements Runnable { Thread runner; Vector frames = new Vector(); int cur_frame, frame_delay = 400; public void init() { URL base = getDocumentBase(); for (int i = 1; i <= 8; i++) { Image img = getImage(base, "image/bunny" + i +".gif"); frames.addElement(img);} } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { runner = null; } public void run() { cur_frame = 1; while(runner != null) { Thread.sleep(frame_delay); repaint(); if(cur_frame >= frames.size()) cur_frame = 1; else cur_frame++; } } public void paint(Graphics g) { super.paint(g); Image img = (Image)frames.elementAt(cur_frame-1); g.drawImage(img, cur_frame*30, 0, this); }

14 Steps of a Simple Animation Animation using Thread In “init()”, load the images into frames (data structure of a vector). In “start()”, start a thread to do the animation. In “run()” of the thread, paint the cur-frame, pause a while, and move forward the pointer of frames. In “paint()”, paint the image of the cur-frame. Animation using Timer class Create a Timer object: new Timer(delay, new TimerListener()); The timer generates an action event in interval of delay and TimerListener() is an actionListener class that handles this action event. Methods for Timer control: timer.setDelay(delay); timer.start(); timer.stop();

15 Example of Animation using Timer public class bounceBall extends JApplet { int delay = 100, x = 0, y = 20, radius = 10, dx = 10, dy = 10; public Timer timer = new Timer(delay, new TimerListener()); public void init() { panel.add(btSuspend = new JButton("Suspend")); btSuspend.addActionListener(new ButtonAdapter()); panel.add(btResume = new JButton("Resume")); btResume.addActionListener(new ButtonAdapter()); JScrollBar scrollBar = new JScrollBar(); scrollBar.setOrientation(JScrollBar.HORIZONTAL); scrollBar.addAdjustmentListener(new ScrollAdapter()); add(scrollBar, BorderLayout.NORTH); add(displayArea, BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); timer.start(); } public void start() { timer.start(); } public void stop() { timer.stop(); } public void paint(Graphics g) { ………… x += dx; y += dy; g.setColor(Color.red); g.fillOval(x-radius, y-radius, radius*2, radius*2); } class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { repaint(); } class ButtonAdapter implements ActionListener { public void actionPerformed (ActionEvent e) { if(e.getSource() == btSuspend) timer.stop(); if(e.getSource() == btResume) timer.start(); } class ScrollAdapter implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { timer.setDelay(scrollBar.getMaximum() - e.getValue()); }

16 Media Tracker MediaTracker class provides methods to check whether the loading of an image (a group of images) is complete or not, and get status of image loading. It is particularly useful in managing a group of images. Construct a MediaTracker object by: MediaTracker myTracker = new MediaTracker(this); Useful methods of MediaTracker class: addImage(img, id), add the img into the Tracker isErrorAny(), if loading process has an error checkID (id), check if the image is loaded checkAll (), if all images are loaded waitForID (id), waitForAll (), block the current executing thread until all images are loaded.

17 Example of using MediaTracker public class ImageApplet extends JApplet { Image myImg = null; MediaTracker myTracker = null; public void init() { myTracker = new MediaTracker(this); myImg = getImage(getDocumentBase(), “me.gif”); myTracker.addImage(myImg, 0); } public void paint(Graphics g) { if (myTracker.isErrorAny()) { g.drawString(“errors in loading”, 10, 10); return; } else if (myTracker.checkAll(true)) { g.drawImage(myImg, 0, 0, this); } else { g.drawString(“image loading…”, 10, 10); repaint(100) // recursive call paint every 100ms }

18 Applet Network Access Security of an applet: applets cannot run any local executable program. applets cannot read or write to the local computer’s file system. applets cannot communicate with any host other than the server from which they are downloaded. Getting data from originating server applets get data from their originating servers: URL getDocumentBase() // URL of this web page URL getCodeBase() // URL of this of this applet Examples cat = getImage (getDocumentBase(), “images/cat.gif”);

19 Message Passing between Applets When a web page contains several applets, it is often required that the applets communicate with each other. An applet can send messages to another applet on the same page (communicating applets must originate from the same server). An applet sends a message to another via invoking a method of the other applet. It is “inter-applet method invocation”. Applets identify each other by using their names (assigned in the HTML file). The message passing is done inside the browser (at the client side) without the server involved. Applet 1Applet 2 …… recvMsg(m) { …… } …… Applet2.recvMsg(m) ……

20 HTML File Containing two Applets with Names

21 Steps for sending a message to another applet 1. Sender gets receiver’s name by recverName = getParameter("RECEIVER") 2. Sender gets the receiver applet by recver = getAppletContext().getApplet(recverName) 3. Sender invokes the receiver’s method by reply_msg = ((receiver) recver).recvMsg(msg) // “recvMsg” is a method of the receiver (for receiving messages). 4. The receiver’s method can return a value (or string) that the sender can get as the reply of its message.

22 The sender program // sender.java public class sender extends Applet implements ActionListener { public void init() { recvName = getParameter("RECEIVER"); //... set the GUI } public void actionPerformed(ActionEvent event) { String reply_msg, msg = “test of applet message passing"; Applet recver = null; //Get the receiver applet recver = getAppletContext().getApplet(recverName); // call receiver applet’s method. reply_msg = ((receiver)recver).recvMsg(myName, msg); status.append(“Replied msg: " + reply_msg +"\n"); msg = reply_msg;} }

23 The receiver program // receiver.java public class receiver extends JApplet implements ActionListener { private JButton button = new JButton("Clear"); private JTextArea status = new JTextArea(5, 60); public void init() { button.addActionListener(this); add("North", button); add("Center", status); add("South", new JLabel("My name is "+ getParameter("NAME"))); } public String recvMsg(String senderName, String msg) { status.append("Received from " + senderName +": " + msg +"\n"); i++; return msg+i;// echo received message back } public void actionPerformed(ActionEvent event) { status.setText(""); i = 0; }