欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA Frame 窗体背景图片,首位相接滚动代码实例

程序员文章站 2024-02-29 16:39:46
背景图片连续滚动,程序已经跑过。前提!背景图片宽度比窗体长些,代码如下: import java.awt.graphics; import java.awt...

背景图片连续滚动,程序已经跑过。前提!背景图片宽度比窗体长些,代码如下:

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 java.awt.image.bufferedimage;
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);
}
}

希望以上内容代码对您有所帮助