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
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
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);
}
}
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.
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
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.
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 Quote:
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