When I run my bucky.java, the background color is black and not pink. Why?

Here is my bucky.java:-

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

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 3/30/12
 * Time: 3:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class bucky extends JFrame {
    public static void main(String[] args){
        DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
        bucky b = new bucky();
        b.run(dm);
    }

    public void run(DisplayMode dm) {
        setBackground(new Color(234, 123, 66));
        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.restoreWindow();
        }
    }
    public void paint(Graphics g) {
    if (g instanceof Graphics2D) {
    Graphics2D g2D = (Graphics2D) g;
    g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }
    g.drawString("This is gonna be awesome!!!", 200, 200);
}     }

Here is my Screen.java:-

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

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 3/30/12
 * Time: 3:29 PM
 * To change this template use File | Settings | File Templates.
 */
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 e) { }
        }


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

Recommended Answers

All 7 Replies

What component is there that uses the background color?

No component uses it. It is simply a background color. By the way, there is a mistake in my code. That is, the background color is actually Color.PINK.

For testing I added these lines:
` JPanel pnl = new JPanel();
pnl.setBackground(Color.magenta); //<<<<<<< shows as narrow band at top of window
b.add(pnl, BorderLayout.NORTH);

and this line to the paint method:
super.paint(g); //<<<<<<<<< Get gray vs black with this NEED THIS TO show magenta band

Ignore the leading ' on the code line. The new forum code formatting is not user friendly.

I didn't understand. Could you explain again?

The code additions I posted will show the color magenta.

I found a new solution. I drew a Rectangle with the x and y positions equal to 0 and the width and height to the screen resolution. Then I wrote my string on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.