Homework question (Java)

Status
Not open for further replies.

psx2514

Junior Member
Oct 17, 2012
16
0
0
I had to make an applet of a Skyline using 3 classes (A Skyline Class, a Building Class, and a Skyline Panel class). I finished that part (I made my Skyline), but now I need to add yellow windows to the buildings (say...3-5 windows per building), and I'm not sure how to do that. Will someone please help me.

The assignment says (in reference to the Building class), "draw() is where the windows are drawn at an x,y location within the dimensions of the building. You choose the size of the windows and how many.

This class contains a constructor, Building(int upperX, int upperY, int nHeight, int nWidth), that initializes the current building's x/y location and height and width using the parameters. It also initializes the number of windows the building contains."


I don't understand the part about the windows. I was able to construct the buildings though. Here's part of my code.

Code:
import java.awt.*;

public class Building
{
   private int x, y, width, height;
   private Color color;
   private String string;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this building with the specified values.
   //-----------------------------------------------------------------
   public Building (Color shade, int upperX, int upperY, int nWidth, int nHeight) {
      color = shade;
      x = upperX;
      y = upperY;
      width = nWidth;
      height = nHeight;
   }
   
   //-----------------------------------------------------------------
   //  Draws this building in the specified graphics context.
   //-----------------------------------------------------------------
   public void draw (Graphics page)
   {
      page.setColor (color);
      page.fillRect (x, y, width, height);
   }
 
   //-----------------------------------------------------------------
   //  Color mutator.
   //-----------------------------------------------------------------
   public void setColor (Color shade)
   {
      color = shade;
   }

   //-----------------------------------------------------------------
   //  X mutator.
   //-----------------------------------------------------------------
   public void setX (int upperX)
   {
      x = upperX;
   }


   //-----------------------------------------------------------------
   //  Y mutator.
   //-----------------------------------------------------------------
   public void setY (int upperY)
   {
      y = upperY;
   }
   //-----------------------------------------------------------------
   //  width mutator.
   //-----------------------------------------------------------------
   public void setWidth (int nWidth)
   {
      width = nWidth;
   }

   //-----------------------------------------------------------------
   //  height mutator.
   //-----------------------------------------------------------------
   public void setheight (int nHeight)
   {
      height = nHeight;
   }

   //-----------------------------------------------------------------
   //  width accessor.
   //-----------------------------------------------------------------
   public int getWidth ()
   {
      return width;
   }
   //-----------------------------------------------------------------
   //  height accessor.
   //-----------------------------------------------------------------
   public int getHeight ()
   {
      return height;
   }
   //-----------------------------------------------------------------
   //  Color accessor.
   //-----------------------------------------------------------------
   public Color getColor ()
   {
      return color;
   }

   //-----------------------------------------------------------------
   //  X accessor.
   //-----------------------------------------------------------------
   public int getX ()
   {
      return x;
   }

   //-----------------------------------------------------------------
   //  Y accessor.
   //-----------------------------------------------------------------
   public int getY ()
   {
      return y;
   }

}

import javax.swing.*;
import java.awt.*;

public class SkylinePanel extends JPanel
{
   private Building building1, building2, building3, building4, building5;
   private Window window1;
   //-----------------------------------------------------------------
   //  Constructor: Creates five Building objects.
   //-----------------------------------------------------------------
   public SkylinePanel()
   {
      building1 = new Building (Color.black, 100, 380, 40, 100);
      building2 = new Building (Color.black, 170, 290, 80, 190);
      building3 = new Building (Color.black, 285, 80, 100, 400);
      building4 = new Building (Color.black, 450, 270, 80, 210);
      building5 = new Building (Color.black, 580, 360, 45, 120);
      
      setPreferredSize(new Dimension(640, 480));
      setBackground (Color.cyan);
     
   }
  
   //-----------------------------------------------------------------
   //  Draws this panel by requesting that each building draw itself.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent(page);

      building1.draw(page);
      building2.draw(page);
      building3.draw(page);
      building4.draw(page);
      building5.draw(page);
      
   
   }
   
}

This thread is effectively a dupe and hereby closed. Please keep everything in the other thread.
-ViRGE
 
Last edited by a moderator:

Maximilian

Lifer
Feb 8, 2004
12,603
9
81
For the windows you would need to use all 4 variables passed to the constructor the buildings current X,Y and its height/width to figure out where they will be drawn.

Perhaps using a for loop nested within another for loop to draw each window, the outer for loop would control the current row (windows Y position), the inner would control the current column (windows X position) drawing each row until the building is populated with windows.

Might have to dick around with it a bit to make it look right but that's the general approach i would take.
 

Broheim

Diamond Member
Feb 17, 2011
4,592
2
81
For the windows you would need to use all 4 variables passed to the constructor the buildings current X,Y and its height/width to figure out where they will be drawn.

Perhaps using a for loop nested within another for loop to draw each window, the outer for loop would control the current row (windows Y position), the inner would control the current column (windows X position) drawing each row until the building is populated with windows.

Might have to dick around with it a bit to make it look right but that's the general approach i would take.

this, you have all the info you need in the fields, all you need is a window width an height and a margin between the windows that you decide yourself. as maximilian said, two loops and a 5 ints are all you need (x, y and width, height and margin), thow in a couple of ifs if you want it to look nicer.
 

KB

Diamond Member
Nov 8, 1999
5,397
383
126
I would set a standardized size window, say 10 x 10 pixels. Then in the building constructor determine the height and width of the building class and divide height and width by the size of the window (plus some sort of offset to create space between the windows), to get the number of windows the building can hold in the X and Y direction.

I would create an array (could be two-dimensional) of window classes (each having its own X, Y coordinate) in the building class and add the number of windows to this array. The X and Y coordinates of the windows would be determined as Maximilian described.

Then have the building class draw the windows in the buildings draw method much like you are drawing the building.
 

psx2514

Junior Member
Oct 17, 2012
16
0
0
For the windows you would need to use all 4 variables passed to the constructor the buildings current X,Y and its height/width to figure out where they will be drawn.

Perhaps using a for loop nested within another for loop to draw each window, the outer for loop would control the current row (windows Y position), the inner would control the current column (windows X position) drawing each row until the building is populated with windows.

Might have to dick around with it a bit to make it look right but that's the general approach i would take.


Thanks. Could you give me an example though? I learn better when I actually see it done rather than it just being explained to me. Also, we're not using arrays yet, so I can't jump ahead and use those quite yet.

I also actually have to make it so the program generates a random number of windows. Then I have to make it so the program randomly generates the width and height of the buildings. Then I have to write a for loop that constructs the buildings.
 

psx2514

Junior Member
Oct 17, 2012
16
0
0
I would set a standardized size window, say 10 x 10 pixels. Then in the building constructor determine the height and width of the building class and divide height and width by the size of the window (plus some sort of offset to create space between the windows), to get the number of windows the building can hold in the X and Y direction.

I would create an array (could be two-dimensional) of window classes (each having its own X, Y coordinate) in the building class and add the number of windows to this array. The X and Y coordinates of the windows would be determined as Maximilian described.

Then have the building class draw the windows in the buildings draw method much like you are drawing the building.

So, if my building is 40 x 100, that means it could hold 4 columns and 10 rows of windows, right?
 

cl-scott

ASUS Support
Jul 5, 2012
457
0
0
WOW!!! I made TWO posts, and I'm spamming? Maybe the topics are somewhat different? And good job on perpetuating the stereotype of the arrogant prick programmer. I hope most programmers aren't like you.

No, but I do seem to recall seeing this same question posted over on Tom's Hardware forums, so it would stand to reason you are posting it several other places.

Plus, it's homework. If you can't do it, then you haven't mastered the skills for the course yet, and so should fail. What happens if you graduate, get a job, and then are tasked with doing something like this? Aside from quickly being shown the door that is.
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
No, but I do seem to recall seeing this same question posted over on Tom's Hardware forums, so it would stand to reason you are posting it several other places.

Plus, it's homework. If you can't do it, then you haven't mastered the skills for the course yet, and so should fail. What happens if you graduate, get a job, and then are tasked with doing something like this? Aside from quickly being shown the door that is.

There's nothing wrong with asking for help on homework, so don't berate the OP too much.

I would, however, like to see a little more effort put in before asking a question like "I don't get this, how do I do it?" If you (OP) have specific questions about syntax or common algorithm techniques, people will be more than happy to help and/or point you to reference materials.

Saying "I don't know how to do this, tell me how to do it." isn't going to fly.
 

psx2514

Junior Member
Oct 17, 2012
16
0
0
There's nothing wrong with asking for help on homework, so don't berate the OP too much.

I would, however, like to see a little more effort put in before asking a question like "I don't get this, how do I do it?" If you (OP) have specific questions about syntax or common algorithm techniques, people will be more than happy to help and/or point you to reference materials.

Saying "I don't know how to do this, tell me how to do it." isn't going to fly.

First of all, thanks for your help, and for not being an arrogant prick like so many programmers seem to be.

I showed you the program I wrote. Isn't that putting in effort? There's just one part I don't understand.

Also, this is an intro class, so don't expect me to know the same things as someone who has been programming for 15 years.
 

douglasb

Diamond Member
Apr 11, 2005
3,163
0
76
Asking nicely goes a long way. Calling people arrogant pricks because they don't immediately give you the answer you want does not go over so well. If you are having this much trouble with an assignment like this, and you are unable to find an answer on your own, with help from Google, you will have a rough road ahead. Best of luck to you.
 

psx2514

Junior Member
Oct 17, 2012
16
0
0
Asking nicely goes a long way. Calling people arrogant pricks because they don't immediately give you the answer you want does not go over so well. If you are having this much trouble with an assignment like this, and you are unable to find an answer on your own, with help from Google, you will have a rough road ahead. Best of luck to you.


I didn't ask for a lecture. Why do self-righteous people like you always have to do stuff like this?

The hostility here is unwarranted and unappreciated. Before posting any further, please check your inbox, you have a PM from me.
-ViRGE
 
Last edited by a moderator:
Status
Not open for further replies.