我制作的乒乓球游戏中没有显示我的背景图像

问题描述 投票:0回答:1

我的背景图像无法加载到我用 Java 创建的乒乓球游戏中。 这是我的 GamePanel.java 文件,我相信错误来自于此。

import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.net.URL;

public class GamePanel extends JPanel implements ActionListener, KeyListener {
    static final int GAME_WIDTH = 800;
    static final int GAME_HEIGHT = 600;
    static final int BALL_DIAMETER = 20;
    static final int PADDLE_WIDTH = 10;
    static final int PADDLE_HEIGHT = 100;
    Paddle player1;
    Paddle player2;
    Ball ball;
    Timer timer;
    int player1Score = 0;
    int player2Score = 0;
    boolean playState = true;
    private Image backgroundImage;
    private SoundEffect scoreSound;  // Declaration of the SoundEffect object

    public GamePanel() {
        player1 = new Paddle(0, (GAME_HEIGHT / 2) - (PADDLE_HEIGHT / 2), PADDLE_WIDTH, PADDLE_HEIGHT, 1);
        player2 = new Paddle(GAME_WIDTH - PADDLE_WIDTH, (GAME_HEIGHT / 2) - (PADDLE_HEIGHT / 2), PADDLE_WIDTH, PADDLE_HEIGHT, 2);
        ball = new Ball((GAME_WIDTH / 2) - (BALL_DIAMETER / 2), (GAME_HEIGHT / 2) - (BALL_DIAMETER / 2), BALL_DIAMETER, BALL_DIAMETER);
        loadImage();
        scoreSound = new SoundEffect("/sounds/score.wav");  // Initialize the sound effect

        this.setFocusable(true);
        this.addKeyListener(this);
        this.setPreferredSize(new java.awt.Dimension(GAME_WIDTH, GAME_HEIGHT));
        timer = new Timer(10, this);
        timer.start();
    }

    private void loadImage() {
        // Example path that assumes the 'images' folder is directly in the project root directory
        String path = "/images/backgroundImage.png"; // Adjust this path as needed
        URL url = getClass().getClassLoader().getResource(path);
        if (url == null) {
            System.err.println("Failed to load background image: " + path);
            return;
        }
        ImageIcon ii = new ImageIcon(url);
        backgroundImage = ii.getImage();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 800, 600, this.getWidth(), this.getHeight(), this); // Draw the background full-size
        }
        drawScoreboard(g);
        drawCenterLine(g);
        player1.draw(g);
        player2.draw(g);
        ball.draw(g);
    }
    public void scoreUpdate(int player) {
        if (player == 1) {
            player1Score++;  // Correctly increment player1's score
        } else if (player == 2) {
            player2Score++;  // Correctly increment player2's score
        }
        scoreSound.play(); // Play sound on scoring
    }
    

    public void actionPerformed(ActionEvent e) {
        if (playState) {
            player1.move();
            player2.move();
            ball.move();
        }
        checkCollision();
        repaint();
    }

    public void checkCollision() {
        // Ball bounces off top & bottom window edges
        if (ball.y <= 0 || ball.y >= GAME_HEIGHT - ball.height) {
            ball.setYDirection(-ball.yVelocity);
        }

        // Ball bounces off paddles
        if (ball.intersects(player1) || ball.intersects(player2)) {
            ball.setXDirection(-ball.xVelocity);
        }

        // Ball goes out of bounds
        if (ball.x <= 0) {
            scoreUpdate(2);
            playState = false;
            newPaddles();
            newBall();
            ball.stop();
        } else if (ball.x >= GAME_WIDTH - ball.width) {
            scoreUpdate(1);
            playState = false;
            newPaddles();
            newBall();
            ball.stop();
        }
    }

    public void newPaddles() {
        player1.y = (GAME_HEIGHT / 2) - (PADDLE_HEIGHT / 2);
        player2.y = (GAME_HEIGHT / 2) - (PADDLE_HEIGHT / 2);
    }

    public void newBall() {
        ball.x = (GAME_WIDTH / 2) - (BALL_DIAMETER / 2);
        ball.y = (GAME_HEIGHT / 2) - (BALL_DIAMETER / 2);
    }




    private void drawScoreboard(Graphics g) {
        g.setColor(Color.WHITE);
        g.setFont(new Font("Consolas", Font.BOLD, 60));
        g.drawString(String.valueOf(player1Score), (GAME_WIDTH / 4), 50);
        g.drawString(String.valueOf(player2Score), (GAME_WIDTH / 4) * 3, 50);
    }

    private void drawCenterLine(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{10}, 0);
        g2d.setStroke(dashed);
        g2d.setColor(Color.WHITE);
        g2d.drawLine(GAME_WIDTH / 2, 0, GAME_WIDTH / 2, GAME_HEIGHT);
    }

    public void keyPressed(KeyEvent e) {
        player1.keyPressed(e);
        player2.keyPressed(e);
        if (e.getKeyCode() == KeyEvent.VK_SPACE && !playState) {
            playState = true;  // Start moving the ball again
            int direction = (player1Score + player2Score) % 2 == 0 ? 1 : -1;
            ball.start(direction);  // Serve the ball towards the last scorer
        }
    }

    public void keyReleased(KeyEvent e) {
        player1.keyReleased(e);
        player2.keyReleased(e);
    }

    public void keyTyped(KeyEvent e) {}
}

我的文件格式:
My file format

我尝试重命名文件并更改命名约定,以确保一切都相同。 我尝试过不同的 png 文件,看看我使用的 png 文件是否存在问题。 我什至咨询过人工智能来解决这个问题,但没有得出更好的结果。

java swing game-development
1个回答
0
投票

这似乎是错误的。这里的 x、y 坐标将远离框架的中心或边缘:

g.drawImage(backgroundImage, 800, 600, this.getWidth(),
    this.getHeight(), this);

尝试在

0,0
而不是
800,600
绘图,看看是否能解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.