I need help with Java I need to draw a building with multipl

I need help with Java, I need to draw a building with multiple rows of windows, implementing the overrident method paintComponent. And the requirements below. I know how to draw the building but cant do the windows and the colors, Can someone help me get this done

Requirements:

Use for loops to draw the windows.

Use random colors (every time the building is repainted some of the colors change randomly.

Which of the areas change is up to you.

***************************************

// building class
import javax.swing.JPanel;

@SuppressWarnings(\"serial\")
public class Building extends JPanel {
  
   @Override
   protected void paintComponent(Graphics g) {
super.paintComponent(g);

// TODO: write code to draw the building

   }

}

*********************************************

// buildingApp class

package labBuilding;

import javax.swing.JFrame;

@SuppressWarnings(\"serial\")
public class BuildingApp extends JFrame {

   public static void main(String[] args) {
new BuildingApp().run();
   }
  
   public void run() {
setBounds(100, 10, 400, 500);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new Building());
   setVisible(true);
   }

}

Solution

/*To perform the operation first of all we needs to create a package with name labBuilding and then save the class file with name Building.class in this package.

In main program BuildingApp we need to import the package labBuilding class Building using

import labBuilding.Building;

The following program can be used after some modification in above given code to get the window generated with different random colors of object */

/* This Program create a building (filled rectangle) with random color using looping statememt */

// building class

package labBuilding;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.*;
import java.awt.*;

@SuppressWarnings(\"serial\")
public class Building extends JPanel {

   @Override
   protected void paintComponent(Graphics g) {
super.paintComponent(g);

// TODO: write code to draw the building
   int i,j,randomNum,min,max;
   min=0;max=255;
   for(i=0;i<=100;i++)
   {
       randomNum = min + (int)(Math.random() * max);
       g.setColor(new Color(randomNum,80,50));
       g.fillRect (i+10,i+10,i+50,i+60); //draw a rectabgle with given co-ordinates with loop);
       repaint();
   }
   }

}

/* Following program is main program with name of the class is BuildingApp */

import javax.swing.JFrame;
import labBuilding.Building;
@SuppressWarnings(\"serial\")
public class BuildingApp extends JFrame {

   public static void main(String[] args) {
new BuildingApp().run();
   }

   public void run() {
setBounds(100, 10, 400, 500);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new Building());
   setVisible(true);
   }

}

I need help with Java, I need to draw a building with multiple rows of windows, implementing the overrident method paintComponent. And the requirements below. I
I need help with Java, I need to draw a building with multiple rows of windows, implementing the overrident method paintComponent. And the requirements below. I

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site