-1

I am trying to set background color of my screen to green.

My code so far:

package game;

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


public class Game extends JFrame {

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

    }

    public void run(DisplayMode dm) {
        setBackground(Color.GREEN);
        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 E) {
            }
        } finally {
            s.restoreScreen();
        }
    }

    @Override
    public void paint(Graphics g){
        g.drawString("Check Screen", 200, 200);
    }
}

When I run the program, I get this:

printScreen

Screen should be green according to line:

setBackground(Color.GREEN);

Why does the background is not being set to green when I run the program?

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
asmundur
  • 707
  • 3
  • 7
  • 11
  • 1
    Have you tried `this.getContentPane ().setBackground (Color.GREEN)`? – Laf Oct 11 '13 at 15:38
  • The way you word it, you seem to be wanting to set the SCREEN to green, but you're setting the JFrame to green in the code….? – user2277872 Oct 11 '13 at 15:39
  • 1
    See [answer for the same question already asked][1] [1]: http://stackoverflow.com/questions/2742270/jframe-setbackground-not-working-why – Samhain Oct 11 '13 at 15:39
  • I have tried `this.getContentPane ().setBackground (Color.GREEN)` but screen still is in black. – asmundur Oct 11 '13 at 15:43
  • And what happens if you comment out your definition of `paint ()`? Joined with the `this.getContentPane ().setBackground (Color.GREEN)` line, does it make your frame green? – Laf Oct 11 '13 at 15:46
  • Yes, makes my frame green. – asmundur Oct 11 '13 at 15:50
  • possible duplicate of [Setting background color for the JFrame](http://stackoverflow.com/questions/1081486/setting-background-color-for-the-jframe) – demongolem Feb 18 '14 at 13:31

2 Answers2

1

You need to add a call to super.paint (g); in your paint () method.

@Override
public void paint(Graphics g){
    super.paint (g);
    g.drawString("Check Screen", 200, 200);
}

This will ensure that the component will draw itself correctly, including the background color, then draw your text.

Laf
  • 7,965
  • 4
  • 37
  • 52
1

Generally, whole approach is very bad. Even if it works with getContentPane().setBackground(Color.GREEN) it won't probably work because you are calling a Thread.sleep(5000) on EDT (or you will have issues soon or later). Use proper component for repetitive task's (refreshing of your screen): Swing Timer.

Instead of overriding JFrame's paint method, it's far better to use JPanel and to override it's paintComponent method. So, something like this:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Game extends JFrame {

    JFrame frame = new JFrame();

    public Game() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Panel());
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Game();
            }
        });
    }

    class Panel extends JPanel {
        Timer timer;

        Panel() {
            setBackground(Color.BLACK);
            setForeground(Color.WHITE);
            refreshScreen();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setFont(new Font("arial", Font.PLAIN, 24));
            g.drawString("Check Screen", 200, 200);
        }

        public void refreshScreen() {
            timer = new Timer(0, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    repaint();
                }
            });
            timer.setRepeats(true);
            //Aprox. 60 FPS
            timer.setDelay(17);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(650, 480);
        }
    }

}
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85