Creating the Desktop Version


The Forecaster project can also be written as a standalone desktop Java application, Forecast.java. To display the weather graph, as you see in Figure 10.2, you can create the Forecast class by extending the Frame class:

 import java.io.*;  import java.awt.*; import java.net.*; import java.awt.image.*; import java.awt.event.*; public class Forecast extends Frame         .         .         . 

This application will also need a way of asking the user his ZIP Code, so it'll use the OKCancelDialog class introduced in Chapter 2, "Slapshot! The Interactive Hockey Game":

 import java.io.*; import java.awt.*; import java.net.*; import java.awt.image.*; import java.awt.event.*; public class Forecast extends Frame {     OkCancelDialog textDialog;         .         .         . 

The main method will call the Forecast constructor, where the real action takes place:

 import java.io.*; import java.awt.*; import java.net.*; import java.awt.image.*; import java.awt.event.*; public class Forecast extends Frame {     OkCancelDialog textDialog;     public static void main(String[] args)     {         new Forecast();     }     public Forecast()     {         .         .         .     } } 

In the Forecast constructor, the first order of business is to get the user's ZIP Code. This application stores that information in a file named zip.txt, so it checks to see if zip.txt already exists, and if so, it reads in the user's ZIP Code:

     public Forecast()     {         String zip ="";         File zipFile = new File("zip.txt");         try {             if(zipFile.exists()){                 FileReader filereader = new FileReader("zip.txt");                 BufferedReader bufferedreader = new                     BufferedReader(filereader);                 zip = bufferedreader.readLine();             }         .         .         .     } 

If zip.txt doesn't exist, Forecast.java uses an OKCancelDialog object to ask the user for his ZIP Code and stores it in zip.txt:

     public Forecast()     {         String zip ="";         File zipFile = new File("zip.txt");         try {             if(zipFile.exists()){                 FileReader filereader = new FileReader("zip.txt");                 BufferedReader bufferedreader = new                     BufferedReader(filereader);                 zip = bufferedreader.readLine();             }             else             {                 textDialog = new OkCancelDialog(this,                     "Enter your five-digit zip code", true);                 textDialog.setVisible(true);                 zip = textDialog.data.trim();                 FileOutputStream fileoutputstream = new                     FileOutputStream("zip.txt");                 fileoutputstream.write(zip.getBytes());             }         .         .         . } 

After you have the user's ZIP Code, you can fetch the weather data as before:

 URL url = new URL     ("http://www.srh.noaa.gov/zipcity.php?inputstring="     + zip); URLConnection urlconnection = url.openConnection(); . . . 

After fetching the data and filling the hiTemperature and loTemperature arrays, you can create the forecast graph as in the JSP version of this project:

 image = new BufferedImage(225, 201,     BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics();     g.setColor(Color.white);     g.fillRect(0, 0, 224, 201); . . . 

Finally, you can set up the window with a title and size, and you can make it visible:

 setTitle("The Forecaster"); setResizable(false); setSize(250, 240); setVisible(true);     this.addWindowListener(new WindowAdapter(){         public void windowClosing(             WindowEvent e){                 System.exit(0);             }         }     ); . . . 

Now that the image is ready, you can draw it in the paint method this way:

     public void paint(Graphics g)     {         if(image != null){             g.drawImage(image, 10, 30, this);         }     } } 

And that completes Forecast.java, the desktop version of this application. To run it, just compile it and you're ready to go.

NOTE

Download the complete source code for the Forecaster project, forecast.jsp and Forecast.java, at the Sams website. If you have a JSP-enabled server around, you can use forecast.jsp on it as you'd use any other JSP page. If you want to run the desktop version, just compile Forecast.java and run it as you'd run any other Java program (making sure you have a connection to the Internet available).




    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net