tags:

views:

88

answers:

1

Hello all,

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  A: 

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

Shaun
+1, because paint() is called multiple times by the system, and because super.paintComponent() will fill everything with the background color and erase your buildings.
Guillaume
Yup - sure enough, that was it! Thanks to you guys for the help! relieved that it was something simple. I was beating my head against the table for hours with this one.
Bennett Von Bennett
@Jesper - you really should make your criticism constructive, or no one will read it.
Bennett Von Bennett