JavaRanch » Java Forums »
Java »
Swing / AWT / SWT
| Author |
drawRect()
|
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Hi, I'm having trouble using the drawRect method. I'm using a JTabbedPane which calls the class FamilyTree which in turn calls the RectanglesCanvas class, not sure what i'm doing wrong. Thanks.
|
 |
Nathan Pruett
Bartender
Joined: Oct 18, 2000
Posts: 4121
|
|
|
The very first thing I noticed is that RectangleCanvas extends Canvas. This is an AWT component, and you are using Swing components in other places. This causes no end of problems, but is easy to fix. Instead of subclassing Canvas, just subclass JPanel, and change paint(Graphics) to paintComponent(Graphics).
|
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
|
Thanks, but the rectangles still don't seem to be displaying
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
The line paintComponent(g); will cause an infinite loop right ? I think you want super.paintComponent( g ); D.
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Thanks Don, But it still doesn't seem to return the panel.
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
post your latest code. D.
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
I have cut out the middle class FamilyTree just to simplify it
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
The code you posted doesn't appear to call draw() or drawRect() or anything of relevence...
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Whoops, posted the same file twice, here there are again:
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
|
RectanglesCanvas.paintComponent draws the Rectangle it finds in the member variable "rect" of the enclosing class. But I don't see where this variable is ever initialzed -- i.e., it's null, so there's nothing to draw. Actually, you ought to be getting stack traces because of this.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Sorry meant to use the following: Is there a scope problem with this?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
So, looking back through this thread, I don't see where you actually told us what the problem is we're trying to solve. Does this compile? Does it run but crash? Does it run but do nothing? Do some tabs work, but not others? How about if you just stuff the RectanglesApplet into a JFrame, without the JTabbedPane and all the other stuff. Does it show up? What do you see?
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
|
It compiles and runs, the other tabs work ok, but this tab is blank. I tried to simplify it too and it still shows up blank. Presume i'm accessing it wrong.
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
|
Anyone any ideas?
|
 |
 |
|
|
subject: drawRect()
|
|
|
|