Why won't my background color display in JFrame?

Ethan Pieper picture Ethan Pieper · Jun 29, 2012 · Viewed 12.2k times · Source

I have two class files:

Screen https://gist.github.com/3020101

JMain https://gist.github.com/3020107

I'm trying to get it to go fullscreen for 5 seconds and display the background (or, at this point, even the foreground) but when I run it it goes fullscreen for 5 seconds, yay, but it is just a blank light grey screen.

What am I doing wrong? Eventually I'm going to use an image for the background and I want to make sure I'm not screwing up somewhere.

Thanks guys!

Edit: When I add this at the end of my JMain class the font color is the same as the foreground color, but the background is always black no matter what color I change it to in the program.

public void paint(Graphics g) {
    g.drawString("This is gonna be awesome", 200, 200);
}

code from github

import java.awt.*;
import javax.swing.JFrame;

public class JMain extends JFrame {

    private JFrame frame = new JFrame();

    public static void main(String[] args) {
        DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
        JMain m = new JMain();
        m.run(dm);
    }

    public void run(DisplayMode dm) {
        this.getContentPane().setBackground(Color.RED);
        frame.setForeground(Color.BLACK);
        frame.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();
        }
    }
}

and

import java.awt.*;
import javax.swing.JFrame;

public class Screen {

    private GraphicsDevice vc;

    public Screen() {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        vc = env.getDefaultScreenDevice();
    }

    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) {
            }
        }
    }

    public Window getFullScreenWindow() {
        return vc.getFullScreenWindow();
    }

    public void restoreScreen() {
        Window w = vc.getFullScreenWindow();
        if (w != null) {
            w.dispose();
        }
        vc.setFullScreenWindow(null);
    }
}

Answer

mKorbel picture mKorbel · Jun 29, 2012
  1. Don't extend a JFrame, but instead create a local JFrame variable and use it.

  2. You can't paint the JFrame's background Color, but you can do this for the JFrame's contentPane (usually a JPanel). An example of code that does this follows:

    this.getContentPane().setBackground(Color.RED);

  3. Never use Thread.sleep(int) in code called on the Swing event thread, as this will completely block this thread, preventing it from performing its necessary actions of painting the GUI and interacting with the user and effectively freezing the application for as long as the thread is sleeping.

  4. Use a Swing Timer in place of Thread.sleep(...)