我正在研究 Buckysroom.org 上提供的 Java 游戏开发教程。我正在创建一个 Screen 类,它允许我全屏显示。然后,当我尝试用文本绘制它时,屏幕不会出现,并且只有文本显示在我的工作窗口(Eclipse)上如果我注释掉paint方法或将其设为paintComponent,则会出现红色屏幕,但没有任何内容文本。谁能帮我解决这个问题?
屏幕类:
import java.awt.*;
import javax.swing.JFrame;
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) {
}
}
}
public Window getFullScreenWindow() {
return vc.getFullScreenWindow();
}
public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
这是我称之为的类:
import java.awt.*;
import javax.swing.JFrame;
public class bucky extends JFrame {
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
bucky m = new bucky();
m.run(dm);
}
public void run(DisplayMode dm) {
this.getContentPane().setBackground(Color.RED);
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) {
}
} finally {
s.restoreScreen();
}
}
public void paint(Graphics g) {
g.drawString("This is awesome", 200, 200);
}
}
最佳答案
我怀疑 Thread.sleep
会导致事件调度线程进入休眠状态,从而阻止它处理任何绘制事件(或与此相关的任何其他类型的事件)和/或者 JFrame
的 paint
方法不是执行绘制的合适方法(通常出于多种原因)...
相反,您应该考虑使用像 javax.swing.Timer
这样的东西,您可以用它来设置 future 某个时间的闹钟,它会给您回电。
您还应该考虑使用诸如 JPanel
之类的东西来执行自定义绘画,例如......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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 FullScreenTest {
public static void main(String[] args) {
new FullScreenTest();
}
public FullScreenTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
Screen.setFullScreen(null, frame);
Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Screen.restoreScreen();
}
});
timer.start();
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setFont(new Font("Arial", Font.PLAIN, 24));
setBackground(Color.RED);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g.drawString("This is awesome", 200, 200);
g2d.dispose();
}
}
public static class Screen {
public static GraphicsDevice getGraphicsDevice() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
return env.getDefaultScreenDevice();
}
public static void setFullScreen(DisplayMode dm, JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
GraphicsDevice gd = getGraphicsDevice();
gd.setFullScreenWindow(window);
if (dm != null && gd.isDisplayChangeSupported()) {
try {
gd.setDisplayMode(dm);
} catch (Exception ex) {
}
}
}
public static Window getFullScreenWindow() {
GraphicsDevice gd = getGraphicsDevice();
return gd.getFullScreenWindow();
}
public static void restoreScreen() {
GraphicsDevice gd = getGraphicsDevice();
Window w = gd.getFullScreenWindow();
if (w != null) {
w.dispose();
}
gd.setFullScreenWindow(null);
}
}
}
看一下:
了解更多详情...
此外,由于它所执行的工作类型,Screen
也可以是一个完全静态
类。它不维护上下文信息,只是提供实用功能 - 恕我直言。
如果每个 GraphicsDevice
都需要一个,那将是另一回事了;)
关于java - 未创建用于显示拉绳文本的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24303015/