2

I am making a Game in Java, currently the set background doesn't work, but the setforeground works. Here is my code for the main class:

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

public class Sam extends JFrame{

public static void main(String[] args) {

    DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
    Sam s = new Sam();
    s.run(dm);
}
public void run(DisplayMode dm){
    this.setBackground(Color.PINK);
    this.setForeground(Color.BLACK);
    setFont(new Font("Arial", Font.PLAIN, 24));

    Screen s = new Screen();
    try{
        s.setFullScreen(dm, this);
        try{
            Thread.sleep(5000);
        }catch(Exception ex){ex.printStackTrace();}
    }finally{
        s.restoreScreen();
    }
}
public void paint(Graphics g){
    g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
}

}

here is the code for my Screen class:

import java.awt.*;
import javax.swing.*;
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){ex.printStackTrace();}
    }
}
public Window getFullScreenWindow(){
    return vc.getFullScreenWindow();
}
public void restoreScreen(){
    Window w = vc.getFullScreenWindow();
    if(w != null){
        w.dispose();
    }
    vc.setFullScreenWindow(null);
}
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Sammy D PoiseEn
  • 81
  • 1
  • 2
  • 7

1 Answers1

2

JFrame is a composite component, it's actually made up of a number of other components

enter image description here

(From How to Use Root Panes)

So, when you call setBackground, you're only changing the background color of the underlying frame, not the contentPane which resides on it.

Instead, you should be using getContent().setBackground(...).

"But why does setForegound work" I here you ask?

That's because you've overriding paint of JFrame, which is pre-configured with the foreground property of the frame (and not the contentPane)

Now, having said all that...

  1. You should avoid extending from top level containers like JFrame, for these types of reasons AND...
  2. You shouldn't override paint of them either. You should start with a component, extending from something like JPanel and override it's paintComponent method.

Have a look at:

for things that go wrong when you override paint of top level containers, and also, they aren't double buffered, so updating them causes flickering

Example...

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
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.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Sam extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
                Sam s = new Sam();
                s.run(dm);
            }
        });
    }

    public Sam() {
        add(new MainView());
    }

    public void run(DisplayMode dm) {

        Screen s = new Screen();
        s.setFullScreen(dm, this);
        Timer timer = new Timer(5000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                s.restoreScreen();
            }
        });
        timer.start();
    }

    public class MainView extends JPanel {

        public MainView() {
            this.setBackground(Color.PINK);
            this.setForeground(Color.BLACK);
            setFont(new Font("Arial", Font.PLAIN, 24));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            String text = "THIS IS GUNNA BE AWESOME";
            FontMetrics fm = g.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g.drawString(text, x, y);
        }

    }

    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) {
                    ex.printStackTrace();
                }
            }
        }

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

        public void restoreScreen() {
            Window w = vc.getFullScreenWindow();
            if (w != null) {
                w.dispose();
            }
            vc.setFullScreenWindow(null);
        }
    }
}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366