Okay, instead of me trying to explain the assignment, I'm going to just post it here verbatim from my class website. Then I'll post the code I have so far.
Instructions/Design
In this assignment we will refactor our work from the last Skyline program to add randomness and additional classes.
Note: Start by making copies of your Building.java, SkylinePanel.java and Skyline.java files. Always keep a copy of working code before making changes.
Update the Skyline application you wrote in the last programming assignment to include randomness in the size and number of the buildings and windows.Name your program RandomSkyline.java.Because of the randomness we will use loops to control how the skyline looks.Once again, the Splat example from Chapter 4 is useful; as well as the paintComponent() methods from Chpater 5 Bullseye and Boxes examples. Your program needs to make the following changes to the 3 classes from the Skyline application:
1) Change Building constructor to set the number of windows to a random number. The draw method now needs a loop to draw this random number of windows.Remember to keep the windows within the dimensions of the building.
2) SkylinePanel the constructor will no longer instantiate the buildings.Its only job is to set preferred size and background color.
The building instantiation will now be done within the paintComponent() method as part of a loop. Here is the algorithm (this is VERY high-level, you still have plenty of designing to do):
1. generate random number
for each number from one to random number
2. generate random width
generate random height
3. instantiate building
4. draw building
5. generate random gap between next building
6. update distance from left side of frame
First generate a random number.Instantiate that number of buildings. Generate a random width and height. Use these random numbers as parameters to the building's constructor. Also, have the gap between buildings be a random number of pixels. This gap plus width of buildings creates the x coordinate (left edge of skyline) parameter. You can choose how you want to calculate y coordinate parameter (it is OK of buildings are drawn past the bottom of the frame).
3)Rename the class called Skyline to RandomSkyline NO other changes are needed.
4) Additionally, you’ll need to name your city. Use your own name(s).
Extra Credit:Create a fourth class called Star. Fill the skyline with a random number of stars. Put this into a file named Star.java. Note: you will need to draw the stars in the paintComponent() method of SkylinePanel.
I don't understand this at all (except for #s 3 and 4). I don't get how to add windows, and I especially don't get the #2. Will someone please show me how to do this. Also, I can't use arrays yet since we're not up to those in class yet. Here's my code for the original Skyline that was mentioned.
Instructions/Design
In this assignment we will refactor our work from the last Skyline program to add randomness and additional classes.
Note: Start by making copies of your Building.java, SkylinePanel.java and Skyline.java files. Always keep a copy of working code before making changes.
Update the Skyline application you wrote in the last programming assignment to include randomness in the size and number of the buildings and windows.Name your program RandomSkyline.java.Because of the randomness we will use loops to control how the skyline looks.Once again, the Splat example from Chapter 4 is useful; as well as the paintComponent() methods from Chpater 5 Bullseye and Boxes examples. Your program needs to make the following changes to the 3 classes from the Skyline application:
1) Change Building constructor to set the number of windows to a random number. The draw method now needs a loop to draw this random number of windows.Remember to keep the windows within the dimensions of the building.
2) SkylinePanel the constructor will no longer instantiate the buildings.Its only job is to set preferred size and background color.
The building instantiation will now be done within the paintComponent() method as part of a loop. Here is the algorithm (this is VERY high-level, you still have plenty of designing to do):
1. generate random number
for each number from one to random number
2. generate random width
generate random height
3. instantiate building
4. draw building
5. generate random gap between next building
6. update distance from left side of frame
First generate a random number.Instantiate that number of buildings. Generate a random width and height. Use these random numbers as parameters to the building's constructor. Also, have the gap between buildings be a random number of pixels. This gap plus width of buildings creates the x coordinate (left edge of skyline) parameter. You can choose how you want to calculate y coordinate parameter (it is OK of buildings are drawn past the bottom of the frame).
3)Rename the class called Skyline to RandomSkyline NO other changes are needed.
4) Additionally, you’ll need to name your city. Use your own name(s).
Extra Credit:Create a fourth class called Star. Fill the skyline with a random number of stars. Put this into a file named Star.java. Note: you will need to draw the stars in the paintComponent() method of SkylinePanel.
I don't understand this at all (except for #s 3 and 4). I don't get how to add windows, and I especially don't get the #2. Will someone please show me how to do this. Also, I can't use arrays yet since we're not up to those in class yet. Here's my code for the original Skyline that was mentioned.
Code:
import java.awt.*;
public class Building
{
private int x, y, width, height;
private Color color;
private String string;
//-----------------------------------------------------------------
// Constructor: Sets up this building with the specified values.
//-----------------------------------------------------------------
public Building (Color shade, int upperX, int upperY, int nWidth, int nHeight) {
color = shade;
x = upperX;
y = upperY;
width = nWidth;
height = nHeight;
}
//-----------------------------------------------------------------
// Draws this building in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (color);
page.fillRect (x, y, width, height);
}
//-----------------------------------------------------------------
// Color mutator.
//-----------------------------------------------------------------
public void setColor (Color shade)
{
color = shade;
}
//-----------------------------------------------------------------
// X mutator.
//-----------------------------------------------------------------
public void setX (int upperX)
{
x = upperX;
}
//-----------------------------------------------------------------
// Y mutator.
//-----------------------------------------------------------------
public void setY (int upperY)
{
y = upperY;
}
//-----------------------------------------------------------------
// width mutator.
//-----------------------------------------------------------------
public void setWidth (int nWidth)
{
width = nWidth;
}
//-----------------------------------------------------------------
// height mutator.
//-----------------------------------------------------------------
public void setheight (int nHeight)
{
height = nHeight;
}
//-----------------------------------------------------------------
// width accessor.
//-----------------------------------------------------------------
public int getWidth ()
{
return width;
}
//-----------------------------------------------------------------
// height accessor.
//-----------------------------------------------------------------
public int getHeight ()
{
return height;
}
//-----------------------------------------------------------------
// Color accessor.
//-----------------------------------------------------------------
public Color getColor ()
{
return color;
}
//-----------------------------------------------------------------
// X accessor.
//-----------------------------------------------------------------
public int getX ()
{
return x;
}
//-----------------------------------------------------------------
// Y accessor.
//-----------------------------------------------------------------
public int getY ()
{
return y;
}
}
import javax.swing.*;
import java.awt.*;
public class SkylinePanel extends JPanel
{
private Building building1, building2, building3, building4, building5;
//-----------------------------------------------------------------
// Constructor: Creates five Building objects.
//-----------------------------------------------------------------
public SkylinePanel()
{
building1 = new Building (Color.black, 100, 380, 40, 100);
building2 = new Building (Color.black, 170, 290, 80, 190);
building3 = new Building (Color.black, 285, 80, 100, 400);
building4 = new Building (Color.black, 450, 270, 80, 210);
building5 = new Building (Color.black, 580, 360, 45, 120);
setPreferredSize(new Dimension(640, 480));
setBackground (Color.cyan);
}
//-----------------------------------------------------------------
// Draws this panel by requesting that each building draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
building1.draw(page);
building2.draw(page);
building3.draw(page);
building4.draw(page);
building5.draw(page);
}
}
Last edited:
