File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with custom painting and multiple JPanel

 
Thang Pham
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning some custom painting and I encounter some problems. I want to create a frame that when I click a button, the component of the frame get redraw. It worked, however, since I add all the components onto 1 panel, I cant control its layout. So I put every component onto a separate panel, and add all these panel to one main panel, and at the end, add that main panels to the frame. Now my paintComponent(Graphics g), dont work anymore. I think since I create multiple panel, when I call repaint(), it does not know what panel to repaint any more.

Here is when I only have one panel. Custom painting work fine


Here is the code with multiple panels. Custom painting stop working

 
Bartek Myszkowski
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In "multiple panels" code, in class called MyPanel, You extend JPanel and override paintComponent to create custom painting but also You create mainPanel where You add other panels and You use only mainPanel which method paintComponent is not overridden! mainPanel is not object of MyPanel!

I have fixed Your code (and it works - it does custom painting) - compare it with code You have provided!!!
 
Thang Pham
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Thang Pham
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bartek Myszkowski, your help was great. So after I fix that problem, I develop a bit more and ran into another problem. Before I have a Textfield on SOUTH, a button on NORTH. Now if I add a panel that contain a image to the CENTER, now I cannot see my string that draw by my paintComponent(). I know that the Image Panel cover it, is there a way that I can draw the string on top of that Image Panel. Or, if I want to draw graphics on the top panel layer, how can I do it? Thank you. Let me know if you need me to provide the code. Here is the baisc layout
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Now if I add a panel that contain a image to the CENTER, now I cannot see my string that draw by my paintComponent().
> I know that the Image Panel cover it, is there a way that I can draw the string on top of that Image Panel.

combine the two.

on this forum's main page there is a link to the faq, which has a background image faq.
add your drawString to it's paintComponent(..)
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross-posted: forums.sun.com Swing Section
 
Thang Pham
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael, I solve the problem already. I need to set the opaque to false
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic