Results 1 to 5 of 5
  1. #1
    Estvan is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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,
    Java 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);
    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. Thanks
    Last edited by Estvan; 10-22-2012 at 02:28 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  3. #3
    Estvan is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: New to Java, background color help

    In that case there must be a bug in your code which resets everything after 20 objects.

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

    Default 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.

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

Similar Threads

  1. button background color
    By new2java2009 in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2012, 05:26 AM
  2. How to change background color
    By Jeffrey4u in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 10-22-2011, 08:32 AM
  3. Background color of window
    By Annie George in forum NetBeans
    Replies: 1
    Last Post: 09-18-2010, 09:52 AM
  4. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 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
  •