所以我试图改变我在教程中制作的全屏游戏的背景,我正在尝试将背景更改为绿色,但它保持黑色,代码有什么问题?
Screen.java
package debels;
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 e){}
}
}
public Window getFullScreen(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if (w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
Main.java
package debels;
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame{
private static final long serialVersionUID = -7498470134154894036L;
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
Main m = new Main();
m.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.GREEN);
setForeground(Color.BLUE);
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.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("Hello", 200, 200);
}
}
答案 0 :(得分:5)
您的第一个问题将是您的paint
方法...
public void paint(Graphics g){
g.drawString("Hello", 200, 200);
}
paint
方法的部分责任是绘制......背景。但是,既然你没有打电话给super.paint
,那就不会发生这种情况。
您应该避免在paint
等顶级容器上覆盖JFrame
,而是使用自定义组件,例如JPanel
,并覆盖其paintComponent
方法(不是忘了给super.paintComponent
打电话。
潜在问题的另一个方面是使用Thread.sleep
,这可能导致事件调度线程停止,从而阻止处理新的绘制请求。一个更好的解决方案可能是使用javax.swing.Timer
,它会在后台停顿并在EDT的上下文中提供通知(通过ActionListener
)...
例如
import java.awt.BorderLayout;
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.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 TestFullScreen {
public static void main(String[] args) {
new TestFullScreen();
}
public TestFullScreen() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
final Screen s = new Screen();
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
s.setFullScreen(dm, frame);
Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
s.restoreScreen();
}
});
timer.start();
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBackground(Color.GREEN);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
FontMetrics fm = g2d.getFontMetrics();
String text = "It's not easy to be green";
int x = (width - fm.stringWidth(text)) / 2;
int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
System.out.println(width + "x" + height);
System.out.println(x + "x" + y);
g2d.drawString(text, x, y);
g2d.dispose();
}
}
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 getFullScreen() {
return vc.getFullScreenWindow();
}
public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
}
答案 1 :(得分:0)
这就是我为克服这一挑战所做的工作。我从Main类的run方法中删除了setBackground,setForeground和setFont,并将它们添加到Screen类的setFullScreen方法中:
window.getContentPane().setBackground(Color.PINK);
window.setForeground(Color.BLUE);
window.setFont(new Font ("Arial", Font.PLAIN, 24));
此后,我添加了#34; super.paint(g);"作为Main类
中paint方法的第一行答案 2 :(得分:0)
您只需要进行3个小更改,它就会起作用。请按照下列步骤操作:
步骤1:将getContentPane()添加到run()方法中存在的setBackgroud()中
getContentPane().setBackground(Color.GREEN);
第2步:将以下行从run()方法剪切并粘贴到paint()方法。
getContentPane().setBackground(Color.GREEN);
setForeground(Color.BLUE);
setFont(new Font("Arial", Font.PLAIN, 24));
步骤3:在paint()方法的开头添加super.paint(g)。整个paint()方法将如下所示;
public void paint(Graphics g){
super.paint(g);
getContentPane().setBackground(Color.GREEN);
setForeground(Color.BLUE);
setFont(new Font("Arial", Font.PLAIN, 24));
g.drawString("Hello", 200, 200);
}
如果仍然无法正常工作,请在评论中告诉我! #YNWA