Goals
Write a Java program that will draw a skyline of houses.
Skills
- 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.
- Building Class
- Write a class that that extends JPanel and will draw a single building.
- You can draw the house from your older program if you wish.
- Your class should have 3 variables that control the color, width and height of the building.
- These variables should be passed into the class using a constructor method.
- See 4.6, p. 184-185, for the coding syntax.
- Your building needs at least 4 parts (4 shapes).
- You need at least 3 different colors choices for the building.
- Colors can be passed in as just integers and then converted to a color using if-else statements.
- Colors can also be passed in as Colors using RGB values if you can figure out that code.
- Main Program
- Building panels
- Create 5 different buildings using the class above.
- You will have 5 different variables which are actually Jpanels with buildings drawn in them.
- Each building will have a different size and color.
- Use random numbers to determine the color and size of the buildings.
- Limit the range of the random numbers so your buildings are not too big or too small.
- Building should be drawn as though they are on the ground (building shouldn’t be floating in the air).
- Main panel
- Create another blank main panel. Jpanel mainPanel = new Jpanel();
- Add your 5 building to this main panel. mainPanel.add(building1);
- Your frame below can only have one panel added to it, but a panel can have multiple panels added to it.
- Frame
- Create a fram folloiwng the code on p. 183.
- Add your mainPanel to the frame to show all 5 buildings.
- Building panels
Hints:
- Break the program down into the steps below. Don’t try to do the program all at once.
- Draw a building of fixed size and color. Put the drawing of your building in a class apart from your main program.
- Draw a house of varying size and color. Modify your building class to make it variable is size and color.
- Draw a skyline of houses. Modify your main program to draw multiple houses.
Resources
No tutorial videos for this, yet.