• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused on how to create a rectangle

 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a rectangle for my project but I'm stuck and confused about creating the code.

here is the project: "Box.java should have these instance variables (all of type int): upperLeftX, upperLeftY, height, width, and one more instance variable of type Color named boxColor. Make them all private and provide Getters and Setters for each instance variable. Tip: Eclipse will build getters and setters for you. Go to the Menu bar in Eclipse and select Source -> Generate Getters and Setters.

Provide the body of the following Constructor that sets the values of the instance variables:

public Box(int upperX, int upperY, int h, int w, Color myColor)

Create a method called display(Graphics g) that uses setColor(boxColor); and fillRect(upperLeftX,upplerLeftY,width, height); to fill our box".

my code so far:


 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where exactly are you stuck?
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the display(graphic g) method.

Is that just another form of paint() or is it a different type of method?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't really have anything to do with the paint() method. You just have to create a method named "display" that takes a Graphics object as its parameter, as the instructions say.

By the way, in your code above the constructor that you're supposed to create is also still missing.
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

great! I know but I just wanted to get my question answered.

Thanks for the help
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I finished up the code but I'm still getting quite a bit of errors.

Lines 6-9 state that "=" is an invalid variabledeclarator

Line 26: the method setColor(Color) is undefined for the type Box

27: Multiple markers at this line
- The field Component.height is not
visible
- upperLeftY cannot be resolved to a
variable
- upperLeftX cannot be resolved to a
variable
- The field Component.width is not
visible

here is the new code:
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you really follow an assignment operator by a semicolon like that? I would have thought was a syntax error. You also need to precede the name of a variable by its type. You got it right with the colour. You should simply declare the fields, and set them all up in the constructor (remembering to omit types in the constructor).
The fields you describe as not visible are obviously in another class.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kenny gill wrote:Create a method called display(Graphics g) that uses setColor(boxColor); and fillRect(upperLeftX,upplerLeftY,width, height); to fill our box".

That sentence is incomplete. What the assignment meant to say is that it should call these two methods on the Graphics object. You should not create your own fillRect method and call that, and calling setColor(boxColor) will do nothing as it will change boxColor to boxColor.

kenny gill wrote:

I don't really get why you have the declaration of boxColor right but can't get the declaration of the other four fields to work. You've even been told the type they should get (int).
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is not valid. you don't have a type for any of these variables. The compiler therefore thinks the name is the type, and the '=' is the name. you need something like



and then the appropriate constructors.

More generally, I'd say you have written WAY too much code before you compiled. NEVER write more than 2-3 lines at MOST before you attempt to recompile. It lets you focus in on where the problem is and not get overwhelmed with "quite a bit of errors"
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sorry 'bout that.

I edited the statements like you said but when I select Getters and Setters do I make them private or public?

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you understand what it means to make a method public vs private?

a public method can be called by other objects. you made your variables private so they can't be directly accessed by other classes, and now you need some way for other classes to be able to set the variables. If you make the setters private, how will you accomplish this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic