6.3.7.6.2 Create a User-Defined Method to Select and Display Desired Faculty Image
Open the Code Window of the FacultyFrame class by clicking on the Source tab from the top of the window and enter the code shown in Figure 6.36 to create the new method, ShowFaculty(). Let’s have a closer look at this piece of code to see how it works.
A. Some local variables and objects used in this method are declared and defined first. imgId and timeout are used as the ID of the tracked image and the maximum waiting time for that tracking process. A local Image object, img, which is used to temporarily hold the selected faculty image, is created and initialized here. Since we need to retrieve our faculty image and store it in output stream format, a FileOutputStream object, imgOut-putStream, is declared here and initialized to null. A new instance of the MediaTracker class, tracker, is created since we need to use it in the ShowFaculty() method.
B. In order to store our retrieved faculty image in our current project folder, the system method getProperty(), with our current directory (user.dir), is used, and this current folder is assigned to a local string variable, imgPath.
C. To get the selected faculty image, we need to get the current selected or queried faculty name from the Faculty Name combo box, convert this item to a string and attach a “.jpg” as the image file name. We need to use this faculty image name later to store and display the selected faculty image in the Canvas.
D. A try-catch block is used to activate and initialize a new instance for the FileOutputStream class imgOutputStream by attaching our current project

FIGURE 6.36 Detailed code for the user-defined method ShowFaculty().
folder to the name of the selected faculty image, since we need to store this image file in that folder and later retrieve it to display it on our Canvas in the FacultyFrame Form. The forward slash, “/”, is necessary to separate the current folder from the image name.
E. Any possible error, including the file not found exception, is collected by this catch block.
F. If the imgOutputStream instance is initialized successfully, our retrieved faculty image, bimg, which is a Blob and passed argument to the ShowFaculty() method, is written or stored into our current folder with the selected faculty image name, fimgName. This writing operation is performed by executing a method, getBytes(), with the length of the Blob, which means that the entire Blob is retrieved and written byte by byte into our current folder with the image name.
G. TheimgOutputStream should be closed when the write operation is completed.
H. To display the selected faculty image, the getImage() method that belongs to the abstract class Toolkit is executed to load the selected image. Since the Toolkit class is an abstract class, we use the getToolkit() method to create it instead of generating it by invoking its constructor. The getGraphics() method is called to get a Graphics context, and our ImageCanvas works as an image holder for this faculty image.
I. The addImage() method that belongs to the MediaTracker class is called to add our image with its ID into the tracking system.
J. A try catch block is used to begin this tracking process, and the waitForID() method is called to execute the tracking. If a timeout occurs for this tracking process, which means that the selected faculty image has not been loaded into the project, a warn-ing message is displayed using our MsgDialog object.
K. Any possible exception or error will be caught by the catch block and displayed in our msgDlg dialog.
L. If no timeout error happens, which means that the selected faculty image has been loaded into our project and is ready to be displayed, the drawImage() method is executed to display it in the FacultyFrame Form window. We want to display this image starting from the origin of the Canvas object, which is the upper-left corner of the can-vas (0, 0), with a width and height that are identical to those of the canvas. Therefore, the getWidth() and getHeight() methods are called to get both of these from the canvas object. A true is returned to the main program to indicate that the execution of this method is successful.
Now we have finished the coding process for this method. Next let’s finish the coding process to call the user-defined method to select and display the selected faculty image by adding the addi-tional code into the Select button event handler.