Sites/Resources

Note:

Lab 6: CityScapes

Goals: To write complete a project that uses multiple classes.

Overview: In this lab you will write a Java applet that will draw a city skyline.  There are several different classes that will be used for this - CityApplet, City, Building, Window, and Point.  Each time the city is drawn it will look different because the height of the buildings and which windows are "on" is random.  Be Creative!  Add some pizazz factors to make your city look even better.  Here is a sample picture with a  sunset:

Photo of a city skyline

What to do: Complete each of the classes below.  Some are complete and only need to be added to if you want to add pizazz to your project, others have multiple methods that need to be completed.

Starting from the top level class, here is CityApplet..java.  It is complete. No need to make changes here.


import java.applet.Applet;
import java.awt.Graphics;

public class CityApplet extends Applet
{
	public void paint(Graphics g)
	{
		City c;
		c = new City();
		c.draw(g);
	}
}

Next comes City.java that draws the city.  It is functional, but you may want to add some pizazz here:


import java.awt.Graphics;
import java.util.Random;

public class City
{
	private int numBuildings;
	private int maxNumStories;
	private int buildingWidth;
	private Random r;

	public City()
	{
		numBuildings = 15;
		maxNumStories = 40;
		buildingWidth = 5;
		r = new Random();
	}

	public City(int numBuildings, int maxNumStories, int buildingWidth)
	{
		this.numBuildings = numBuildings;
		this.maxNumStories = maxNumStories;
		this.buildingWidth = buildingWidth;
		r = new Random();
	}

	public void draw(Graphics g)
	{
		for(int i = 0; i<numBuildings; i++)
		{
			int numStories;
			numStories = r.nextInt(maxNumStories/2) + maxNumStories/2;
			Building b;
			b = new Building(500, 
                                 50+(Building.ONE_STORY*buildingWidth+5)*i,
                                         numStories,
                                         buildingWidth); b.draw(g); } } }

As you saw above, the City creates buildings. Here is Building.java.


import java.awt.Color;
import java.awt.Graphics;
public class Building
{
    //buildings are so many stories tall and wide.  Here we are setting
    //one story to be 10 pixels.
    public static final int ONE_STORY = 10;

    private int base;  //the y location of the base of the building
    private int x;  //the x location of the building
    private int heightInStories;
    private int widthInStories;     
    private Color color;  //the color of the building
    
    //The defaul constructor sets the base to 400, x to 100, makes the
    //building 30 stories high and 5 stories wide, and sets the building's
    //color to black.
    public Building()
    {
       //your code here
    }
    
    public Building (int base, int x, int heightInStories, int widthInStories)
    {
        //your code here
    }

    public void draw(Graphics g)
    {
        int windowWidth = ONE_STORY - 4; //leave 2 pixels on either side of the wondow!
        int windowHeight = ONE_STORY - 4;
        
        //set to the appropriate color for the building and draw the building
       //your code here

        //Loop over every possible window location in the building 
        //and create a window there and draw it. //your code here } }

Now for the Window class!


import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class MyWindow
{
    private Rectangle window;  //using the Rectangle class!
    private Color color;  //The color of the window
    private double probabilityOfLightsOn;
    private Random r;

    //The default window would be a rectangle located at 10,10 with a 
    //width of 10 and a height of 10.
    //The default color should be yellow and the window would have a
    //25% chance (0.25) of being on.
    public MyWindow()
    {
        //your code here
    }

    public MyWindow (int x, int y, int width, int height)
    {
        //your code here
    }

    public void draw(Graphics g)
    {
        //if it is this windows lucky time to be left on (throw a
        //random number to find out) then set g to the appropriate color
        //and draw the Window
        //your code here
    }

}

The window class uses the Rectangle class, which uses the MyPoint class.  We discussed the classes earlier and they are attached below.


What to submit: Show me your program when completed.

© Daubenmier 2011
Č
ċ
ď
MyPoint.java
(2k)
Mark Daubenmier,
Nov 16, 2011, 2:51 AM
ċ
ď
Rectangle.java
(1k)
Mark Daubenmier,
Nov 16, 2011, 2:51 AM
Comments