Java Reference
In-Depth Information
ioEx.printStackTrace();
}
}while (true);
}
}
Before looking at the client code, it is appropriate to give consideration to how
the image might be displayed when it has been received. The simplest way of doing
this is to create a GUI (using a class that extends JFrame ) and defi ne method
paint to specify the placement of the image upon the application. As was seen in the
'juggler' animation bean in Sect. 10.2 (though that application used a JPanel , rather
than a JFrame ), this will entail invoking the ImageIcon method paintIcon . The four
arguments required by this method were stated in Sect. 10.2 and are repeated here:
￿ a reference to the component upon which the image will be displayed (usually
this , for the application container);
￿ a reference to the Graphics object used to render this image (provided by the
argument to paint );
￿
the x-coordinate of the upper-left corner of the image's display position;
￿
the y-coordinate of the upper-left corner of the image's display position.
Remember that we cannot call paint directly, so we must call repaint instead
(and allow this latter method to call paint automatically). This call will be made at
the end of the constructor for the client. Steps 3-5 from the original fi ve steps are
commented in bold type in the client program.
Now for the code…
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageClient extends JFrame
{
private InetAddress host;
private fi nal int PORT = 1234;
private ImageIcon image;
public static void main(String[] args)
{
ImageClient pictureFrame = new ImageClient();
//Ideally, size of image should be
//known in advance…
pictureFrame.setSize(340,315);
pictureFrame.setVisible(true);
pictureFrame.setDefaultCloseOperation(
EXIT_ON_CLOSE);
Search WWH ::




Custom Search