图像不会画在屏幕上,但不会出现错误

问题描述:

我已经开始创建一个简单的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); 
} 
} 

Player类:

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类给玩家一个起点:

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

} 

我有重新绘制游戏循环类:

public class GameLoop implements Runnable { 

GameWindow gWindow; 

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

} 

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

此代码不能编译,我...什么是'GlobalPosition'? – joshreesjones

+2

从小开始:测试一个非常小的程序,它除了加载图像,将其放入ImageIcon并将ImageIcon显示在JOptionPane中之外什么也不做。首先解决这个问题,并且只有**然后**才能在更大的应用程序中使用它。此外,最好通过getClass()。getResource(“....”)作为URL读取图像,而不是作为文件。 –

+0

我的猜测是你需要'repaint()',但由于没有找到GlobalPosition,我无法编译。 – joshreesjones

意见建议:

  • 从小开始:测试一个非常小的程序,它除了加载图像,将其放入ImageIcon中并将ImageIcon显示在JOptionPane中。首先解决,并且只有然后在更大的应用程序中使用它。
  • 你最好是通过getClass().getResource("....")作为URL读取图像,而不是作为文件。
  • 不要在顶层窗口调用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(); 
     } 

    } 
} 

事实证明,它是

setVisible(true) 

呼叫所导致的问题。我把它放在方法的所有其他调用之下,屏幕和图像出现。谢谢您的帮助。

编辑:

@Sage这个位置,这样的位置是正确的,但我将开始从现在开始使用

getClass().getResource(..) 

方法。

enter image description here