我正在用 Java 开发一个文本 RPG 引擎。如果您无法通过我的问题来判断,那么我在这方面是个菜鸟。无论如何,我希望文本出现在背景上,讲述一个故事,但是,每当我实现字符串时,我只能看到白色背景上的字符串,而不是我最初编程的蓝色背景。这是我用于文本和背景的绘制方法:
public GamePanel(){
setPreferredSize(gameDim);
setBackground(Color.BLUE);
setFocusable(true);
requestFocus(true);
}
public void paint(Graphics g){
paintComponent(dbg);
}
public void paintComponent(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
g.drawImage(dbImage, 0, 0, this);
g.drawString("Hello World", 250, 250);
}
这是整个 GamePanel 类,以防万一您需要它:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class GamePanel extends JPanel{
//Double buffering
private Image dbImage;
private Graphics dbg;
//JPanel variables
static final int GWIDTH = 500, GHEIGHT = 500;
static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
//Game variables
private Thread game;
private volatile boolean running = false;
public GamePanel(){
setPreferredSize(gameDim);
setBackground(Color.BLUE);
setFocusable(true);
requestFocus(true);
}
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g) {
g.drawString("Hello World", 250, 250);
}
private void log(String s){
System.out.println(s);
}
}
感谢您的时间。