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
Joined: Feb 03, 2009
Posts: 44
posted
0
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
Joined: Jan 21, 2009
Posts: 35
posted
0
Thank you.
Thang Pham
Ranch Hand
Joined: Jan 21, 2009
Posts: 35
posted
0
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
Joined: Jun 09, 2003
Posts: 4632
posted
0
> 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(..)