The Design and Implementation of Multimedia Software



Similar documents
Advanced Network Programming Lab using Java. Angelos Stavrou

Homework/Program #5 Solutions

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

An Overview of Java. overview-1

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Essentials of the Java(TM) Programming Language, Part 1

How To Write A Program For The Web In Java (Java)

Programming with Java GUI components

Essentials of the Java Programming Language

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

CS 335 Lecture 06 Java Programming GUI and Swing

The Basic Java Applet and JApplet

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Extending Desktop Applications to the Web

11. Applets, normal window applications, packaging and sharing your work

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

CS506 Web Design and Development Solved Online Quiz No. 01

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Using A Frame for Output

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Introduction to the Java Programming Language

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Building a Multi-Threaded Web Server

Java Appletek II. Applet GUI

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Fundamentals of Java Programming

Graphical User Interfaces

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Dev Articles 05/25/07 11:07:33

Java is commonly used for deploying applications across a network. Compiled Java code

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

CSC 551: Web Programming. Spring 2004

Tutorial Reference Manual. Java WireFusion 4.1

Chapter 2 Introduction to Java programming

Java SE 6 Update 10. la piattaforma Java per le RIA. Corrado De Bari. Sun Microsystems Italia Spa. Software & Java Ambassador

Swing. A Quick Tutorial on Programming Swing Applications

How to Convert an Application into an Applet.

Java applets. SwIG Jing He

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

The Design and Implementation of Multimedia Software

Assignment # 2: Design Patterns and GUIs

Crash Course in Java

How Scala Improved Our Java

Chapter 1 Java Program Design and Development

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

Summer Internship 2013

public class demo1swing extends JFrame implements ActionListener{

Syllabus for CS 134 Java Programming

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

JIDE Action Framework Developer Guide

public static void main(string[] args) { System.out.println("hello, world"); } }

Introduction to Java Applets (Deitel chapter 3)

Lösningsförslag till tentamen

WRITING DATA TO A BINARY FILE

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Creating a Simple, Multithreaded Chat System with Java

Tema: Encriptación por Transposición

public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

Network/Socket Programming in Java. Rajkumar Buyya

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Illustration 1: An applet showing constructed responses in an intuitive mode. Note the misprint!

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Contents. 9-1 Copyright (c) N. Afshartous

DC60 JAVA AND WEB PROGRAMMING JUNE b. Explain the meaning of the following statement public static void main (string args [ ] )

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Java from a C perspective. Plan

JiST Graphical User Interface Event Viewer. Mark Fong

Java Network. Slides prepared by : Farzana Rahman

Building a Java chat server

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Analysis Of Source Lines Of Code(SLOC) Metric

CS108, Stanford Handout #33. Sockets

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

JAVA Program For Processing SMS Messages

Construction of classes with classes

Java History. Java History (cont'd)

Java CPD (I) Frans Coenen Department of Computer Science

Lecture J - Exceptions

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

ICOM 4015: Advanced Programming

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Java Interview Questions and Answers

Overview of Web Services API

WEEK 2 DAY 14. Writing Java Applets and Java Web Start Applications

There are some important differences between an applet and a standalone Java application, including the following:

Transcription:

Chapter 3 Programs The Design and Implementation of Multimedia Software David Bernstein Jones and Bartlett Publishers www.jbpub.com David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 1 / 63

Introduction About this Chapter Thus far the the phrase software product has been used instead of the word program. This chapter develops a (somewhat) formal definition of the word program. This chapter also discusses a way to unify different types of Java programs (which is especially important in the context of multimedia software products). David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 2 / 63

Java Programs A Definition Definition A program in an object-oriented programming language is a group of cooperating classes with a well-defined entry point (i.e., a method that should be executed first) and, perhaps, a re-entry point and/or an exit point. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 3 / 63

Java Programs Java Programs with GUIs Environment Top-Level Container Entry Point Application Operating System JFrame main() Applet Browser JApplet init() then start() David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 4 / 63

Java Programs Application Lifecycle When an application is started the main() method is executed in a non-daemon thread called the main thread. A single-threaded application terminates when the System.exit() method is called, in response to a platform-specific event such as a SIGINT or a Ctrl-C, or when the main thread drops out of the main() method. A multi-threaded application terminates when the System.exit() method is called, in response to a platform specific event, or when all non-daemon threads have died. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 5 / 63

Java Programs Applet Lifeycyle When an HTML page containing an <applet> element is loaded for the first time, the appropriate object (i.e., the descendent of the Applet class referred to in the <applet> element) is constructed, its init() and start() methodsarecalledina thread other than the event dispatch thread. Each time the user leaves the page containing the applet, the stop() method is called (again, not in the event dispatch thread). Similarly, each time the user re-loads the page containing the applet, the start() method is called. When the browser is shut down, the destroy() method is called (again, not in the event dispatch thread). David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 6 / 63

Java Programs Applications An Application Revisited import java.awt.event.*; import java.util.*; import javax.swing.*; public class BadInteractiveRandomMessageSwingApplication implements ActionListener, Runnable // Attributes private JLabel label; // The pseudo-random number generator private static Random rng = new Random(); // String "constants" private static final String CHANGE = "Change"; // The messages private static final String[] MESSAGES = "What a great book.","bring on the exercises.", "Author, author!","i hope it never ends."; public static void main(string[] args) throws Exception SwingUtilities.invokeAndWait( new BadInteractiveRandomMessageSwingApplication()); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 7 / 63

Java Programs Applications An Application Revisited (cont.) public void actionperformed(actionevent event) String actioncommand; actioncommand = event.getactioncommand(); if (actioncommand.equals(change)) label.settext(createrandommessage()); private static String createrandommessage() return MESSAGES[rng.nextInt(MESSAGES.length)]; public void run() JButton button; JFrame window; JPanel contentpane; String s; // Select a message at random s = createrandommessage(); // Construct the "window" window = new JFrame(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 8 / 63

Java Programs Applications An Application Revisited (cont.) window.setsize(600,400); window.setdefaultcloseoperation(jframe.dispose_on_close); // Get the container for all content contentpane = (JPanel)window.getContentPane(); contentpane.setlayout(null); // Add the message component to the container label = new JLabel(s, SwingConstants.CENTER); label.setbounds(50,50,500,100); contentpane.add(label); // Add the button to the container button = new JButton(CHANGE); button.setbounds(450,300,100,50); contentpane.add(button); button.addactionlistener(this); // Make the "window" visible window.setvisible(true); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 9 / 63

Java Programs Applets An Applet with the Same Functionality import java.awt.event.*; import java.util.*; import javax.swing.*; public class BadInteractiveRandomMessageJApplet extends JApplet implements ActionListener // Attributes private JLabel label; // The pseudo-random number generator private static Random rng = new Random(); // String "constants" private static final String CHANGE = "Change"; // The messages private static final String[] MESSAGES = "What a great book.","bring on the exercises.", "Author, author!","i hope it never ends."; public BadInteractiveRandomMessageJApplet() super(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 10 / 63

Java Programs Applets An Applet with the Same Functionality (cont.) public void actionperformed(actionevent event) String actioncommand; actioncommand = event.getactioncommand(); if (actioncommand.equals(change)) label.settext(createrandommessage()); private static String createrandommessage() return MESSAGES[rng.nextInt(MESSAGES.length)]; public void init() JButton button; JPanel contentpane; String s; // Select a message at random s = createrandommessage(); // Get the container for all content contentpane = (JPanel)getContentPane(); contentpane.setlayout(null); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 11 / 63

Java Programs Applets An Applet with the Same Functionality (cont.) // Add a component to the container label = new JLabel(s, SwingConstants.CENTER); label.setbounds(50,50,500,100); contentpane.add(label); // Add the button to the container button = new JButton(CHANGE); button.setbounds(450,300,100,50); button.addactionlistener(this); contentpane.add(button); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 12 / 63

An Observation The differences between applications and applets are problematic for multimedia programmers, who must frequently create applications and applets that provide the same functionality. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 13 / 63

Dealing with this Problem Develop distinct applications and applets that share classes (which is not difficult since the general structures of applets and applications are very similar). What are the shortcomings? Create a unified system that, to the extent possible, makes it possible to used the same glue code (i.e., code that connects the various cooperating classes) in applets and applications. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 14 / 63

Dealing with this Problem Develop distinct applications and applets that share classes (which is not difficult since the general structures of applets and applications are very similar). Code Duplication Create a unified system that, to the extent possible, makes it possible to used the same glue code (i.e., code that connects the various cooperating classes) in applets and applications. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 14 / 63

Requirements F3.1 Applets and applications must have a common programming interface. F3.2 Applets and applications must have a common lifecycle. F3.3 Applets and applications must have a common way to obtain start-up parameters. N3.4 Transition methods in both applets and applications must be called in the event dispatch thread. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 15 / 63

Alternative 1 David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 16 / 63

Alternative 1 - The run() Method public final void run() constructmainwindow(); init(); mainwindow.setvisible(true); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 17 / 63

Alternative 1 - The constructmainwindow() Method mainwindow = new JFrame(); mainwindow.settitle("multimedia Software - jblearning. mainwindow.setresizable(false); contentpane = (JPanel)mainWindow.getContentPane(); contentpane.setlayout(null); contentpane.setdoublebuffered(false); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 18 / 63

Alternative 1 - The init() Method public abstract void init(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 19 / 63

Alternative 1 - An Example import java.util.*; import javax.swing.*; import app.japplication; public class BadRandomMessageJApplication extends JApplication // Attributes private JLabel label; // The pseudo-random number generator private static Random rng = new Random(); // The messages private static final String[] MESSAGES = "What a great book.","bring on the exercises.", "Author, author!","i hope it never ends."; public static void main(string[] args) throws Exception SwingUtilities.invokeAndWait( new BadRandomMessageJApplication(600,400)); public BadRandomMessageJApplication(int width, int height) David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 20 / 63

Alternative 1 - An Example (cont.) super(width, height); private static String createrandommessage() return MESSAGES[rng.nextInt(MESSAGES.length)]; public void init() JPanel contentpane; String s; // Select a message at random s = createrandommessage(); // Get the container for all content contentpane = (JPanel)getContentPane(); contentpane.setlayout(null); // Add a component to the container label = new JLabel(s,SwingConstants.CENTER); label.setbounds(50,50,500,100); contentpane.add(label); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 21 / 63

Alternative 1 - Shortcomings Requirement 3.3 is not satisfied because the RootPaneContainer for an applet (which is the JApplet itself) has access to the start-up parameters whereas the RootPaneContainer for an application does not. Requirement 3.4 is also not satisfied because the transition methods (i.e., the init(), start(), stop() and destroy() methods) in a JApplet are not called in the event dispatch thread. It still encourages code duplication. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 22 / 63

Alternative 1 - Towards Solving the First Shortcoming package app; import javax.swing.*; public interface MultimediaRootPaneContainer extends RootPaneContainer public abstract String getparameter(string name); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 23 / 63

Alternative 2 Use the Decorator Pattern David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 24 / 63

Alternative 2 - MultimediaApp package app; import javax.swing.*; public interface MultimediaApp public abstract void destroy(); public abstract void init(); public abstract void setmultimediarootpanecontainer( MultimediaRootPaneContainer container); public abstract void start(); public abstract void stop(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 25 / 63

Alternative 2 - AbstractMultimediaApp package app; import javax.swing.*; public abstract class AbstractMultimediaApp implements MultimediaApp protected MultimediaRootPaneContainer rootpanecontainer; public void destroy() public void init() public void setmultimediarootpanecontainer( MultimediaRootPaneContainer container) rootpanecontainer = container; public void start() David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 26 / 63

Alternative 2 - AbstractMultimediaApp (cont.) public void stop() David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 27 / 63

Alternative 2 - MultimediaApplet package app; Structure import java.awt.*; import javax.swing.*; public abstract class MultimediaApplet extends JApplet implements MultimediaRootPaneContainer private MultimediaApp app; public MultimediaApplet(MultimediaApp app) super(); this.app = app; setlayout(null); app.setmultimediarootpanecontainer(this); protected MultimediaApp getmultimediaapp() return app; David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 28 / 63

Alternative 2 - MultimediaApplet (cont.) Satisfying Requirement 3.4 public void destroy() if (SwingUtilities.isEventDispatchThread()) app.destroy(); else try SwingUtilities.invokeAndWait(new DestroyRunnable()); catch (Exception e) public void init() if (SwingUtilities.isEventDispatchThread()) app.init(); else try SwingUtilities.invokeAndWait(new InitRunnable()); catch (Exception e) e.printstacktrace(); public void start() if (SwingUtilities.isEventDispatchThread()) app.start(); else try SwingUtilities.invokeAndWait(new StartRunnable()); catch (Exception e) e.printstacktrace(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 29 / 63

Alternative 2 - MultimediaApplet (cont.) Satisfying Requirement 3.4 (cont.) private class DestroyRunnable implements Runnable public void run() app.destroy(); private class InitRunnable implements Runnable public void run() app.init(); private class StartRunnable implements Runnable public void run() app.start(); private class StopRunnable implements Runnable public void run() David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 30 / 63

Alternative 2 - An Example import java.util.*; import javax.swing.*; import app.*; public class RandomMessageApp extends AbstractMultimediaApp // Attributes private JLabel label; // The pseudo-random number generator private static Random rng = new Random(); // The messages private static final String[] MESSAGES = "What a great book.","bring on the exercises.", "Author, author!","i hope it never ends."; private static String createrandommessage() return MESSAGES[rng.nextInt(MESSAGES.length)]; public void init() JPanel contentpane; David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 31 / 63

Alternative 2 - An Example (cont.) String s; // Select a message at random s = createrandommessage(); // Get the container for all content contentpane = (JPanel)rootPaneContainer.getContentPane(); contentpane.setlayout(null); // Add a component to the container label = new JLabel(s, SwingConstants.CENTER); label.setbounds(50,50,500,100); contentpane.add(label); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 32 / 63

Alternative 2 - The Applet import app.*; public class RandomMessageMultimediaApplet extends MultimediaApplet public RandomMessageMultimediaApplet() super(new RandomMessageApp()); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 33 / 63

Alternative 2 - The Applet Demo In examples/chapter: RandomMessage.html David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 34 / 63

Alternative 2 - MultimediaApplication package app; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public abstract class MultimediaApplication extends JApplication implements MultimediaRootPaneContainer private MultimediaApp app; private Properties params; public MultimediaApplication(String[] args, MultimediaApp app, int width, int height) super(width, height); this.app = app; app.setmultimediarootpanecontainer(this); params = new Properties(); for (int i=0; i<args.length; i++) params.put(integer.tostring(i), args[i]); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 35 / 63

Alternative 2 - MultimediaApplication (cont.) public void destroy() app.destroy(); protected MultimediaApp getmultimediaapp() return app; public String getparameter(string name) return params.getproperty(name); public void init() app.init(); public void start() app.start(); public void stop() David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 36 / 63

Alternative 2 - MultimediaApplication (cont.) app.stop(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 37 / 63

Alternative 2 - The Application import app.*; import java.util.*; import javax.swing.*; public class RandomMessageMultimediaApplication extends MultimediaApplication public static void main(string[] args) throws Exception SwingUtilities.invokeAndWait( new RandomMessageMultimediaApplication(args, 600, 400)); public RandomMessageMultimediaApplication(String[] args, int width, int height) super(args, new RandomMessageApp(), width, height); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 38 / 63

Alternative 2 - The Application Demo In examples/chapter: java -cp RandomMessage.jar RandomMessageMultimediaApplication David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 39 / 63

Alternative 2 - One Remaining Issue The Issue: A MultimediaApplet has its transition methods called by the browser when the page containing the JApplet is loaded/unloaded. The transition methods in MultimediaApplication objects should be called at corresponding times. Resolution: Make JApplication a WindowListener on its main window. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 40 / 63

Alternative 2 - One Remaining Issue (cont.) The constructmainwindow() Method mainwindow.setdefaultcloseoperation( JFrame.DO_NOTHING_ON_CLOSE); mainwindow.addwindowlistener(this); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 41 / 63

Alternative 2 - One Remaining Issue (cont.) The windowopened() Method public void windowopened(windowevent event) resize(); start(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 42 / 63

Alternative 2 - One Remaining Issue (cont.) The windowdeiconfied() Method public void windowdeiconified(windowevent event) start(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 43 / 63

Alternative 2 - One Remaining Issue (cont.) The windowiconified() Method public void windowiconified(windowevent event) stop(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 44 / 63

Alternative 2 - One Remaining Issue (cont.) The windowclosing() Method public void windowclosing(windowevent event) exit(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 45 / 63

Alternative 2 - One Remaining Issue (cont.) private void exit() int response; The exit() Method response = JOptionPane.showConfirmDialog(mainWindow, "Exit this application?", "Exit?", JOptionPane.YES_NO_OPTION); if (response == JOptionPane.YES_OPTION) mainwindow.setvisible(false); stop(); mainwindow.dispose(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 46 / 63

Alternative 2 - One Remaining Issue (cont.) The windowclosed() Method public void windowclosed(windowevent event) destroy(); System.exit(0); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 47 / 63

Final Design of the Unified System David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 48 / 63

Program Resources The Issue Most multimedia programs, be they applications or applets, need to load resources of various kinds (e.g, artwork, preferences) at run-time. This can be problematic because of the different ways in which applets and applications can be organized (e.g., in a.jar file, in a packaged set of classes, in an un-packaged set of classes) and delivered/installed (e.g., by an HTTP server, by an installer, as files on a CD/DVD). Hence, it can be very difficult for a program to know where resources are. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 49 / 63

Program Resources How Does the Interpreter Do It? The Java interpreter obtains the byte codes that constitute a class using a class loader. Wecandothesamethingusingreflection. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 50 / 63

Program Resources Reflection Basics Every interface, class and object in Java has an associated Class object that can be used to obtain information about it. This information is encapsulated as Constructor, Field, Method, and Type objects. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 51 / 63

Program Resources Creating a ResourceFinder Use the getresource() and getresourceasstream() methods in Class objects. Allow it to use either its class loader or another object s class loader. David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 52 / 63

Program Resources Structure of the ResourceFinder package io; import java.io.*; import java.net.*; import java.util.*; public class ResourceFinder private Class c; private ResourceFinder() c = this.getclass(); private ResourceFinder(Object o) // Get the Class for the Object that wants the resource c = o.getclass(); public static ResourceFinder createinstance() return new ResourceFinder(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 53 / 63

Program Resources Structure of the ResourceFinder (cont.) public static ResourceFinder createinstance(object o) return new ResourceFinder(o); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 54 / 63

Program Resources The findinputstream() Method public InputStream findinputstream(string name) InputStream is; is = c.getresourceasstream(name); return is; David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 55 / 63

Program Resources The findinputstream() Method public URL findurl(string name) URL url; url = c.getresource(name); return url; David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 56 / 63

A Simple Example Revisited Structure of the StopWatchApp() import java.util.*; import java.awt.event.*; import javax.swing.*; import app.*; import event.*; public class StopWatchApp extends AbstractMultimediaApp implements ActionListener, MetronomeListener private boolean running; private JLabel label; private Metronome metronome; private static final String START = "Start"; private static final String STOP = "Stop"; David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 57 / 63

A Simple Example Revisited Code that is Unchanged public void actionperformed(actionevent event) String actioncommand; actioncommand = event.getactioncommand(); if (actioncommand.equals(start)) label.settext("0"); metronome.reset(); metronome.start(); running = true; else if (actioncommand.equals(stop)) metronome.stop(); running = false; public void handletick(int millis) label.settext(""+millis/1000); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 58 / 63

A Simple Example Revisited Code Moved from run() to init() public void init() JButton JPanel running = false; start, stop; contentpane; contentpane = (JPanel)rootPaneContainer.getContentPane(); contentpane.setlayout(null); label = new JLabel("0"); label.setbounds(250,100,100,100); contentpane.add(label); start = new JButton(START); start.setbounds(50,300,100,50); start.addactionlistener(this); contentpane.add(start); stop = new JButton(STOP); stop.setbounds(450,300,100,50); stop.addactionlistener(this); contentpane.add(stop); metronome = new Metronome(1000, true); metronome.addlistener(this); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 59 / 63

A Simple Example Revisited The start() and stop() Methods public void start() if (running) metronome.start(); public void stop() if (running) metronome.stop(); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 60 / 63

A Simple Example Revisited The MultimediaApplication import app.*; import javax.swing.*; public class StopWatchMultimediaApplication extends MultimediaApplication public static void main(string[] args) throws Exception SwingUtilities.invokeAndWait( new StopWatchMultimediaApplication(args, 600,400)); public StopWatchMultimediaApplication(String[] args, int width, int height) super(args, new StopWatchApp(), width, height); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 61 / 63

A Simple Example Revisited The MultimediaApplet import app.*; public class StopWatchMultimediaApplet extends MultimediaApplet public StopWatchMultimediaApplet() super(new StopWatchApp()); David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 62 / 63

A Simple Example Revisited StopWatch Demonstration In examples/chapter: StopWatchMultimedia.html java -cp StopWatchMultimedia.jar StopWatchMultimediaApplication David Bernstein (jbpub.com) Multimedia Software Jones and Bartlett 63 / 63