Results 1 to 5 of 5
- 04-17-2012, 05:42 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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); } }
- 04-17-2012, 05:54 AM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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.
- 04-17-2012, 06:16 AM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-17-2012, 07:38 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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.
- 04-17-2012, 09:49 PM #5
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
Go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)and why you don't need the latter.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Dynamically change the font and color message to be displayed on a panel.
By jeskoston in forum New To JavaReplies: 5Last Post: 05-09-2011, 06:28 AM -
Setting BG color of JTabbedPane tab
By Onra in forum New To JavaReplies: 2Last Post: 10-21-2010, 08:22 PM -
I have a problem in setting layout for panel.
By Vin in forum AWT / SwingReplies: 5Last Post: 01-19-2010, 10:22 AM -
change Panel Color
By aadi_j in forum AWT / SwingReplies: 2Last Post: 11-16-2009, 10:12 AM -
setting background color of JFrame form with NetBeans 6.1
By onefootswill in forum New To JavaReplies: 4Last Post: 08-12-2008, 07:02 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks