ISE 582 Web Technology for Industrial Engineering University
ISE 582: Web Technology for Industrial Engineering University of Southern California DJE Dept of Industrial and Systems Engineering Lecture 10 JAVA Cup 8: Applets 14 November 2002 Web Technology for IE
Handouts & Announcements • Lecture 9 slides • READ Winston & Narasimhan : – Chapters 42 -46 (pp 275 -304) 14 November 2002 Web Technology for IE 2
JAVA Cup 9 • Resource Locators • Making Lists for Selection • Adding Images to Applets 14 November 2002 Web Technology for IE 3
Using Resource Locators • • • Why use Resource Locators? The Class class Using the Class class How Resource Locators work Accessing Image Files 14 November 2002 Web Technology for IE 4
Why Use Resource Locators • They find files even after you have moved your program from one directory to another • They find files properly not only for standalone applications, but also for browser applications 14 November 2002 Web Technology for IE 5
The Class class • When the JVM loads a class / interface, – it creates a companion “Class” instance • The “Class” instance provides – information about the class’s name and instance variables – access to methods for locating resources such as text and image files 14 November 2002 Web Technology for IE 6
Auxiliaries Using the Class class public class Movie. Auxiliaries { public static Vector read. Movie. File(String file. Name) { Vector v = new Vector(); URL url = Movie. Auxiliaries. class. get. Resource(file. Name); Input. Stream stream = (Input. Stream) (url. get. Content()); Input. Stream. Reader reader = new Input. Stream. Reader(stream); . . . } } stream 14 November 2002 reader Web Technology for IE tokens 7
How Resource Locators Work • The resource locator starts looking for the file in the same location as the class • If the filename includes a relative path, the resource locator will look for the file in the subdirectory of the directory holding that class • If the class is part of a browser, this determines the security rules employed • Why insist on the same-place restriction? 14 November 2002 Web Technology for IE 8
Auxiliaries Accessing Image Files import java. net. *; import java. awt. image. *; Blah blah… public static Image read. Movie. Image(String file. Name) { try { URL url = Movie. Auxiliaries. class. get. Resource(file. Name); if (url == null) {return null; } Image. Producer producer = (Image. Producer) (url. get. Content()); if (producer == null) {return null; } Toolkit tk = Toolkit. get. Default. Toolkit(); Image image = tk. create. Image(producer); return image; } catch (IOException e) { System. out. println(e); }; return null; } Blah blah… 14 November 2002 Web Technology for IE 9
Using Choice Lists • • • A new Movie. Data class Modifications to the Model side Modifications to the View side Linking the new model and new view Final changes Using a Scroll. Pane 14 November 2002 Web Technology for IE 10
Movie. Data. Interface import java. util. *; public interface Movie. Data. Interface { // Put movies into data source: public abstract void set. Movie. Vector (Vector v) ; // Extract movies from data source: public abstract Vector get. Movie. Vector () ; // Find a particular movie at given index: public abstract Movie get. Movie(int index) ; // Miscellaneous method: public abstract void changed () ; } 14 November 2002 Web Technology for IE 11
Application Modifications to the Model Blah blah… private Movie. Data movie. Data; Blah blah… public Movie. Data get. Movie. Data () { if (movie. Data == null) { set. Movie. Data(new Movie. Data ()); } return movie. Data; } Blah blah… public void set. Movie. Data (Movie. Data m) { movie. Data = m; movie. Data. add. Observer(new Movie. Data. Observer(this)); movie. Data. changed(); } Blah blah… 14 November 2002 Web Technology for IE 12
Application Modifications to the View Blah blah… private JList j. List; Blah blah… public Movie. Application() { get. Movie(); get. Movie. Data(); get. Content. Pane(). add(“Center”, get. Meter()); get. Content. Pane(). add(“East”, get. JList()); } Blah blah… public JList get. JList () { if (j. List == null) {set. JList(new JList()); } return j. List; } Blah blah… public void set. JList (JList j) { j. List = j; } 14 November 2002 Web Technology for IE 13
Linking Model and View import java. util. *; import javax. swing. *; public class Movie. Data. Observer implements Observer { private Movie. Application applet; public Movie. Data. Observer (Movie. Application a) { applet = a; } public void update (Observable observable, Object object) { Vector titles = new Vector(); for (Iterator i = applet. get. Movie. Data(). get. Movie. Vector(). iterator(); i. has. Next(); ) { Movie movie = (Movie) (i. next()); titles. add(movie. get. Title()); } applet. get. JList(). set. List. Data(titles); } } 14 November 2002 Web Technology for IE 14
Linking Model and View import javax. swing. *; import javax. swing. event. *; public class Movie. Listener implements List. Selection. Listener { private Movie. Application applet; public Movie. Listener (Movie. Application a) { applet = a; } public void value. Changed(List. Selection. Event e) { int index = applet. get. JList(). get. Selected. Index(); applet. set. Movie(applet. get. Movie. Data(). get. Movie(index)); } } 14 November 2002 Web Technology for IE 15
Application Final Changes Blah blah… public void set. Movie. Data (Movie. Data m) { movie. Data = m; movie. Data. add. Observer(new Movie. Data. Observer(this)); movie. Data. changed(); } public void set. JList (JList j) { j. List = j; j. List. add. List. Selection. Listener(new Movie. Listener(this)); } Blah blah… 14 November 2002 Web Technology for IE 16
Using a Scroll Pane • Instead of – get. Content. Pane(). add(“East”, get. JList()); • Use – get. Content. Pane(). add(“East”, new JScroll. Pane(get. JList())); 14 November 2002 Web Technology for IE 17
- Slides: 17