How to connect seamlessly with the Java implementation of the rolling cycle pictures

I. Introduction

This week is just a software training school week, to do a similar airplane war game, but to achieve better results airplane war game, will inevitably encounter a problem, how to achieve the cycle of the game background image scrolling and seamless connectivity it? After thinking, there was the following methods to achieve the same picture of two pictures by seamlessly rolling cycle.

Second, the realization of ideas

Say you have a picture AB, AB top picture if you want to achieve seamless loop rolling, then scroll down for some time the image AB displacement move, move down to find a way to move the end of the picture (B) is connected to the picture (A), as shown on FIG.
Here Insert Picture Description

Third, the implementation method

The two pictures with the same, a moving picture from the top down images, another picture from the bottom image moves up, as long as the same speed of movement two pictures, the picture can be realized seamless connection.

1. Move the picture from the top down
 Moving down from the top of the image

2. Move the picture from the bottom up
Moving up from the bottom of the picture

Fourth, to achieve results

Achieve results

Fifth, the reference code

Note that g is a Graphics object , namely canvas .

 private ImageIcon img=new ImageIcon("img/Background/1.png");
 private int moveY=0; //从图片顶端向下滚动
 private int moveYY=800;//从图片底端向上滚动,大小等于图片的长度

//如果移动超过图片的长度,则回到初始位置
moveY=(moveY>=800)?0:moveY;
moveYY=(moveYY<=0)?800:moveYY;
  
 //图片从底端向上滚动
 g.drawImage(img.getImage(), 
 getX(), getY(),  //屏幕左上角坐标
 getX()+300, getY()+400,  //屏幕右下角坐标(“+”后面为窗体的大小)
 0,moveYY,             //图片左上角坐标 
 600,800+moveYY,          //图片右下角坐标
 null);
 
 //图片从顶端向下滚动
 g.drawImage(img.getImage(), 
 getX(), getY(),  //屏幕左上角坐标
 getX()+300, getY()+400,  //屏幕右下角坐标
 0,-moveY,             //图片左上角坐标 
 600,800-moveY,          //图片右下角坐标
 null); 
 
 //移动位移!
 moveY+=5;
 moveYY-=5;  

Sixth, the picture requirements

To ensure the best display, the best choice for pictures on the left and right or up and down can join.
Here Insert Picture Description
If convergence is not on the picture, you can use Photoshop tools such as horizontal or vertical flip after stitching two pictures in a can.
Here Insert Picture Description
Here Insert Picture Description

Seven summary

Seemingly simple idea, think of it is not easy. Direct a picture can also be achieved with a rolling cycle of the picture, but it seems unattainable images seamlessly, may appear the following three cases:
when the coordinates of the lower right corner of the screen is greater than 1. The size of the window, showing part of the picture, the picture after a period of time to move the reset cause flicker.
flicker
2. When the lower right corner of the screen coordinates equal to the size of the window, displays the entire image, moving just a little blank area will appear.
blank
3. When the lower right corner of the screen coordinates is less than the size of the window, showing the whole picture, but the picture is less than the size of the window, surrounded by large areas of white space.
Big blank

Published 18 original articles · won praise 40 · views 50000 +

Guess you like

Origin blog.csdn.net/seawaysyyy/article/details/89290830