gpt4 book ai didi

java - 图像不会在屏幕上绘制,但不会发生错误

转载 作者:行者123 更新时间:2023-12-02 06:28:41 25 4
gpt4 key购买 nike

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

以下是类(class):

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 类为玩家提供起点:

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

最佳答案

建议:

  • 从小处开始:测试一个非常小的程序,该程序除了加载图像、将其放入 ImageIcon 并在 JOptionPane 中显示 ImageIcon 之外什么也不做。首先解决这个问题,然后才然后在更大的应用程序中使用它。
  • 您最好通过 getClass().getResource("....") 以 URL 形式读取图像,而不是以文件形式读取图像。
  • 在添加所有组件之后之前,不要在顶级窗口上调用setVisible(true)
  • 重写 JComponent 的 paintComponent(Graphics g) 方法,而不是其 Paint 方法。
<小时/>

例如,

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

}
}

关于java - 图像不会在屏幕上绘制,但不会发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272407/

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