Why won't my JFrame program change background color?

pur3extrme

This is a program that goes full screen for five seconds then go back to normal. What i am trying to do is change the back ground color to yellow. I tried getContentPane().setBackground(Color.YELLOW) but it still does not work.

  package gamedev;

   import java.awt.Color;
   import java.awt.DisplayMode;
   import java.awt.Font;
   import java.awt.Graphics;      
   import java.io.PrintWriter;
   import javax.swing.JFrame;

  public class GameDev extends JFrame {

   public static void main(String[] arg) {

    DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    GameDev b = new GameDev();
    b.getContentPane().setBackground(Color.yellow);
    b.run(dm);

}

public void run(DisplayMode dm) {
    setBackground(Color.YELLOW);
    setForeground(Color.red);
    setFont(new Font("Arial", Font.PLAIN, 25));

    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("You know you love me home boy.", 200, 200);

}
}

and second class

   package gamedev;

   import java.awt.DisplayMode;
   import java.awt.GraphicsDevice;
   import java.awt.GraphicsEnvironment;
   import java.awt.Window;
   import javax.swing.JFrame;

   public class Screen {

    GraphicsDevice videoCard;

    public Screen() {

       GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      videoCard = env.getDefaultScreenDevice(); // acces computer screen

   }

    public void setFullScreen(DisplayMode dm, JFrame window) { 
    window.setUndecorated(true);
    window.setResizable(false);
    videoCard.setFullScreenWindow(window);

    if (dm != null && videoCard.isDisplayChangeSupported()) {

        try {

            videoCard.setDisplayMode(dm);

        } catch (Exception ex) {

        }
      }
   }

  public Window getFullScreenWindow() {

    return videoCard.getFullScreenWindow(); //returns ur window

  }

public void restoreScreen() {

    Window w = videoCard.getFullScreenWindow();

    if (w != null) {

        w.dispose();
    }
    videoCard.setFullScreenWindow(null); // takes away from full screen.
   }
}
MadProgrammer

Basically, you are blocking the Event Dispatching Thread, preventing from processing any new incoming events, including paint requests. Instead, you should use something like a javax.swing.Timer to wait in the background until the required time has elapsed, then restore the screen.

The javax.swing.Timer will provide notification after the specified delay within the context of the Event Dispatching Thread, making it safe to use when you need to make modifications to the UI.

Take a closer look at Concurrency in Swing for more details...

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;

public class GameDev extends JFrame {

    public static void main(String[] arg) {

        DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
        GameDev b = new GameDev();
        b.getContentPane().setBackground(Color.yellow);
        b.run(dm);

    }

    public void run(DisplayMode dm) {
        setBackground(Color.YELLOW);
        setForeground(Color.red);
        setFont(new Font("Arial", Font.PLAIN, 25));

        final Screen s = new Screen();
        s.setFullScreen(dm, this);

        Timer timer = new Timer(5000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                s.restoreScreen();
            }
        });
        timer.setRepeats(false);
        timer.start();
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("You know you love me home boy.", 200, 200);

    }

    public class Screen {

        GraphicsDevice videoCard;

        public Screen() {

            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            videoCard = env.getDefaultScreenDevice(); // acces computer screen

        }

        public void setFullScreen(DisplayMode dm, JFrame window) {
            window.setUndecorated(true);
            window.setResizable(false);
            videoCard.setFullScreenWindow(window);

            if (dm != null && videoCard.isDisplayChangeSupported()) {

                try {

                    videoCard.setDisplayMode(dm);

                } catch (Exception ex) {

                }
            }
        }

        public Window getFullScreenWindow() {

            return videoCard.getFullScreenWindow(); //returns ur window

        }

        public void restoreScreen() {

            Window w = videoCard.getFullScreenWindow();

            if (w != null) {

                w.dispose();
            }
            videoCard.setFullScreenWindow(null); // takes away from full screen.
        }

    }

}

You should also take a look at Performing Custom Painting, as your paint method may be preventing it from actually painting the background

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Title color of UIButton won't change on highlighted/selected but background color will

From Dev

Background color won't change in panel

From Dev

Why doesn't SVG background color change?

From Dev

Why Won't the Screen Change Color

From Dev

JFrame background color will not change

From Dev

My image won't show up but the background color shows

From Dev

willDisplayCell won't change background color consistent with logic

From Dev

why won't my jframe appear?

From Dev

Why won't my background image show?

From Dev

My div background color won't show up

From Dev

Why won't my program work in python?

From Dev

How to change background color at JFrame

From Dev

Jumbotron color and background won't change

From Dev

Body background-color won't change while scrolling

From Dev

Why can't the menu background color change?

From Dev

Why won't the background color apply?

From Dev

Why won't my buttons appear when I run this JFrame?

From Dev

How change background color of JFrame

From Dev

SKView/SKScene won't change background color

From Dev

why won't my jframe appear?

From Dev

Why won't my "Circle" program run?

From Dev

Why won't my program work in python?

From Dev

Why won't my input Scanner and the rest of my program work?

From Dev

Why won't my background Nav bar change colour?

From Dev

Jumbotron color and background won't change

From Dev

Body background-color won't change while scrolling

From Dev

setOpaque is true, but JPanel won't change the background color

From Dev

Program with JFrame won't start

From Dev

Background color won't change javascript