0

这是一个全屏显示五秒钟然后恢复正常的程序。我想要做的是将背景颜色更改为黄色。我试过 getContentPane().setBackground(Color.YELLOW) 但它仍然不起作用。

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

}
}

和二等

   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.
   }
}
4

1 回答 1

1

基本上,您正在阻塞事件调度线程,阻止处理任何新的传入事件,包括绘制请求。相反,您应该使用类似 a 的东西javax.swing.Timer在后台等待,直到所需的时间过去,然后恢复屏幕。

javax.swing.Timer在 Event Dispatching Thread 的上下文中指定延迟后提供通知,以便在您需要修改 UI 时安全使用。

仔细查看Swing 中的并发以获取更多详细信息...

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.
        }

    }

}

您还应该看看Performing Custom Painting,因为您的绘画方法可能会阻止它实际绘制背景

于 2013-10-19T06:56:21.613 回答