如何重复绘制图像?

时间:2016-07-24 10:52:03

标签: java

我正在尝试用Java Swing制作简单的汽车游戏。我希望背景能够移动。当背景图像向下移动时,我必须再次连续绘制它。 我怎样才能做到这一点? P.S:背景和背景1是相同的图像

package com.mycompany.cardemo.car;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
*
* @author Suraj Gautam
*/
public class MainScreen extends JPanel implements ActionListener {



  Timer timer = new Timer(20, this);
  private ImageIcon background = new ImageIcon(getClass().getResource("/res/background.png"));
  private ImageIcon background2 = new ImageIcon(getClass().getResource("/res/background1.png"));
  private int x = 0;
  private int y = 0;
  private int velX = 1;
  private int velY = 1;

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    background.paintIcon(this, g, x, y);
    if (y > 0 && y<400) {
        background2.paintIcon(this, g, x, y);
    }


    timer.start();

  }

  public static void main(String[] args) {
    JFrame f = new JFrame("Car game");
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new MainScreen());
    f.setResizable(true);
    f.setVisible(true);

  }

  @Override
  public void actionPerformed(ActionEvent e) {

    y += velY;
    repaint();

  }

  public void updateValueOfy(int y) {
    this.y = y;
  }
}

background2.paintIcon在这里不起作用

2 个答案:

答案 0 :(得分:1)

在你的程序中background.paintIcon(this, g, x, y);background2.paintIcon(this, g, x, y);会将image2绘制在image1之上,因为它们具有相同的原点(x,y)。

至少需要做的是background2.paintIcon(this, g, x, y + background.getIconHeight());,以便image1和image2不重叠。

此外,要实现最终目标,您需要使用Y作为绘画的锚点,在整个帧上重复绘制2幅图像,您可以使用以下方法

  1. 绘制image1和image2,从Y向下开始直到到达帧结束
  2. 绘制image2和image1,从Y开始 - (图像2的高度)向上,直到到达帧的开始
  3. Y需要在帧结束后从帧的开头重新启动。
  4. 重启Y时需要考虑image1和image2的高度以防止出现故障。
  5. 以下是一个示例列表,证明可以在我的计算机上运行:

    private int bg1Height = background.getIconHeight();
    private int bg2Height = background2.getIconHeight();
    // painting height should be a multiple of (bg1height + bg2height) that is immediately larger than frameHeight
    private int paintingHeight = ((frameHeight / (bg1Height + bg2Height)) + 1)  * (bg1Height + bg2Height);
    
    public static int frameHeight = 400;
    
    public MainScreen() {
        timer.start();
    }
    
    public void actionPerformed(ActionEvent e) {
        y = (y + velY) % paintingHeight;
        repaint();
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        // paint downwards repeatedly
        int yTemp = y;
        while (yTemp < 400) {
            background.paintIcon(this, g, x, yTemp);
            yTemp += bg1Height;
            if (yTemp < 400) {
                background2.paintIcon(this, g, x, yTemp);
                yTemp += bg2Height;
            }
        }
    
        // paint upwards repeatedly
        yTemp = y;
        while (yTemp > 0) {
            yTemp -= bg2Height;
            background2.paintIcon(this, g, x, yTemp);
            if (yTemp > 0) {
                yTemp -= bg1Height;
                background.paintIcon(this, g, x, yTemp);
            }
        }
    }
    

答案 1 :(得分:0)

如果您想再次从位置(0,0)重复绘制图像,只需重置x and y坐标,如下所示: 假设您的最大身高JFrame为400。

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if(y >= 400) {
        y = 0;  x = 0;
    }

    background.paintIcon(this, g, x, y);
    timer.start();

  }