Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Setting panel color in different places produces different effects.

  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Setting panel color in different places produces different effects.

    The only way I have found to change the panel color is to fill it will a color. However, it depends in what part of the program I perform that action. For example, if I do it in the paintComponent member function it changes the panel color. However, if I do it in my main function, it doesn't change the panel color. Is there another way to change the panel color? I tried setting the foreground color and that doesn't work.

    Example 1: Using paintComponent member function to change panel color
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class PaintPanelBackGround extends JPanel
    {
    
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D) g;
    
            g2d.setColor(Color.WHITE);
    
            g2d.fillRect(0,0,800,800);        
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            final PaintPanelBackGround panel = new PaintPanelBackGround();
    
            JFrame frame = new JFrame("CaptureImagePosition");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(800, 800);
    
            Dimension a = new Dimension();
            a.width = 800;
            a.height = 800;
            panel.setPreferredSize(a);
    
            frame.setContentPane(panel);
    
            frame.setVisible(true);
        }
    }

    Example 2: Using main to change panel color

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class PaintPanelBackGround extends JPanel
    {
    
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D) g;
    
            //this.setForeground(Color.red);
    
            //g2d.setColor(Color.WHITE);
    
            //g2d.fillRect(0,0,800,800);        
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            final PaintPanelBackGround panel = new PaintPanelBackGround();
    
            JFrame frame = new JFrame("CaptureImagePosition");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(800, 800);
    
            Dimension a = new Dimension();
            a.width = 800;
            a.height = 800;
            panel.setPreferredSize(a);
    
            frame.setContentPane(panel);
    
            frame.setVisible(true);
    
            Graphics2D g2d = (Graphics2D) frame.getGraphics();
    
            g2d.setColor(Color.WHITE);
    
            g2d.fillRect(0,0,800,800);
        }
    }

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Setting panel color in different places produces different effects.

    Ok, I tried panel.setBackground(Color.white) in main and that changed the panel color to white.

    I had thought I tried that before.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Setting panel color in different places produces different effects.

    1. Never set the background, foreground or any other attribute in a painting method override.
    2. Never use getGraphics() of a Component. Not until (or if ever) you intimately understand the difference between passive and active rendering, and why you don't need the latter.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Setting panel color in different places produces different effects.

    Thanks for the info. I haven't heard of those terms before and I didn't know about active rendering.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Setting panel color in different places produces different effects.

    You don't really need to know about it. Not in these days of efficient JVMs and fast computers. That's why I said
    and why you don't need the latter.
    Go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 05-09-2011, 06:28 AM
  2. Setting BG color of JTabbedPane tab
    By Onra in forum New To Java
    Replies: 2
    Last Post: 10-21-2010, 08:22 PM
  3. I have a problem in setting layout for panel.
    By Vin in forum AWT / Swing
    Replies: 5
    Last Post: 01-19-2010, 10:22 AM
  4. change Panel Color
    By aadi_j in forum AWT / Swing
    Replies: 2
    Last Post: 11-16-2009, 10:12 AM
  5. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •