对于游戏,我需要实现侧面滚动背景。
我已经尝试过了,但是我的照片超出了画框。

我究竟做错了什么?

import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import javax.imageio.ImageIO;

public class GameBoard extends JPanel {

    private BufferedImage image;
    private int imageCoordX = 0;

    public GameBoard() throws Exception {
        setBackground(new Color(0,0,86));
        URL url = new URL("https://i.stack.imgur.com/P59NF.png");
        image = ImageIO.read(url);
        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

        ActionListener animListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                imageCoordX = imageCoordX + 10;
                repaint();
            }
        };
        Timer timer = new Timer(120, animListener);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, imageCoordX, 0, null);
        System.out.println(imageCoordX);

        if (imageCoordX < 0) {
            g.drawImage(image, image.getWidth() + imageCoordX, 0, null); //
        }

        if (imageCoordX > 0) {
            g.drawImage(image, image.getWidth() - imageCoordX, 0, null);
        }
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            JFrame f = new JFrame("Animation of Gameboard");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            try {
                f.add(new GameBoard());
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

最佳答案

如果有必要向左或向右一致地移动图像,则可以使用oneInt%anotherInt的模数(例如imageCoordX)来获取可用绘图空间范围内的值。

这是一个这样做的例子。

import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import javax.imageio.ImageIO;

public class WorldMapScroll extends JPanel {

    private final BufferedImage image;
    private int imageCoordX = 0;
    private int step = 5; // Travels east. Use negative for west

    public WorldMapScroll() throws Exception {
        setBackground(new Color(0, 0, 86));
        URL url = new URL("https://i.stack.imgur.com/P59NF.png");
        image = ImageIO.read(url);
        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

        ActionListener animListener = (ActionEvent e) -> {
            imageCoordX = imageCoordX + step;
            repaint();
        };
        Timer timer = new Timer(60, animListener);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int w = image.getWidth();
        g.drawImage(image, imageCoordX % w, 0, this);
        System.out.println(imageCoordX);

        if (step>0) {
            g.drawImage(image, (imageCoordX%w)-image.getWidth(), 0, this);
        } else {
            g.drawImage(image, (imageCoordX%w)+image.getWidth(), 0, this);
        }
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            JFrame f = new JFrame("Animation of World Map");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            try {
                f.add(new WorldMapScroll());
                f.setResizable(false);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

09-20 02:14