2

我正在用 Java 制作游戏,目前设置的背景不起作用,但设置的前景有效。这是我的主类代码:

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

}

这是我的 Screen 类的代码:

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);
}
}
4

1 回答 1

2

JFrame是一个复合组件,它实际上是由许多其他组件组成的

在此处输入图像描述

(来自如何使用根窗格

因此,当您调用 时setBackground,您只是更改了基础框架的背景颜色,而不是contentPane驻留在其上的背景颜色。

相反,您应该使用getContent().setBackground(...).

“但为什么setForegound有效”我在这里你问?

那是因为您已经覆盖paintJFrame,它预先配置foreground了框架的属性(而不是contentPane

现在,说了这么多……

  1. JFrame由于这些类型的原因,您应该避免从顶级容器扩展,并且......
  2. 你也不应该覆盖paint它们。您应该从一个组件开始,从类似的东西扩展JPanel并覆盖它的paintComponent方法。

看一下:

paint对于覆盖顶级容器时出错的事情,而且它们不是双缓冲的,因此更新它们会导致闪烁

例子...

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);
        }
    }
}
于 2016-01-21T00:15:32.183 回答