gpt4 book ai didi

java - 背景颜色不变

转载 作者:行者123 更新时间:2023-11-29 07:54:53 25 4
gpt4 key购买 nike

所以我正在尝试更改我根据教程制作的全屏游戏的背景,我正在尝试将背景更改为绿色,但它仍然是黑色,代码有什么问题?

屏幕.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);
}
}

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

最佳答案

您的第一个问题是您的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)。 .

例如

enter image description here

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

关于java - 背景颜色不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18526608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com