I need to design a class called Building that represents a drawing of a building(the parameters to the constructor should be the building's height and width). I need to color the buildings black with a few random windows colored yellow. Then I need to make an applet that draws a random skyline of buildings.

This is what I have.
CLASS:

import java.awt.*;
import java.util.*;

public class Building
{
    private final int SET_WIDTH = 50;
    private final int SET_HEIGHT = 100;
    private final int SET_WIND = 5;
    private final int MAX_WIND = 15;
    private final int WIND_SIZE = 5;

    private int width, height, windows;

    Random gen = new Random();
    //----------------------------------------------------------------------------------------------
    //  Constructor. Set values.
    //----------------------------------------------------------------------------------------------
    public Building ()
    {
        width = SET_WIDTH;
        height = SET_HEIGHT;
        windows = SET_WIND;
    }

    //----------------------------------------------------------------------------------------------
    //  Sets building size. Decides number of windows.
    //----------------------------------------------------------------------------------------------
    public Building (int bWidth, int bHeight)
    {
        width = bWidth;
        height = bHeight;;
        windows = (gen.nextInt() * MAX_WIND) + 1;
    }

    //----------------------------------------------------------------------------------------------
    //  Get width.
    //----------------------------------------------------------------------------------------------
    public int getWidth ()
    {
        return width;
    }

    //----------------------------------------------------------------------------------------------
    //  To draw building.
    //----------------------------------------------------------------------------------------------
    public void draw (Graphics page, int leftC, int bottom)
    {
        int top = bottom - height;

        page.setColor (Color.black);                 // building
        page.fillRect (leftC, top, width, height);

        page.setColor (Color.yellow);                 // windows
        for (int count = 1; count <= windows; count++)
        {
            int windX, windY;

            windX = leftC + (gen.nextInt() * (width - WIND_SIZE));
            windY = top + (gen.nextInt() * (height - WIND_SIZE));

            page.fillRect (windX, windY, WIND_SIZE, WIND_SIZE);            
        }
    }
}

AND THE OBJECT/APPLET:

import java.awt.*;
import java.util.*;
import java.applet.Applet;

public class Skyline extends Applet
{
    private final int APPLET_WIDTH = 550;
    private final int APPLET_HEIGHT = 200;
    private final int MAX_GAP = 40;
    private final int MIN_GAP = 5;

    Random gen = new Random();
    //----------------------------------------------------------------------------------------------
    //  Creates different building objects.
    //----------------------------------------------------------------------------------------------
    Building b1 = new Building (55, 75);
    Building b2 = new Building (40, 170);
    Building b3 = new Building ();
    Building b4 = new Building (75, 135);
    Building b5 = new Building (30, 150);
    Building b6 = new Building ();
    Building b7 = new Building (25, 100);


    //----------------------------------------------------------------------------------------------
    //  Draws building with different gap distances.
    //----------------------------------------------------------------------------------------------
    public void paint (Graphics page)
    {
        setBackground (Color.cyan);
        setSize (APPLET_WIDTH, APPLET_HEIGHT);


        int gapVar, base, gap, leftC;

        gapVar = MAX_GAP - MIN_GAP;
        base = APPLET_HEIGHT - 10;
        gap = (gen.nextInt() * gapVar) + MIN_GAP;
        leftC = gap;

        page.drawLine (0, base, APPLET_WIDTH, base);

        b1.draw (page, leftC, base);

        gap = (int) (Math.random() * gapVar) + MIN_GAP;
        leftC += b1.getWidth() + gap;
        b2.draw (page, leftC, base);

        gap = (int) (Math.random() * gapVar) + MIN_GAP;
        leftC += b2.getWidth() + gap;
        b3.draw (page, leftC, base);

        gap = (int) (Math.random() * gapVar) + MIN_GAP;
        leftC += b3.getWidth() + gap;
        b4.draw (page, leftC, base);

        gap = (int) (Math.random() * gapVar) + MIN_GAP;
        leftC += b4.getWidth() + gap;
        b5.draw (page, leftC, base);

        gap = (int) (Math.random() * gapVar) + MIN_GAP;
        leftC += b5.getWidth() + gap;
        b6.draw (page, leftC, base);

        gap = (int) (Math.random() * gapVar) + MIN_GAP;
        leftC += b6.getWidth() + gap;
        b7.draw (page, leftC, base);

    }
}

It is not working. Nothing is showing up when I run the applet.

Try debugging the code by printing the values of the variables used in the draw() method to see where the fillRect() method is drawing.

Line 58 & 59, what do you think the value that nextInt() method will return? Read its API and you will find out that the range of returned value (which will be between 0 to 2^32). It is obvious that it will be out of display range you are trying to do...

Ok I got it. It doesn't close the Applet runner after I run it still.. I have to turn off the computer. That's weird. But either way, I got it. Thank you!

This question has already been answered. Start a new discussion instead.