Java Reference
In-Depth Information
Example output from the client program after it has successfully received an
image from the server is shown in Fig. 11.1 .
Fig. 11.1 Displaying a
received image on a JFrame
An alternative to displaying the image directly onto the application frame is to
make use of an overloaded form of the JLabel constructor that takes an Icon 'object'
as its single argument. Icon is an interface that is implemented by class ImageIcon ,
making an ImageIcon object also an Icon 'object'. Thus, the only changes that need
to be made to the client code above are as follows:
￿
declare the JLabel object;
￿
use new to create the above JLabel , supplying the ImageIcon as the argument to
the constructor;
￿
add the JLabel to the application frame;
￿
remove the call to repaint ;
￿
remove the re-defi nition of paint .
Example (of lines that need to be added)
JLabel imageLabel; //Amongst initial declarations.
.........................................
imageLabel = new JLabel(image);
add(imageLabel,BorderLayout.CENTER);
The resultant output will be virtually identical to that shown in Fig. 11.1 .
11.2
Transferring Media Files
Unfortunately, there is no Serializable sound class corresponding to the ImageIcon
class for images, so we need a different transfer method for sound fi les. One viable
method involves transferring the fi le as an array of bytes (which is Serializable ,
since it is a stream of primitive-type elements). This method can also be applied to
graphics fi les, which means that we can use the same method to transfer fi les that
may be of mixed types. In some applications, this is likely to be very useful.
Adopting a client-server approach again, the steps required at each end of the trans-
mission will be considered in turn…
Search WWH ::




Custom Search