图像不会绘制到屏幕上,但不会发生错误

时间:2013-11-28 18:05:45

标签: java image swing awt paint

我已经开始创建一个简单的Java游戏,目前我已经使用屏幕和基本的Player类创建了游戏窗口。然而,尽管程序没有给我任何错误,但播放器图像不会绘制到屏幕上,所以我不知道从哪里开始调试问题也许有人可以帮我解决?

以下是课程:

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class GameWindow extends JFrame {

public static int windowWidth = 600;
public static int windowHeight = 600;

public static void main(String[] args) {
    new GameWindow();
}

public GameWindow() {
    this.setSize(windowWidth, windowHeight);
    this.setTitle("Berzerk Clone");
    this.setVisible(true);
    // Defaults the window to be set in the middle of the screen
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GameDrawing drawGame = new GameDrawing();
    this.add(drawGame, BorderLayout.CENTER);
}

}

绘图类:

import java.awt.*;
import javax.swing.*;


@SuppressWarnings("serial")
public class GameDrawing extends JComponent {

PlayerHuman p;

public GameDrawing() {      
    p = new PlayerHuman(300, 300);
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D graphics = (Graphics2D)g;

    graphics.setColor(Color.BLACK);
    graphics.fillRect(0, 0, GameWindow.windowWidth,GameWindow.windowHeight);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    graphics.drawImage(p.getPlayerImage(), 300, 300, null);
}
}

玩家类:

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

public class PlayerHuman extends GlobalPosition {

BufferedImage basicPlayer;  

public PlayerHuman(int x, int y) {
    super(x, y);
    try {
        basicPlayer = ImageIO.read(new File("Images/Player.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void draw(Graphics2D g2d) {
    g2d.drawImage(getPlayerImage(), x, y, null);
}

public BufferedImage getPlayerImage() {
    return basicPlayer;
}

}

感谢所有帮助。

修改

我的道歉

GlobalPosition Class为玩家提供一个起点:

public class GlobalPosition {

public int x;
public int y;

public GlobalPosition() {
    x = y = 0;
}

public GlobalPosition(int _x, int _y) {
    x = _x;
    y = _y;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public void setX(int newX) {
    x = newX;
}

public void setY(int newY) {
    y = newY;
}

}

我有一个重新绘制的Game Loop类:

public class GameLoop implements Runnable {

GameWindow gWindow;

public GameLoop(GameWindow newGWindow) {
    this.gWindow = newGWindow;

}

@Override
public void run() {
    gWindow.repaint();
}
}

2 个答案:

答案 0 :(得分:2)

建议:

  • 从小开始:测试一个非常小的程序,除了加载图像之外什么都不做,将它放入ImageIcon并在JOptionPane中显示ImageIcon。首先解决这个问题,然后只有然后才能在更大的应用程序中使用它。
  • 您最好通过getClass().getResource("....")作为网址阅读图片而不是文件。
  • 不要在顶级窗口中调用setVisible(true),直到 添加所有组件后才能调用
  • 覆盖JComponent的paintComponent(Graphics g)方法,而不是其绘制方法。

如,

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ShowImage {

   // ******* add the path to the image resource below *****
   private static final String IMAGE_RESOURCE_PATH = "";

   public static void main(String[] args) {
      URL imgUrl = ShowImage.class.getResource(IMAGE_RESOURCE_PATH);
      BufferedImage img;
      try {
         img = ImageIO.read(imgUrl);
         if (img == null) {

            String message = "Image cannot be found at \""
                  + IMAGE_RESOURCE_PATH + "\"";
            JOptionPane.showMessageDialog(null, message);
            System.exit(-1);

         }
         Icon icon = new ImageIcon(img);
         JOptionPane.showMessageDialog(null, icon);
      } catch (IOException e) {
         e.printStackTrace();
      }

   }
}

答案 1 :(得分:0)

原来这是

setVisible(true)

调用导致问题的原因。我把它推到方法中的所有其他调用之下,屏幕和图像出现了。谢谢你的帮助。

修改

@Sage这是位置,所以位置正确,但我会开始使用

getClass().getResource(..) 

从现在开始的方法。

enter image description here