Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3236798
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:39:49+00:00 2026-05-17T17:39:49+00:00

I’m a Java novice taking a beginning programming class and I’m trying to finish

  • 0

I’m a Java novice taking a beginning programming class and I’m trying to finish my homework assignment (due tomorrow!) and I’ve been banging my head against the wall trying to figure out what I’m doing wrong.

Here’s the basic assignment:

  1. create a Building class that takes parameters for x and y location – this I have done no problem

  2. use a Panel class to draw a random number of Buildings – this is where I am having the trouble.

I have done this kind of thing many times in Actionscript and Javascript but for some reason I cannot get it to work in Java.

my strategy has been:
create a random number r (done)
set a ‘for’ loop to run from 0 to r
call the constructor for a new Building
add that Building to an Array (or an ArrayList)
draw the Building
end for loop

I have tried multiple variations of this (using an Array instead of an ArrayList for ex) but here is my current code:

here is my code:

public class CityPanel extends JPanel
{
 private Bldg bldg
 ArrayList<Bldg> bs = new ArrayList<Bldg>();


 //------------------------------------------------------------------------
 //Constructor; Creates five building objects
 //------------------------------------------------------------------------

 public CityPanel()
 {



  setPreferredSize (new Dimension(430, 400));
  setBackground (new Color(0x46, 0x82, 0xb4));
 }

 //------------------------------------------------------------------------
 //Draws this panel by requesting each object draw itself.
 //------------------------------------------------------------------------

 public void paintComponent (Graphics page)
 {
  int b = (int) Math.floor(Math.random() * 10);

  for( int i = 0; i < b; i++ ) {
   intArr[i] = new Integer( i );
   bldg = new Bldg(100*i, 300);
   bs.add(bldg);
   super.paintComponent(page);
   bldg.draw(page);
   System.out.print(bs.get(i).toString());
  }
}

when I run the main program (a City class that draws the panels etc) I get an empty frame.

if I just put in a constructor function without the ‘for’ loop I get the Building(s) to draw, so the problem lies somewhere in my ‘for’ loop and the way I’m trying to make and/or store the objects.

when I google “java creating multiple objects” or “random” or what-have-you I get a lot or results but nothing relevant!

sorry for the long post – I’m not sure how to ask the question!

thanks in advance.
b

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:39:49+00:00Added an answer on May 17, 2026 at 5:39 pm

    Make your buildings in the constructor

    int b = (int) Math.floor(Math.random() * 10);
    
    for( int i = 0; i < b; i++ ) {
       bldg = new Bldg(100*i, 300);
       bs.add(bldg);
    }
    

    and then change your paintComponent to

    public void paintComponent (Graphics page)
    {
       super.paintComponent(page);
    
       for (Building bldg : bs) {
         bldg.draw(page);
       }
    }
    

    Give that a try. You should only call super.paintComponent once

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.