• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with objects and classes  RSS feed

 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks,
Here is the program that i have written which creates a class called Brick and then creates an instance of this class called myBrick. My problem is that my code does almost everything it's supposed to do but one little problem that instead of displaying a brick (basically a rectangle) i see a line that responds to every single event like grow, shrink, etc... Could anyone help me figure out what am i doing wrong and i did use drawRect().
Thanx,
kt

 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brick's constructor is the problem.You declare new local variables to hold the parameter values. When the constructor exits, the local variables disappear. Removing the four "int"s should fix the problem. You might want to use "this.x = ..." to be very clear that you're setting the instance variables.
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't examined your code carefully, and I'm no GUI maven, but one problem that sticks out right away is that you never initialize the private coordinate ints x and y when you construct a Brick. Instead, you write

What this does is introduce variables local to the constructor--it does NOT initialize the member variables declared above it. These local variables disappear the moment the call to the constructor is popped off the stack. As it happens, an object's member variables are initialized to default values (in this case 0) when it is created; so what you end up with is an effectively dimensionless Brick located at (0,0) whenever you make a new Brick.

Hope that helps.
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Harkness:
Brick's constructor is the problem.You declare new local variables to hold the parameter values. When the constructor exits, the local variables disappear. Removing the four "int"s should fix the problem. You might want to use "this.x = ..." to be very clear that you're setting the instance variables.


thanx, David. It did fix the problem. I am beginner trying hard to learn java and since i didn't get any compilation error i didn't look at that spot carefully. I appreciate you taking time to help me out.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help. Also, make sure you read Adam's post as he actually explains what happens behind the scenes in the original code. Heh, that's why I beat him by four minutes! Those details will help you remember it later and also give you a better understanding of how the language works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!