【问题标题】:How to implement an infinite image loop?如何实现无限图像循环?
【发布时间】:2013-08-03 09:48:07
【问题描述】:

我目前正在开发具有移动背景的 2D 射击游戏。背景只是一个简单的图像。我想实现图像从上到下无休止地移动。

我考虑确定位于框架可见部分之外的底部图像部分,然后再次将其绘制在顶部。

@Override
public void paint(Graphics go) {
    Graphics2D g = (Graphics2D) go;

    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    g.drawImage(this.imgBackground, 0, this.yBackground, this);
}

yBackground 是绘制图像左上角的坐标。它是从另一个线程更改的:

while (!this.paused) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    int y = this.display.getBackgroundY();

    if (y == this.display.getHeight()) {
        y = 0;
    } else {
        y++;
    }
    this.display.setBackgroundY(y);
}

所以这是我的问题:我怎样才能得到在框架可见部分之外的图像部分?

【问题讨论】:

  • 1.请问为什么??? 2. 不要使用 public void paint(Graphics go) {, 搜索paintComponent, 3. 不要通过 Thread.sleep(10); 阻塞 EDT, 使用 Swing Timer 来代替 4. sleep(10);是非常快速的暂停,触及 Native OS 的延迟,从 => 33 开始,没有自己的 RepaintManager 等...为了更好的帮助,请尽快发布 SSCCE,简短,可运行,可编译
  • 好的,我会记住的

标签: java image awt paint infinite


【解决方案1】:

您可以从高度yBackground + imgBackground.getHeight() 开始再次绘制图像。视图上方的额外延伸部分会被剪掉,就像代码中的底部已经被剪掉一样。

【讨论】:

    【解决方案2】:

    您可以通过多种方式实现这一点,但基本前提是,您需要某种滚动偏移来确定基本图像的开始。

    然后你需要在它之前和之后填充区域(如果图像小于可用高度)直到空间被填充。

    以下示例使用javax.swing.Timer 将偏移量更新给定数量。然后paintComponent 方法会渲染它之前和之后的所有空间,包括当前图像位置。

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class ScrollingBackground {
    
        public static void main(String[] args) {
            new ScrollingBackground();
        }
    
        public ScrollingBackground() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new BackgroundPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class BackgroundPane extends JPanel {
    
            private BufferedImage bg;
            private int yOffset = 0;
            private int yDelta = 4;
    
            public BackgroundPane() {
                try {
                    bg = ImageIO.read(new File("Background.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yOffset += yDelta;
                        if (yOffset > getHeight()) {
                            yOffset = 0;
                        }
                        repaint();;
                    }
                });
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (bg != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
    
                    int xPos = (getWidth() - bg.getWidth()) / 2;
                    int yPos = yOffset;
    
                    while (yPos > 0) {
                        yPos -= bg.getHeight();
                        g2d.drawImage(bg, xPos, yPos, this);
                    }
    
                    yPos = yOffset;
                    while (yPos < getHeight()) {
                        g2d.drawImage(bg, xPos, yPos, this);
                        yPos += bg.getHeight();
                    }
    
                    g2d.dispose();
                }
            }
        }
    }
    

    您也许可以通过使用后备缓冲区和/或子图像对此进行优化,但您明白了...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多