Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a new method for an existing object?

 
Tania Bull
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've started with Java only a few weeks ago. So, if this sounds like a silly question, please forgive me.
I'm trying to create an applet which would display a rectangle which can be resized at run-time. So, naturally I thought that I needed to create a Rectangle object, which is not a problem in itself. The catch is that Rectangle doesn't know how to display itself. I probably need to create a method for my rectangle which looks something like this:
public void display(Graphics g);{
g.drawRect(x,y,width,height);
}
And then I would need to call the paint() method:
rect.display(gc);
But it doesn't work. What am I doing wrong?
Thanks.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's just a problem with the way you have copied your code to here, but you have an extra ';' after your first ')' which will (in this case) compile OK, but cause your code to behave very strangely. Maybe it should really be:
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you calling repaint()? That is where you tell the system that it is time to clear the screen and paint() it again.
 
Tania Bull
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are both onto something here.
Althought I didn't have a ";" in the wrong spot, I did have some non-printable characters in that place. I was able to see them when I enabled non-printable chars to be displayed in HomeSite.
And yes, I forgot to put repaint() again.
I'll have another go.
Thanks, guys.
 
Tania Bull
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realised something. When I pressed the paragraph icon in HomeSite, I thought I was enabling the non-printable chars (like you would in Word). But that's what actually posted those

tags (which I thought were non-printable chars). It just so happened that I had my cursor sitting in the "right place".
What do you know! You learn something every day if you are not careful :-).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic