侧滚动背景正在移出框架

时间:2018-06-09 20:57:54

标签: java multithreading algorithm swing jpanel

对于游戏,我需要实现侧滚动背景。 我试过这个,但我的画面正在移动。

我做错了什么?

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

1 个答案:

答案 0 :(得分:2)

如果需要将图像一致地向左或向右移动,则可以使用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 GameBoard extends JPanel {

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

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