我有一个小而简单的程序我已经开始并且有一个奇怪的错误我无法弄清楚或找到在线原因。
我现在所做的所有程序都是构建一个JFrame,构建一个JPanel,并从一个线程将图像绘制到JPanel上。问题是,除非我在框架中单击,左键单击,右键单击,或者甚至用鼠标滚轮单击以显示图像,否则图像不会显示,但调整框架大小不会使图像显示。我已经在面板上尝试了validate()并对两者进行了框架处理,并且我已经尝试将repaint()放入线程调用的代码中,但是当repaint()在代码中时,图像会闪烁。
我在实际绘制图像的代码中放置了一个print语句,但是直到我在框架中单击才会显示该语句。我已经使用了调试器,但代码似乎正在被正确调用。如果我在主线程的update()函数中调用代码的任何地方放置一个print语句,图像就会正确显示而不必点击。
这是我的代码,任何建议都将不胜感激:
框架
public class GameFrame extends JFrame{
int w = 1300;
int h = 740;
public GameFrame() {
this.setSize(w, h);
setTitle("Frame");
GamePanel panel = new GamePanel(w, h);
this.add(panel);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GameFrame gameFrame = new GameFrame();
}
}
小组
public class GamePanel extends JPanel implements Runnable{
//core classes
GameRenderer renderer;
GameManager manager;
GameLoader loader;
//graphics stuff
private Graphics dbg;
private Image dbImage = null;
private int panelWidth;
private int panelHeight;
//game updating stuff
Thread animator;
boolean running;
public GamePanel(int w, int h) {
this.setSize(w, h);
this.setVisible(true);
this.setFocusable(true);
this.panelWidth = w;
this.panelHeight = h;
renderer = new GameRenderer();
manager = new GameManager();
loader = new GameLoader();
startGame();
}
private void startGame() {
animator = new Thread(this, "Animator");
animator.start();
running = true;
}
public void run() {
while(running) {
gameAction();
}
}
//everything that needs to be updated continuously
private void gameAction() {
//updateGame(); // Need code here to update everything
gameRender(); // Draw to the double buffer.
paintScreen(); // Draw double buffer to screen.
}
/**
* Draws the game image to the buffer.
*
*/
private void gameRender() {
if(dbImage == null) {
dbImage = createImage(this.panelWidth, this.panelHeight);
return;
}
dbg = dbImage.getGraphics();
renderer.draw((Graphics2D) dbg, panelWidth, panelHeight);
}
/**
* Draws the game image to the screen by drawing the buffer.
*/
private void paintScreen() {
Graphics g;
try {
g = this.getGraphics();
if ((g != null) && (dbImage != null)) {
g.drawImage(dbImage, 0, 0, null);
g.dispose();
}
} catch (Exception e) { System.out.println("Graphics context error: " + e); }
}
}
图像渲染器
public class GameRenderer {
ImageManipulator im = new ImageManipulator();
/**
* Actually draw everything that needs to be drawn
* Layering also happens here
*/
public void draw(Graphics2D g,int screenWidth, int screenHeight) {
BufferedImage tileImg = im.loadImage("Images/tile.png");
Tile tile = new Tile(tileImg, 30, 30);
tile.draw(g);
}
}
带图像的课程我试图绘制
public class Tile {
BufferedImage tileImg;
//x and y values based on tile grid - z based on height up
int tileX;
int tileY;
int tileZ;
//actual coordinate of top left corner of tile *image*
int pixelX;
int pixelY;
public Tile(BufferedImage img, int pixelX, int pixelY) {
this.pixelX = pixelX;
this.pixelY = pixelY;
this.tileImg = img;
}
public void draw(Graphics g) {
g.drawImage(tileImg, pixelX, pixelY, null);
}
}
同样,任何建议或想法都会有所帮助。我对可能导致这种情况的想法不以为然。 此外,是否有人知道为什么这可能与包含的印刷声明有关但不是没有?
答案 0 :(得分:3)
您误解了如何在Swing组件中进行自定义绘制。拨打this.getGraphics()
是不正确的。绘制Swing组件的正确方法是覆盖其paintComponent
方法。请注意,该方法的第一行必须调用super.paintComponent
:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (dbImage != null) {
g.drawImage(dbImage, 0, 0, this);
}
}
另请注意,您不得尝试处置Graphics对象,因为您没有创建它并且它受Swing的控制,而不是您的。
有关详细信息,请参阅Painting in AWT and Swing教程。
当您想要重新绘制游戏时,只需调用面板的repaint方法即可。因此,您的gameAction
方法应该用repaint()替换paintScreen(),它会自动调用面板的paintComponent方法(以及其他一些方法;正如该教程所解释的那样,绘制并不是一件简单的事情。)
您需要在run()
方法的循环中添加一个简短的sleep调用。如果您尝试不断更新和重新绘制游戏,程序将没有时间实际绘制任何内容。例如,如果您希望游戏每秒更新五次(即每200毫秒):
try {
while (running) {
gameAction();
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
我有两个猜测。第一个是GamePanel中的startGame方法。把行“running = true;”在你的线程上调用“start()”之前。
如果这不起作用,请在GamePanel中的run()方法中尝试:
public void run() { gameAction(); }
如果它呈现,则问题是while循环。