1. To practice with
String:
indexOf (str,
start),
indexOf(str),
substring(start),
substring(start, end), and
BufferedReader:
readLine()
JDK
API
2.
To utilize the java.net.URL and java.io classes
URL and
InputStreamReader.
3. To build debugging skills by utilizing
System.out in loops and catch blocks.
4. To practice with adding images to
JLabel.in a JFrame and working with menus.
In this lab you will view the source code for one or more web pages, discern points of interest, open an input stream to the target URLs, have your program search the source code for the item of interest and return a string with which to build a more specific URL, then display the desired target in your JFrame by adding the target image to a JLabel.
Part I
Define a Comics class that extends JFrame and implements ActionListener. Add
a menu with menu items "Dilbert" and "Foxtrot" and a method todaysDilbert. In
the actionPerformed method, if the event is an instance of JMenuItem and
the action command is "Dilbert", then call the method todaysDilbert which returns a string with which to build
the target URL. Create an ImageIcon made from the target URL. Use this
ImageIcon to make a JLabel. The size of the JLabel
should be determined by the image icon's height and width. Remove all the
components from the content pane, then add the newly created JLabel and call repaint().
The following discussion describes a procedure for identifying the target URL in
the method todaysDilbert.
Open the Dilbert web page, http://www.dilbert.com,
and view the source code. Look for a target of interest.
Then determine the required searchStr and offset with which to build a string to
generate a more specific URL. Right click on the image of interest, select
properties, and copy that portion of the
file name that follows www.dilbert.com. Look at the source code (View -> source) and use control-f to search
for the file name in the html source code. In particular look for the html tag
<IMG SRC=...> that contains the relative address of the image. Now that you
have found the image in the html source code, write a program to automate
building a search string based on the directory the comic is stored in. For
example, the properties of the image reveals a typical address on a weekend is
"http://www.dilbert.com/comics/dilbert/archive/images/dilbert2004090116225.jpg"
and on a weekday is
"http://www.dilbert.com/comics/dilbert/archive/images/dilbert2813550040906.gif".
Our goal1 is to break this down into three parts -concatenate these parts to recreate the
complete address (the target URL):
Part II
http://foxtrot.com
Visit the foxtrot homepage, right click on the image and select properties.
Note that the image is stored on a different computer. We will use a more
dirrect approach to build the target URL. The SimpleDateFormat object's format method can be set to format a Date
object in any manner you desire.
Date today = new Date ();
SimpleDateFormat dfmt = new SimpleDateFormat ("yyMMdd");
Notice the construction of the id portion of Foxtrot. In the actionPerformed
method, if the instance of JMenuItem has action command "Foxtrot", build the URL based on the concatenation of "http://images.ucomics.com/comics/ft/2004/ft"
and today's formatted date, and .gif. Use this string to build the target
URL. Create an ImageIcon made from the target URL. Use this
ImageIcon to make a JLabel. The size of the JLabel
should be determined by the image icon's height and width. Remove all the
components from the content pane, then add the newly created JLabel and call repaint().
The following method (optional) will print the image to a file:
public static void download(URL url, File file) throws IOException {
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
}
Email your source code as an attachment to either
CSC221.001@uncw.edu or
CSC221.002@uncw.edu
1. The goal of the search string algorithm is to read the dilbert.com html code one line at a time to find the line that loads the image into the webpage using the IMG tag. This line contains <IMG SRC="... .gif" or .jpg". Once this line is found, the pertinent pieces of the address begin in the search string at the tenth character in this string and the id begins following the search string and ends at the character before the closing double quotes in the IMG tag. The trick is to limit the indexOf method's search to only the pertinent portions of the line (certainly there are multiple occurrences of double quotes in this line.) This is accomplished, for the id, by using the sum of the offset and the search string's length as a starting point for the search.