2014-06-23 7 views
0

내 문제는 내가 길이가 X 인 이미지를 가지고 있으며 이미지가 내 게임의 배경에서 지속적으로 스크롤되도록 만들고 싶습니다.마지막 이미지가 끝나는 동일한 지점에서 자바, 다시 그리기 배경 이미지

이렇게하려면 이전 이미지가 끝나는 지점에서 다음 배경 이미지를 다시 그려야한다는 것을 플레이어에게 분명히 알지 못합니다. 이것이 내가 알 수없는 것입니다.

현재 이미지를 연속적으로 다시 그릴 수 있지만 이전 이미지가 끝나는 것처럼 새 이미지가 다시 (0,0)에 그려 지므로 배경이 다시 그려지는 것이 분명합니다.

문제가 무엇인지는 알지만 해결책은 나를 피하는 것입니다. 문제는 이미지를 다시 그릴 때 위치가 0으로 재설정된다는 것입니다. 현재이 작업을 수행 할 수있는 다른 방법을 찾을 수 없기 때문에 누군가가 나를 도와 줄 수 있습니다.

JPanel의 폭은 1000입니다.

package cje.chris.edwards.game; 

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 


import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 

public class Board extends JPanel implements ActionListener{ 

    Player player; 
    Image background; 
    Timer timer; 
    private int scrollSpeed, location; 

    public Board(){ 

     player = new Player(); 
     this.addKeyListener(new Listener()); 
     setFocusable(true); 
     ImageIcon img_ic = new ImageIcon("map.png"); 
     background= img_ic.getImage(); 
     location = 0; 
     //5 milliseconds 
     scrollSpeed = -2; 
     timer = new Timer(5, this); 
     timer.start(); 


    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     //called using timer.start() and its delay 
     repaint(); 

    } 

    public void paint(Graphics g){ 
     super.paint(g); 
     Graphics2D graphics_2d = (Graphics2D) g; 

     //This is the section where my problem lies. 
     if(location != 0 && Math.abs(location) % (background.getWidth(null) - 1000) == 0){ 
      graphics_2d.drawImage(background, 1000, 0, null); 
      location = 0; 
     } 
     graphics_2d.drawImage(background, location += scrollSpeed, 0, null); 
     graphics_2d.drawImage(player.getImage(), 50, 100, null); 

     System.out.println(location); 
    } 

    private class Listener extends KeyAdapter{ 


     public void keyPressed(KeyEvent e){ 
      scrollSpeed = -1; 

      player.move(e); 
     } 



    } 

} 

내가 마지막 이미지의 끝 부분에 위치를 재설정 할 수있는 방법이 있나요 : 여기

지금까지 코드는? 그래서 완벽하게 매끄럽게 보입니다. 다시 한 번 감사드립니다! 당신이 코드를 게시하려는 경우 https://warosu.org/data/ic/img/0015/95/1385339455019.png

+0

을 시도 :

사례 사람이 내 코드를 시도하고 싶어 그냥, 내가 사용하고있는 이미지입니다. 퍼지가 필요없는 코드를 게시하십시오. – BevynQ

답변

1

public void paint(Graphics g){ 
    super.paint(g); 
    Graphics2D graphics_2d = (Graphics2D) g; 

    // define bounds by width of image 
    while (location > background.getWidth(null)){ 
     location -= background.getWidth(null); 
    } 
    while (location < -background.getWidth(null)){ 
     location += background.getWidth(null); 
    } 
    // draw image twice to handle any overlap 
    graphics_2d.drawImage(background, location += scrollSpeed, 0, null); 
    graphics_2d.drawImage(background, location + background.getWidth(null), 0, null); 

    System.out.println(location); 
}