Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

drawRect()

 
Ritchie Partridge
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ritchie Partridge
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but the rectangles still don't seem to be displaying
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The line paintComponent(g); will cause an infinite loop right ? I think you want super.paintComponent( g );
D.
 
Ritchie Partridge
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Don,
But it still doesn't seem to return the panel.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your latest code.
D.
 
Ritchie Partridge
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have cut out the middle class FamilyTree just to simplify it
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted doesn't appear to call draw() or drawRect() or anything of relevence...
 
Ritchie Partridge
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, posted the same file twice, here there are again:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ritchie Partridge
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry meant to use the following:

Is there a scope problem with this?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone any ideas?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic