Results 1 to 5 of 5
- 10-22-2012, 01:24 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
New to Java, background color help
Hello everyone, i am new to these forums and new to java. Been programming java for a few weeks now and i have a question that someone may be able to help me with. I have a simple program that demonstrates a screen saver. It creates random objects on the screen with random colors and after 20 objects apear on the screen it clears the screen and starts to repaint the objects until it reaches 20 again and so on. Now i have already turned in this assignment but i like to alter my old code as i learn new stuff. My problem is setting the background color. I have my main at the end f my code, my main looks like this,
So when i run my program the background of the window is black but when the 20 items appear and it repaints, it is a white background again. Is this even going to be taken care of in main or is there another portion of my program that needs a background color because of me using repaint? If more information is needed let me know i can post more from my code. ThanksJava Code:public static void main(String[] args) { JFrame window = new JFrame("Screen Saver"); window.setContentPane(new ScreenSaver()); window.setLocationRelativeTo(null); // window opens in center of screen window.pack(); window.setVisible(true); window.setSize(400, 400); window.setBackground(Color.BLACK); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Last edited by Estvan; 10-22-2012 at 02:28 AM.
- 10-22-2012, 02:14 AM #2
Re: New to Java, background color help
I'm not sure how much difference this will make but generally the pack and setVisible methods are the last methods you call when creating GUIs. Any changes you make after that may not be visible.
- 10-22-2012, 02:22 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Re: New to Java, background color help
when i set it before those is still does the same thing. Also when i start the program and enlarge it it resets but the background is still black, when i hit the minimize it also resets it and the background is black, only when it actually waits for all 20 graphics to pop up and reset is when the background turns white again.
- 10-22-2012, 02:25 AM #4
Re: New to Java, background color help
In that case there must be a bug in your code which resets everything after 20 objects.
- 10-22-2012, 04:03 AM #5
Re: New to Java, background color help
1. Your posted code snippet doesn't respect Swing's single threaded rule. Go through Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
2. Setting the background of a top lebvel Swing window is usually pointless. To understand why, go through the documentation for the RootPaneContainer interface, which they implement.
3. You're very possibly not doing your painting the correct way. Have you read Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) ?
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
button background color
By new2java2009 in forum AWT / SwingReplies: 1Last Post: 02-11-2012, 05:26 AM -
How to change background color
By Jeffrey4u in forum Sun Java Wireless ToolkitReplies: 0Last Post: 10-22-2011, 08:32 AM -
Background color of window
By Annie George in forum NetBeansReplies: 1Last Post: 09-18-2010, 09:52 AM -
background color with jpanel
By hannerz06 in forum New To JavaReplies: 6Last Post: 03-31-2010, 03:25 AM -
window background color?
By javan00b in forum New To JavaReplies: 3Last Post: 01-29-2008, 10:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks