Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Problem with setBackground color

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem with setBackground color

    Practiced with Video training for Game Development (up to 5th course). I typed the code which has to create fullscreen window , set background and foreground color, and finally output message. The problem is with background color, it does not switch to required one (to RED as in code example). Foreground color - displays as it is set within run() - method.

    Would you please suggest where to look for the problem.

    attached main code (class JGD3Screen) and class for screen functions (class Screen)

    [highligh=Java]
    import java.awt.*;
    import javax.swing.JFrame;

    public class JGD3Screen extends JFrame {
    public static void main(String[] args){

    DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    JGD3Screen b = new JGD3Screen();
    b.run(dm);

    }

    public void run(DisplayMode dm){
    setBackground(Color.RED);
    setForeground(Color.WHITE);
    setFont(new Font("Arial", Font.PLAIN, 24));

    Screen s = new Screen();
    try{
    s.setFullScreen(dm, this);
    try{
    Thread.sleep(5000);
    }catch (Exception ex){}
    } finally {
    s.restoreScreen();
    }
    }

    public void paint(Graphics g){
    g.drawString("Test message ", 200, 200);

    }
    }
    [/highlight]

    Class Screen()

    import java.awt.*;
    import javax.swing.JFrame;
     
    public class Screen {
     
        private GraphicsDevice vc;  //interface to video card 
     
        //creating constructor
        public Screen(){
            // env - an object to work with graphics environment
                GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
                vc = env.getDefaultScreenDevice();
            }        
        //Method that changes to full screen view of the screen
        public void setFullScreen(DisplayMode dm, JFrame window){
                window.setUndecorated(true);
                window.setResizable(false);
                vc.setFullScreenWindow(window);   
     
                if (dm != null && vc.isDisplayChangeSupported()){
                    try{
                        vc.setDisplayMode(dm);
                    } catch (Exception ex){}
     
                }
        }
            //method 
        public Window getFullScreenWindow(){
                return vc.getFullScreenWindow();
        }
     
            //method to restoring screen to normal
        public void restoreScreen(){
                Window w = vc.getFullScreenWindow();
                if (w != null){
                    w.dispose();
                }
                vc.setFullScreenWindow(null);
        }
     
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problem with setBackground color

    Hello Artem.N!
    Can you elaborate what's the current and the expected behaviour?
    I tried your code and I got a red full screen with "Test Message" on it.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with setBackground color

    Hello Andreas90! I have got black full screen with "Test message" only. That means, problem either with SW or HW. Do you use NetBeans or something else?

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with setBackground color

    Expected as you have got red screen with message.

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problem with setBackground color

    Quote Originally Posted by Artem.N View Post
    Hello Andreas90! I have got black full screen with "Test message" only. That means, problem either with SW or HW. Do you use NetBeans or something else?
    I tried it in Netbeans.
    EDIT: Works the same on cmd.
    Last edited by andreas90; April 24th, 2012 at 06:18 AM.

  6. #6
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Problem with setBackground color

    Found some info for you on the interwebs, have a look.
    swing - Java - Screen turns black, when setting a JFrame to Fullscreen - Stack Overflow
    background and foreground color is not changing in full screen window (Swing / AWT / SWT / JFace forum at JavaRanch)
    Go through that stuff and you will get an answer and probably a better understanding as well.

  7. The Following User Says Thank You to KucerakJM For This Useful Post:

    Artem.N (April 25th, 2012)

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem with setBackground color

    You're missing a few things. You need to call super's paintComponent() in paintComponent() before you do anything else. That handles painting the background, etc. Then you need to set the color of the Graphics instance before you do any custom drawing, otherwise you'll be stuck with the default color.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Artem.N (April 25th, 2012)

  10. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with setBackground color

    Quote Originally Posted by KevinWorkman View Post
    You're missing a few things. You need to call super's paintComponent() in paintComponent() before you do anything else. That handles painting the background, etc. Then you need to set the color of the Graphics instance before you do any custom drawing, otherwise you'll be stuck with the default color.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Thanks for the hint. I tried what you have written and looked at the tutorial. Unfortunately result is negative: I've got now (white) fulll screen without any text message. Would you please have a look to my code.

    import java.awt.*;
    import javax.swing.JFrame;
     
    public class Main extends JFrame {
        public static void main(String[] args){
     
            DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
            Main b = new Main();
            b.run(dm);
     
        }
     
        public void run(DisplayMode dm){
     
        	//setBackground(Color.GREEN);
            //setForeground(Color.BLUE);
     
            setFont(new Font("Arial", Font.PLAIN, 24));
     
            Screen s = new Screen();
            try{
                s.setFullScreen(dm, this);
                try{
                    Thread.sleep(5000);
                }catch (Exception ex){} 
            } finally {
                s.restoreScreen();
              }
        }
     
    //	public void paint(Graphics g) {
    //		g.drawString("Test message ", 200, 200);
    //
    //	}
     
        public void paintComponent(Graphics g) {   
        	super.paintComponents(g); // never forget this line  
        	super.setBackground(Color.GREEN);
        	super.setForeground(Color.BLUE);
     
        	g.drawString("This is FullScreen", 200, 200);
     
        }  
     
    }

  11. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem with setBackground color

    You should extend JPanel instead of JFrame, like in the tutorials. JFrame doesn't have a paintComponent() method, which is what you want, because paint() handles things like double buffering, painting children, etc. I would recommend getting the tutorial I linked you working before you try to add this stuff to it.

    You've extended paintComponent, which is good (with the above in mind), but then you call super.paintComponentS(), which is not good.

    Also, never set the background inside your paint method. That will trigger a repaint, which will call paintComponent, which will set the background, which will trigger a repaint, which will... Chances are you'll end up with an infinite loop, but at the very least you'll end up with buggy code.

    Also also, I don't think setForeground() is what you think it's doing. Call g.setColor(Color) instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JTable Row Color
    By ellias2007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 23rd, 2012, 01:29 PM
  2. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  3. Trying to output color....HELP PLEASE!
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2011, 10:52 PM
  4. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 03:59 PM
  5. Color Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 03:53 PM