• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem with custom painting and multiple JPanel

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(..)
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cross-posted: forums.sun.com Swing Section
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael, I solve the problem already. I need to set the opaque to false
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic