Forum Stats

  • 3,874,298 Users
  • 2,266,718 Discussions
  • 7,911,809 Comments

Discussions

How to change default background color of Panel?

843807
843807 Member Posts: 46,582
edited Jun 8, 2010 4:37PM in Abstract Window Toolkit (AWT)
In a paint function of the Panel I draw black background. But when I drag this panel I can see how this Panel is redrawing, and I can see flickering default system color of Panel (light color). I'd like to change this color not to see this flickering. (I'd like to change only default color of this panel so I can't use UIManager.put() function). How to get rid of this flickering?

ps. I have to use Panel, not JPanel.

Comments

  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    For better help sooner, post an SSCCE.

    Also, why are you coding using AWT based components in this millennium?
  • 843807
    843807 Member Posts: 46,582
    edited Jun 6, 2010 10:57AM
    I need AWT Panel becouse this panel overlaps heavyweight component. Despite new JDK can handle mixing heavyweight and lightweight components, it doesn't work for me, I get heavyweight component above swing component even if swing component was added with lower index. Component hierarchy looks like this:

    JPanel1 --------------------------------> JPanel2 -------------------------------> Heavyweight Component
    .......................|
    .......................|--------------------> JPanel3 (*Panel)


    I'd like JPanel3 to appear above Heavyweight component if both components overlap. If I use Panel instead of JPanel everything is fine.

    Edited by: cartoon_20 on Jun 6, 2010 7:56 AM
    I added dots because it truncates leading white characters
  • 843807
    843807 Member Posts: 46,582
    You can try setting the background color of the 2D graphic context associated with that object.
  • 843807
    843807 Member Posts: 46,582
    use .setOpaque(false); which makes the panel colour not take over the colour of the frame
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    babojee wrote:
    use .setOpaque(false); ..
    (java.awt.)Panel has no setOpaque(boolean) method.
  • 843807
    843807 Member Posts: 46,582
    morgalr wrote:
    You can try setting the background color of the 2D graphic context associated with that object.
    What do you mean? I paint a panel within paint funcion, and set a color with function setColor(Color c) of Graphics object.
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    BTW - some points about your reply to my first reply.
    1) What you posted is not an SSCCE.
    2) The problem with your ASCII art can be fixed by using the code tags. Please use the code tags for code or code snippets, HTML/XML, input/output or ASCII art. To use the code tags, select the sample and click the CODE button.
    3) Why did you ignore my question about using AWT?
  • 843807
    843807 Member Posts: 46,582
    edited Jun 7, 2010 12:13PM
    1) and 2) - ok I'll keep in mind this.
    3) I didn't ignore your question. I use AWT because I get ready heavyweight component (ready object, Canvas) . I can't even access paint function that component. I'd like to draw something above this Canvas, so I can't use Swing. Here is my original answer:
    "+Despite new JDK can handle mixing heavyweight and lightweight components, it doesn't work for me, I get heavyweight component above swing component even if swing component was added with lower index+."
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    cartoon_20 wrote:
    ..I didn't ignore your question. ..
    My bad. I did not read your reply carefully.
    ..I use AWT because I get ready heavyweight component (ready object, Canvas) . ..
    But this I just do not understand. A JPanel can do everything that a Canvas can do (though it might take a little extra coding). A JLayeredPane should be able to provide the layering you mention.
    ..I can't even access paint function that component. I'd like to draw something above this Canvas, so I can't use Swing. Here is my original answer:
    "+Despite new JDK can handle mixing heavyweight and lightweight components, it doesn't work for me, I get heavyweight component above swing component even if swing component was added with lower index+."
    Whichever way you decide to go, note that posting an SSCCE sure encourages people to help..
  • 843807
    843807 Member Posts: 46,582
    edited Jun 8, 2010 4:37PM
    Unless I am missing what you are saying, and I do that frequently, you are experiencing a flash of default color before you write your new color, if you change the background color of your graphics context to match your target color, then when you clear, you will have the color you desire already painted into the background. All you need do then is add your graphics.

    If that is not what you are trying to do, then explain further or just let me wanter aimlessly in the dark.

    Edited by: morgalr on Jun 8, 2010 8:36 PM ==> the following comment added:

    BTW: often when you get "flicker" it is a sign that you have a bloated paint/paintComponent method.
This discussion has been closed.