Introduction to Programming with Java Applets (Boese)

Lab: Chapter 9
Loops


The purpose of this assignment is


Instructions:

You are to create an applet that displays the weather based on the time of the year and time of day.

Details of seasons: NOTE: Only one will be displayed at a time. We'll figure out what the current month and hour is and create the appropriate applet.
We can use the Calendar class to determine the time of the year to create a custom applet drawing. The Calendar class allows us to access the current date and time. We'll use it in this

To create our weather program, we're going to create a separate class that extends the JPanel class. Inside this class, we need three instance variables: ints for the month and hour of the day, and a Calendar object to determine the current time. The Calendar class is from the java.util package, so we also need an additional import statement at the top of our program.

        import java.util.*;
To use the Calendar class, we can get the current time by calling getInstance:
     Calendar rightNow;
     rightNow = Calendar.getInstance( );
To get the current month or hour from our Calendar instance, we call:
        int hour, month;
        hour = rightNow.get( Calendar.HOUR_OF_DAY );
        month = rightNow.get( Calendar.MONTH );
To test our program, we can specifically set the month and hour in the code. Once we're sure all the seasons and day/night settings paint correctly, we can comment out these two lines of code.

The main applet will instantiate our WeatherClip class and add it to the applet. Remember, we have to set the preferred size of the WeatherClip object so it doesn't default to a size of zero.

APPLET PROGRAM


import java.awt.*;
import javax.swing.*;
import java.util.*;
public class WeatherApplet extends JApplet
{
   Calendar rightNow;
   int month, hour;
   Random random;
   public void paint( Graphics g )
   {
      rightNow = Calendar.getInstance( );
      hour = rightNow.get( Calendar.HOUR_OF_DAY );
      month = rightNow.get( Calendar.MONTH );
// month = 1;                  // FOR DEBUGGING
// hour = 12;                // FOR DEBUGGING

       drawSky( g );

	// if summer ( months June, July, August)
	//	color grass
	//	call method to draw the house

	// if Fall ( months Sept, Oct, Nov)
	//	color grass brown
	//	call method to draw the house

	// if winter (months of Jan, Feb or Dec)
	// 	color the ground white
	// 	call method to draw the house
	// 	call method to draw the snow

	// if spring (months March, April, May)
	//	color grass
	// 	call method to draw the house
	// 	call method to draw the rain

   }
   
   public void drawHouse( Graphics g )	   // this code is already done for you
   {
     g.setColor( Color.BLACK );
     g.fillArc( 30, 50, 100, 90, 210, 120 ); // draw roof on house
     g.setColor( Color.PINK );
     g.fillRect( 40, 120, 80, 50 );          // draw the main house
     g.setColor( Color.BLACK );
     g.fillRect( 60, 140, 10, 15 );          // draw a window
     g.fillRect( 80, 140, 20, 30 );          // draw the door
   }
   public void drawSnow( Graphics gr )
   {
    // you add code here
    // create flakes using gr.fillOval to draw each snowflake
	// Use a loop to create 100 snowflakes


   }
   public void drawRain( Graphics gr )
   {
    // you add code here
    // create rain using gr.drawLine to draw each rain drop
	// Use a loop to create 100 rain drops

   }
   public void drawSky( Graphics g )
   {
	// determine if it is daytime or nightime
	// daytime is between 8am and 8pm
	// draw the appropriate sky color
	// 	remember - daytime in winter is gray not CYAN

   }
}


If time is available, add a tree with a few leaves in spring, lots of leaves in summer, multi-colored fall leaves in fall, and no leaves (bare branches) in winter.
When you are finished, show your GTA for credit for this lab exercise.
© by E.S.Boese. All Rights Reserved