Allan Didier

Skyline Drawing


Write a Java program that will draw a skyline of houses.


  • Java graphics
  • For loops

Program Details

Use the code on pages 183-185 to draw a building instead of a smiley face. Create a Building (or House) class that will draw a single house. Your main program will then create multiple Buildings.

  1. Building Class
    1. Write a class that that extends JPanel and will draw a single building.
    2. You can draw the house from your older program if you wish.
    3. Your class should have 3 variables that control the color, width and height of the building.
      1. These variables should be passed into the class using a constructor method.
      1. See 4.6, p. 184-185, for the coding syntax.
    4. Your building needs at least 4 parts (4 shapes).
    5. You need at least 3 different colors choices for the building. 
      1. Colors can be passed in as just integers and then converted to a color using if-else statements.
      2. Colors can also be passed in as Colors using RGB values if you can figure out that code.
  2. Main Program
    1. Building panels
      1. Create 5 different buildings using the class above.
      2. You will have 5 different variables which are actually Jpanels with buildings drawn in them.
      3. Each building will have a different size and color.
      4. Use random numbers to determine the color and size of the buildings.
      5. Limit the range of the random numbers so your buildings are not too big or too small. 
      6. Building should be drawn as though they are on the ground (building shouldn’t be floating in the air).
    2. Main panel
      1. Create another blank main panel. Jpanel mainPanel = new Jpanel();
      2. Add your 5 building to this main panel. mainPanel.add(building1);
      3. Your frame below can only have one panel added to it, but a panel can have multiple panels added to it.
    1. Frame
      1. Create a fram folloiwng the code on p. 183.
      2. Add your mainPanel to the frame to show all 5 buildings.


  1. Break the program down into the steps below. Don’t try to do the program all at once.
  2. Draw a building of fixed size and color. Put the drawing of your building in a class apart from your main program.
  3. Draw a house of varying size and color. Modify your building class to make it variable is size and color.
  4. Draw a skyline of houses. Modify your main program to draw multiple houses.


No tutorial videos for this, yet.