2016-07-24 49 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(); 

    }