This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, I am new to coderanch, obviously from my number of posts, eh? Quick intro: I am having trouble getting java stuck in my head, and then as soon as it seems like I have it figured out, it doesn't work. I have to say I am very glad to find a site that tolerates noobs and has people willing to help! I do the same thing on another forum helping people with marine electronics installation and repair (I assume I shouldn't post links). I am currently working on an AAS networking degree and CCNA. This is my first time with java, and the last time I programmed was in C, and a lot of years ago.
And, off to the important stuff. I need help figuring out why my draw method is not working correctly. This is the skyline programming project out of the Lewis/Loftus Java book (with a couple instructor twists). We are only beginning, so we do not have anything much more complicated to work with than what you see. This project is pressing the point of creating objects, and then having the objects draw themselves on the panel. There is some extra stuff going on, like windows randomly generating their own colors, and a bunch of mutators that aren't being used right now.
I think I pretty much have it. All files compile, and the frame/panel comes up with the correct background color, and the windows all show up in their randomly generated colors. The problem is that I cannot seem to get the buildings to draw. Whether they draw at all or are just not visible, I am not sure. I have stared it at for hours, and tried re-doing things and playing with not using variables. I have looked around by google, but didn't find anything super helpful. The answers I usually find on google are more confusing than what caused me to look for help!
notes: the reason the ranges on the randoms are that way is so that it gives me a small buffer on the edges of the panel. Then, I tried to make the windows stay inside the buildings.
I think this method is where my problem lies:
I did look around and read posts on here, and tried to make sure I posted in the best way possible to get help. I tried to attach all three java files, but apparently I cannot do that. Please let me know if there is a better way I should post in the future. And remember, I am as noob as noob gets when it comes to java!
Thank you in advance, I do very much appreciate it!
a lot will depend on where you're getting 'Graphics page'
if it is via any flavor of getGraphics(), then that's worth a self-inflicted triple-upper-cut.
just looking at the code, you should be 'constructing' a building using a JPanel,
not just drawing a shape - the panel itself should be the building.
here's a simple (rough, very rough) demo
don't do anything inside paintComponent other than paint.
and Random stuff do elswhere, use the random numbers to change the buildings'
coords, then call [building].repaint()
Joined: Feb 10, 2012
Oh wow. You went so far above me on most all that. I have not learned a few of those statements you used, either. I had a feeling that a person would need to see a lot more than that little snippet. I did go an move all the random generation parts up into the constructor. I hope this does not cause trouble to post all this.
Okay, here is my driver program file:
And here is my panel class file:
Lastly, the building class file with constructor and methods (minus all the mutators that are not currently in use anyway):
Joined: Jun 09, 2003
nothing's being drawn because width and height are always 0 i.e. 0 size
bwidth = width;
bheight = height;
width = bwidth;
height = bheight;
Joined: Feb 10, 2012
:o I knew it was going to be something stupid!
I am sure I will be back with more questions. In the meantime, I have been looking through some other questions and have learned a bit.
Now I just have to figure out how to adjust the random placement to get my windows put in the right places.
Thank you so much, I really, really do appreciate it!
Joined: Feb 10, 2012
I fixed my math on the window placement, and made each window it's own randomly generated color. I think it looks bad doing the random x, y coordinates on the buildings, because most every time they end up on top of each other with big spaces in between, but project complete per my class requirements. Next week she said we get to learn the if statements and then the loop statements. I think then that I will be able to control the building placement better, just for the fun of it.