Java Coffee Break Newsletter Volume 2, Issue 12 http://www.javacoffeebreak.com/ ISSN 1442-3790 ================================================================= In this issue * Java in the news * Merry Christmas, and a Happy New Year * Book Review - JBuilder 3 Unleashed * Book Review - Developing JavaBeans Using VisualAge for Java * Article : Using MediaTracker to help load images * Q&A : Why did Java skip from Version 1.1 to Version 2? * Q&A : How do I create a drop-down list of items in my GUI? ================================================================= In the News Here are a selection of recent news items that may be of interest to Java developers. /*/ Judge releases "Findings of Fact" in DOJ Microsoft trial The eagerly anticipated findings of fact in the DOJ trial into Microsoft have been publically released. No surprise that Microsoft has been declared a monopoly, but has this really damaged consumers? Will this impact the long term future of Microsoft? Reaction to the findings have been mixed, but the general consensus is that Microsoft isn't down for the count just yet! See an ongoing special report on CNN into the trial, at http://cnnfn.com/specials/antitrust/ /*/ Findings of fact reveal attempts to undermine Java The findings of fact in United States of America v Microsoft reveal attempts by Microsoft to undermine the portability of Java. An except of relevant paragraphs from the findings of fact pertaining to Java is included in our special report, at http://www.javacoffeebreak.com/articles/microsoftjava/ /*/ Java Coffee Break launches Java directory If you're looking for information on any topic related to Java, you'll find it in the new Java Coffee Break Directory. It's packed full of hundreds of great sites, from general tutorials, to the exotic and specialized. We proudly use data supplied by the Open Directory Project, the world's largest volunteer directory effort. Visit the directory at http://www.javacoffeebreak.com/directory ================================================================= Merry Christmas, and a Happy New Year T'is the season to be jolly. Whether you celebrate Christmas, Chanukah,or Kwanzaz, may your holiday season be bright and festive! And if you find yourself leaving things to the last minute like me, you'll probably want to avoid the hustle and bustle of holiday shopping. Why not consider shopping online this season? Amazon.com, one of the world's largest and most respected book and music retailers makes it easy to buy gifts for friends, collegues and loved ones. You'll also be supporting a great Java newsletter! http://www.davidreilly.com/goto.cgi?id=amazon ================================================================= Book Review - JBuilder 3 Unleashed Author : Various Publisher : Sams ISBN : 0672315483 Experience: Beginner - Intermediate Review Courtesy of Amazon.com JBuilder 3 Unleashed does an excellent job of explaining how to write Java programs in Borland's newest Integrated Development Environment (IDE) for the language. With plenty of code (all of which ships on the accompanying CD-ROM) and lots of documentation on Java classes, this book enables the intermediate-level Java programmer to become more capable with JBuilder and the language as a whole. You'll find good information if you want to write servlets, interface with databases, or do any of dozens of other software development tasks. JBuilder 3 Unleashed naturally covers the JBuilder user interface, but the authors don't explain interface elements for their own sake. Rather, they explain the JBuilder interface in the context of doing certain Java programming jobs. Subjects include some fairly exotic aspects of the Java language, including elaborate user interface design, multithreading, internationalization, and client/server architecture. Coverage of programming JavaBeans (and especially serialization as it applies to Beans) with JBuilder is exemplary, as are the sections on Remote Method Invocation (RMI) and CORBA. In addition to their coverage of the mechanics of JBuilder and Java, the authors include quite a bit of management and engineering information on using Borland's development environment as the centerpiece of a development team, including versioning, testing, and object-oriented analysis and design. --David Wall For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=0672315483 ================================================================= Book Review - Developing JavaBeans Using VisualAge for Java v2 Author : Nilsson et al.  Publisher : John Wiley & Sons ISBN : 0471345342 Experience: Beginner-Intermediate Review courtesy of Amazon.com IBM VisualAge for Java is a powerful Java development tool, but it doesn't always get a lot of press. The authors of Developing JavaBeans Using VisualAge for Java, Version 2, meet a real need with their well-organized tutorial using this capable Java tool. This book can also serve as an introduction to Java programming.  There is good coverage of JavaBeans with both visual and non-visual components. Later chapters whet the user's appetite with newer Java features (like Swing/JFC, enterprise beans, and servlets). You'll want to go elsewhere for more thorough coverage of these technologies, but the book lets you try out these features with small working examples that are scaled just right for readers new to VisualAge development. (A working version of the VisualAge tool is included on the accompanying CD-ROM.) The authors show the strengths (and quirks) of VisualAge, with good coverage of its visual programming style (where beans are "wired" together). They even provide a list of "anti-patterns" -- things to watch out for in your VisualAge designs. There aren't many tools-based guides to Java. (Most concentrate on the language itself.) Developing JavaBeans shows that a good way to learn the Java language is with the VisualAge tool. --Richard Dragan For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=0471345342 ================================================================= Using MediaTracker to help load images When loading images over a network, a common problem which Java applets face is images not loading properly. On a fast network connection the image will load quickly, but over a slow modem or network connection, images take longer, or may not arrive at all. Users may be confused by blank images - particularly when a sequence of images are being displayed. One simple way to let your users know you're still loading is to use MediaTracker, which allows applets to detect when images are loaded. By David Reilly. Ask any user who is connected via a modem dial-up connection, what they think about applets, and the answer is frequently that they are slow to load. As one user puts it, "I hate the ugly gray applet box that sits there on the page". Quite often, an applet is loading images for some form of animation - but the user doesn't know what is going on. Other times, applets that fail to load properly just sit there with a blank gray box. This adds to the confusion, because a slow loading applet is often equated with a broken one. When writing applets, we as programmers have a responsibility to users to let them know what's going on. If your applet loads images over a network connection, it's important to track their progress, and notify the user.  That's where MediaTracker comes in. MediaTracker allows applets to check to see whether an image has loaded or not. Applets can register images with a MediaTracker object, and then wait until one or all images have loaded. While the images are loading, a message can be displayed to the user. When all the images are loaded, they can then be processed by the applet - for example, displayed as part of an animation. Note though MediaTracker can benefit applications, as well as applets - even when loading from a local file-system, there can be a noticeable delay when many large images are loaded. MediaTracker is a class in the AWT package. If you're already working with images, the AWT package will be imported. If not, you'll need to add the following lines to the beginning of your code :- // Import the AWT graphics package import java.awt.*; Next, you need to create an instance of java.awt.MediaTracker. You must pass an instance of java.awt.Component to MediaTracker's constructor. An applet is a subclass of Component, so when writing an applet for a web-page, the applet can pass itself by using the this keyword. // Create a MediaTracker instance, // to montior loading of images tracker = new MediaTracker(this); Once you have a MediaTracker, simply load each image and register it, using the MediaTracker.addImage (Image, int) method. It takes as its first parameter an image, and the idcode of the image as its second parameter. The idcode can be used to inquire about the status of a particular image, rather than a group of images. // Load the image Image img = getImage( getDocumentBase(), "picture.jpg" ); // Register it with media tracker tracker.addImage(img, 1); Once you've registered the image, you can display a text message, or a simple animation, to let your users know that data is loading. Of course, you'll also have to check to see if the images are ready for use, by querying MediaTracker. There are two ways to check on the progress of images. The first is to continually poll MediaTracker, by using the MediaTracker.checkID(int) method. // Check to see if it has loaded if (tracker.checkID(1) == false) { // Not yet, display message ..... } else { // Display the animation } Another way, and often the simpler, is to just wait indefinitely, until all images are loaded. Note that it's best to do this in a separate thread of execution, so that your applet can continue to respond to user events and display a message. This is a blocking operation, whereas the MediaTracker.checkID(int) method is non-blocking and returns immediately. // Wait until all images have loaded tracker.waitForAll(); An example applet using MediaTracker The following example demonstrates the use of MediaTracker. No images will be loaded until you click on the applet. Depending on the speed of your connection, it may take a few seconds, or even a minute to load the images. Note that the applet displays a message, indicating a load is occurring, and that the message disappears once the load is finished. Without MediaTracker, there'd be no way to tell if the image was completely loaded - you might end up seeing only part of the image until it was completely finished. (To see the applet, you must be viewing the HTML version of this article at http://www.javacoffeebreak.com/articles/mediatracker/) Listing One - MediaTrackerDemo.java import java.applet.Applet; import java.awt.*; // // // MediaTrackerDemo // // public class MediaTrackerDemo extends Applet implements Runnable { Image[] imgArray = null; MediaTracker tracker = null; int current = 0; Thread animThread=null; // Check for a mouse click, to start the images downloading public boolean mouseDown(Event evt, int x, int y) { if (tracker == null) { // Create a new media tracker, to track loading images tracker = new MediaTracker(this); // Create an array of three images imgArray = new Image[3]; // Start downloading the images for (int index=0; index < 3; index++) { // Load the image imgArray[index] = getImage( getDocumentBase(), "anim" + (index+1) + ".gif"); // Register it with media tracker tracker.addImage(imgArray[index], index); } // Start animation thread animThread = new Thread(this); animThread.start(); } return true; } public void update(Graphics g) { // Don't repaint gray background paint(g); } public void paint (Graphics g) { g.setColor(Color.white); g.fillRect(0,0, 200, 200); g.setColor(Color.black); // Check to see if images have started loading if (tracker == null) { g.drawString ("Click to start loading",20,20); } else // Check to see if images have loaded if (tracker.checkAll()) { g.drawImage(imgArray[current++], 0, 0, this); if (current >= imgArray.length) current=0; } else // Still loading { g.drawString ("Images are loading...", 20,20); } } public void run() { try { tracker.waitForAll(); for (;;) { // Repaint the images repaint(); Thread.sleep(2000); } } catch (InterruptedException ie) {}; } } Running the MediaTrackerDemo The demonstration applet loads three images, called anim1.gif, anim2.gif, anim3.gif, from the base directory of the page from which the applet is loaded. If you'd like to modify the applet, you could increase the number of images, change the name or location, or change the speed of animation. For the full text of this article, including the demonstration applet, read it at the Java Coffee Break http://www.javacoffeebreak.com/articles/mediatracker/ ================================================================= Q&A: Why did Java skip from Version 1.1 to Version 2? The jump in version numbers between Java 1.1 and Java 2 has caused confusion for many programmers, who were expecting Java 1.2. Indeed, many publishers released titles bearing the title of Java 1.2 or JDK1.2, only to find that the version number changed! The official name for the new version of Java is the Java 2 Platform, version 1.2. This corresponds to JDK1.2, and the terms can be used interchangeably. ================================================================= Q&A: How do I create a drop-down list of items in my GUI? There are several types of GUI components that provide a selectable list. The java.awt.List component, for example, provides a scrollable list of items. However, the most efficient use of GUI space is a drop-down list, which is provided by the java.awt.Choice component. Take the following snippet, for example, which provides a list of days of the week. import java.awt.*; public class ChoiceDemo { public static void main(String args[]) { Frame f = new Frame(); f.setLayout(new FlowLayout()); // List contains 7 elements Choice list = new Choice(); list.add ("Monday"); list.add ("Tuesday"); list.add ("Wednesday"); list.add ("Thursday"); list.add ("Friday"); list.add ("Saturday"); list.add ("Sunday"); f.add (list); f.add ( new Button ("Ok") ); f.pack(); f.setVisible(true); } } Choice boxes allow you to get the index value of the list (getSelectedIndex), or the currently selected String (getSelectedItem). ================================================================= The Java Coffee Break Newsletter is only sent out to email subscribers who have requested it, and to readers of the comp.lang.java.programmer and comp.lang.java.help newsgroups. If you'd like to receive our newsletter, and get the latest Java news, tips and articles from our site, then get your FREE subscription & back issues from http://www.javacoffeebreak.com/newsletter/ If you are an email subscriber and no longer wish to receive the JCB Newsletter, please unsubscribe by emailing javacoffeebreak-unsubscribe@listbot.com