window 在 java 游戏开发中崩溃

window crash in java game development

这是我的代码,它将创建一个 window,它将接管我的全屏,背景颜色为蓝色,并打印 "Hello World"黑色,但是当我 运行 这个应用程序时,我的屏幕冻结并且文本 "Hello World" 出现,5 秒后线程终止,但背景颜色没有改变而且画面变成了这个样子! 谁能指出我做错了什么?

这是我的 Screen class:

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import javax.swing.JFrame;

public class Screen {
    private GraphicsDevice graphic_card;

    public Screen() {
        GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        graphic_card = environment.getDefaultScreenDevice();
    }

    public void set_full_screen(DisplayMode mode, JFrame window) {
        window.setUndecorated(true);
        window.setResizable(false);
        graphic_card.setFullScreenWindow(window);
        if ((mode != null) && (graphic_card.isDisplayChangeSupported())) {
            try {
                graphic_card.setDisplayMode(mode);
            } catch (Exception exception) {
                // No handling of exception
            }
        }
    }

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

    public void restore_screen() {
        Window window = graphic_card.getFullScreenWindow();
        if (window != null) {
            window.dispose();
        }
        graphic_card.setFullScreenWindow(null);
    }
}

这是我的 Mayank class:

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Mayank extends JFrame {
    public static void main(String[] args) {
        DisplayMode display_mode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
        Mayank mayank = new Mayank();
        mayank.run(display_mode);
    }

    public void run(DisplayMode display_mode) {
        setBackground(Color.BLUE);
        setForeground(Color.BLACK);
        setFont(new Font("Arial", Font.PLAIN, 24));
        Screen game_screen = new Screen();
        try {
            game_screen.set_full_screen(display_mode, this);
            try {
                Thread.sleep(5000);
            } catch (Exception exception) {
                // No handling of exception
            }
        } finally {
            game_screen.restore_screen();
        }
    }

    public void paint(Graphics g) {
        g.drawString("Hello World!!", 200, 200);
    }
}

I don't know where you got this code, but it is so wrong.

I created a GUI that displays a blue background for 5 seconds, then changes the background to white.

Here's the GUI.

  1. All Swing applications must start with a call to the SwingUtilities invokeLater method to put the creation and execution of Swing components on the Event Dispatch thread (EDT).

  2. Always draw on a JPanel. Never draw directly on a JFrame.

  3. Your timer blocked the EDT. You must put timing and update events in a separate thread. When you change the GUI from a separate thread, you must execute the invokeLater method to ensure the update of the drawing panel is done on the EDT.

这是代码。

package com.ggl.testing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Manyak implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Manyak());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Hello Word Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DrawingPanel drawingPanel = new DrawingPanel();

        frame.add(drawingPanel);
        frame.pack();
        frame.setVisible(true);

        new Thread(new Delay(drawingPanel)).start();
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = -685508126882892538L;

        private Color color;

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(400, 400));
            this.color = Color.BLUE;
        }

        public void setColor(Color color) {
            this.color = color;
            this.repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());

            g.setColor(Color.BLACK);
            g.setFont(new Font("Arial", Font.PLAIN, 24));
            g.drawString("Hello World!", 120, 200);
        }

    }

    public class Delay implements Runnable {

        private DrawingPanel drawingPanel;

        public Delay(DrawingPanel drawingPanel) {
            super();
            this.drawingPanel = drawingPanel;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {

            }

            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    drawingPanel.setColor(Color.WHITE);
                }

            });

        }

    }
}