GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How to create a new method for an existing object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create a new method for an existing object?" Watch "How to create a new method for an existing object?" New topic
Author

How to create a new method for an existing object?

Tania Bull
Greenhorn

Joined: May 28, 2001
Posts: 3
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

Joined: Jan 07, 1999
Posts: 6920
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:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Are you calling repaint()? That is where you tell the system that it is time to clear the screen and paint() it again.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Tania Bull
Greenhorn

Joined: May 28, 2001
Posts: 3
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

Joined: May 28, 2001
Posts: 3
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 :-).
 
GeeCON Prague 2014
 
subject: How to create a new method for an existing object?