The classes under NSObject in the Classes display of the nib file window represent, in most cases, both Java and Objective-C versions of the same class. When you create source code files from your nib-file definitions, you must specify which language you want.
The constructor for TempImageView loads image files from the application's resources, converts them to NSImage objects, and assigns these to instance variables. (You'll add these images to the project later in this tutorial.) It also sets certain inherited attributes of the image view. The following procedure approaches the implementation of this constructor in three steps.
/* TempImageView */ import com.apple.yellow.application.*; import com.apple.yellow.foundation.*; public class TempImageView extends NSImageView { protected NSImage coldImage; // add this protected NSImage moderateImage; // add this protected NSImage hotImage; // add this
public TempImageView(NSRect frame) { // load images super(frame); coldImage = new NSImage("Cold.tiff", true); if (coldImage == null) { System.err.println("Image Cold.tiff not found."); } moderateImage = new NSImage("Moderate.tiff", true); if (moderateImage == null) { System.err.println("Image Moderate.tiff not found."); } String h = NSBundle.mainBundle().pathForResource("Hot", "tiff"); if (h != null) { hotImage = new NSImage(h, true); } else { System.err.println("Image Hot.tiff not found."); }
There are a few things to observe about the above excerpt of code:
NSImageView(NSRect)
method, so the first thing done is a call to super's constructor.
NSImage(String, boolean)
to locate the specified image resource in the application bundle and create an NSImage with it.
pathForResource(String, String)
method is called to locate the image and return a path to it within the application bundle. This path is used in the
NSImage(String, boolean)
constructor.setEditable(false); setImage(moderateImage); setImageAlignment(NSImageCell.ImageAlignCenter); setImageFrameStyle(NSImageCell.ImageFrameNone); setImageScaling(NSImageCell.ScaleProportionally); }
The foregoing procedure might lead you to wonder how you can learn more about the methods of a Yellow Box class, especially their arguments and return types. The current versions of Mac OS X and Yellow Box for Windows provide a tool to help you, JavaBrowser. They also include a version of the Java Yellow Box API reference documentation which documents many classes; for those that are not documented, a "skeletal" class specification displays the prototype for each method and includes an HTML link to the method's Objective-C counterpart in the reference documentation.
TempImageView has one public method that TempController calls whenever the user enters a new temperature value: the
tempDidChange
method. Implement this method as shown here:
public void tempDidChange(int degree) { if (degree < 45) { setImage(coldImage); } else if (degree > 75) { setImage(hotImage); } else setImage(moderateImage); }
These ranges are completely arbitrary, but could be influenced by a California climate. If you have lower or higher thresholds for hot and cold temperatures, you can specify your own ranges.
In the convert method of TempController, call TempImageView's
tempDidChange
method after converting the entered value. Copy the following example:
public void convert(NSTextField sender) { if (sender == celsius) { int f = (int)((9.0/5.0 * celsius.intValue()) + 32); fahrenheit.setIntValue(f); } else if (sender == fahrenheit) { int c = (int)((fahrenheit.intValue()-32) * 5.0/9.0); celsius.setIntValue(c); } tempImage.tempDidChange(fahrenheit.intValue()); // add this }
You must also add the tempImage outlet you defined earlier in Interface Builder as an instance variable in TempController.java .