2016-07-24 98 views
1

我想从Java Swing制作简单的汽车游戏。我想要背景移动。当背景图片向下移动时,我不得不再次绘制它。 我该怎么做? PS:背景和background1是相同的图像如何重复绘制图像?

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这里没有

+0

我试过你的程序,background2.paintIcon适用于我。有一点需要注意的是,你的框架尺寸是400×400,当你> 0和Y <400时,你绘制背景2的条件是这样的。因此,在整个框架的可视区域中,background2将始终被绘制。在这里通过使background1和background2完全实现在这里试图达到什么目的? – Bon

+0

我不希望那个框架是空的。我想让图像反复移动。每当背景图片移动时,立即我想要下一张图片被绘制,以便该框架看起来不会空白。 –

回答

1

工作在程序background.paintIcon(this, g, x, y);background2.paintIcon(this, g, x, y);会画画图像2在图像1的顶部,因为它们具有相同的原始点(x ,Y)。

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

此外,为了实现自己的最终目标,你需要在整个框架,用Y作为一个锚点你的画,画2幅图像重复,你可以用下面的方法

  1. 漆图像1并且image2交替地从Y向下开始,直到帧结束为止
  2. paint image2和image1交替地从Y开始 - (图像2的高度)向上,直到达到帧开始
  3. Y需要从一旦它达到结尾,帧的开始。
  4. 重新启动Y以防止毛刺时,您需要考虑image1和image2的高度。

下面是一个样本上市,这被证明是在我的电脑上工作:

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

图像根本不动? –

+0

我将timer.start()移到了MainScreen的构造函数中,因为定时器只需要启动一次。也许你错过了那部分? – Bon

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

    }