import javax.swing.JFrame;
public class Building
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Building");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new BuildingPanel());
frame.pack();
frame.setVisible(true);
}
}
//Building Panel
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class BuildingPanel extends JPanel
{
private Rectangle rec1, rec2, rec3, rec4;
//number of windows
private final int NUM_BOXES = 10;
//set up boundaries of windows, so the windows would not go out of walls
private final int boundary = 3;
private final int X = 40-boundary*2, Y = 190-boundary*2;
private final int X2 = 50-boundary*2, Y2 = 170-boundary*2;
private final int X3 = 70-boundary*2, Y3 = 215-boundary*2;
private final int X4 = 90-boundary*2, Y4 = 283-boundary*2;
//Define Randomness
private Random generator;
public BuildingPanel()
{
generator = new Random();
//width,height,x,y
rec1 = new Rectangle (40,190,Color.black,20,210);
rec2 = new Rectangle (50,170,Color.black,90,230);
rec3 = new Rectangle (70,215,Color.black,170,185);
rec4 = new Rectangle (90,283,Color.black,310,117);
setPreferredSize (new Dimension (400,400));
setBackground (Color.blue);
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);
int x,y,width,height;
//set up window size
int windows = 3;
rec1.draw(page);
rec2.draw(page);
rec3.draw(page);
rec4.draw(page);
//set up windows boundaries
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X)+ 20 + boundary;
y = generator.nextInt(Y)+ 210 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X2)+ 90 + boundary;
y = generator.nextInt(Y2)+ 230 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES; count++)
{
x = generator.nextInt(X3)+ 170 + boundary;
y = generator.nextInt(Y3)+ 185 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
for (int count=0; count< NUM_BOXES+10; count++)
{
x = generator.nextInt(X4)+ 310 + boundary;
y = generator.nextInt(Y4)+ 117 + boundary;
width = height = windows;
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
}
}
Thanks.

New Topic/Question
Reply




MultiQuote



|