//背景圖片連續滾動,程序已經跑過。前提!背景圖片寬度比窗體長些(1毫米也行,求求了).
//小白,初學中ing
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import mine.game.util.PropertiesUtil;
@SuppressWarnings("serial")
public class GameFrame extends MyFrame{
private Image img=ImageUtil.imageLoad("image/bk.jpg");
double movs,speed=1,headmovs;
double pWidth,pHeight,bgWidth;
@Override
public void paint(Graphics g) {
//g.drawImage(img, 0, 0, null);
//===================================================
pWidth=PropertiesUtil.getValue("Width", "game.properties");
pHeight=PropertiesUtil.getValue("Height", "game.properties");
bgWidth=new ImageIcon(img).getIconWidth();
//movs+=speed;
if(bgWidth>pWidth+movs){
g.drawImage(img, 0, 0, (int)pWidth,(int)pHeight, (int)movs, 0, (int)(pWidth+movs), (int)pHeight, null);
}
if(bgWidth<=pWidth+movs){
headmovs=pWidth+movs-bgWidth;
g.drawImage(img, 0, 0, (int)(pWidth-headmovs),(int)pHeight, (int)movs, 0, (int)(bgWidth), (int)pHeight, null);
g.drawImage(img,(int)(pWidth-headmovs), 0, (int)pWidth,(int)pHeight, 0, 0, (int)(headmovs), (int)pHeight, null);
if(headmovs>=pWidth){
//重新初始化所有變量數據,循環
movs=headmovs-pWidth;
}
}
movs+=speed;
//===================================================
}
public static void main(String[] args) {
GameFrame gf=new GameFrame();
gf.launchFrame();
}
}
//=================================
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import mine.game.util.PropertiesUtil;
@SuppressWarnings("serial")
public class MyFrame extends Frame{
private BufferedImage imgBuffer;
private Graphics gBuffer;
public void launchFrame(){
int wd=800;//PropertiesUtil.getValue("Width", "game.properties");
int ht=600;//PropertiesUtil.getValue("Height", "game.properties");
setSize(wd,ht);
setLocation(0, 0);
setVisible(true);
new PaintThread().start();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//重畫窗口線程,內部類
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 雙緩衝解決,屏閃.此方法在,繼承Frame的AWT編程中才有效。JFram不湊效,其有自己先進的實現方式(自己猜的,有時間學學)
*/
@Override
public void update(Graphics g) {
if(imgBuffer==null){
imgBuffer=(BufferedImage)createImage(this.getWidth(),this.getSize().height);//創建圖形緩衝
//imgBuffer=new BufferedImage((int)this.getSize().getWidth(),(int)this.getSize().getHeight(),BufferedImage.TYPE_4BYTE_ABGR);//創建圖形緩衝
}
gBuffer=imgBuffer.getGraphics();//獲取圖形緩衝區的圖形上下文
gBuffer.fillRect(0, 0, this.getWidth(), this.getHeight());
this.paint(gBuffer);//用paint方法中編寫的繪圖過程對圖形緩衝區繪圖
gBuffer.dispose();//釋放圖形上下文資源
g.drawImage(imgBuffer, 0, 0, null);//將圖形緩衝區繪製到屏幕上
}
}
//====================
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
public class ImageUtil {
public static Image imageLoad(String path){
URL u=ImageUtil.class.getClassLoader().getResource(path);
return Toolkit.getDefaultToolkit().getImage(u);
}
}