JAVA Frame 窗體背景圖片,首位相接滾動(dòng)代碼實(shí)例
更新時(shí)間:2017年04月14日 09:46:50 作者:hongyu83916
這篇文章主要介紹了JAVA Frame 窗體背景圖片,首位相接滾動(dòng)代碼示例,需要的朋友可以參考下復(fù)制代碼
背景圖片連續(xù)滾動(dòng),程序已經(jīng)跑過。前提!背景圖片寬度比窗體長(zhǎng)些,代碼如下:
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){ //重新初始化所有變量數(shù)據(jù),循環(huán) 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); } }); } //重畫窗口線程,內(nèi)部類 class PaintThread extends Thread{ public void run(){ while(true){ repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 雙緩沖解決,屏閃.此方法在,繼承Frame的AWT編程中才有效。JFram不湊效,其有自己先進(jìn)的實(shí)現(xiàn)方式(自己猜的,有時(shí)間學(xué)學(xué)) */ @Override public void update(Graphics g) { if(imgBuffer==null){ imgBuffer=(BufferedImage)createImage(this.getWidth(),this.getSize().height);//創(chuàng)建圖形緩沖 //imgBuffer=new BufferedImage((int)this.getSize().getWidth(),(int)this.getSize().getHeight(),BufferedImage.TYPE_4BYTE_ABGR);//創(chuàng)建圖形緩沖 } gBuffer=imgBuffer.getGraphics();//獲取圖形緩沖區(qū)的圖形上下文 gBuffer.fillRect(0, 0, this.getWidth(), this.getHeight()); this.paint(gBuffer);//用paint方法中編寫的繪圖過程對(duì)圖形緩沖區(qū)繪圖 gBuffer.dispose();//釋放圖形上下文資源 g.drawImage(imgBuffer, 0, 0, null);//將圖形緩沖區(qū)繪制到屏幕上 } } //==================== 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); } }
希望以上內(nèi)容代碼對(duì)您有所幫助
相關(guān)文章
Java?自定義注解在登錄驗(yàn)證的應(yīng)用示例
本文主要介紹了Java?自定義注解在登錄驗(yàn)證的應(yīng)用示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Kotlin傳遞可變長(zhǎng)參數(shù)給Java可變參數(shù)實(shí)例代碼
這篇文章主要介紹了Kotlin傳遞可變長(zhǎng)參數(shù)給Java可變參數(shù)實(shí)例代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01JMeter連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
本文主要介紹了JMeter操作Mysql數(shù)據(jù)庫(kù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Java代理模式與動(dòng)態(tài)代理之間的關(guān)系以及概念
代理模式是開發(fā)中常見的一種設(shè)計(jì)模式,使用代理模式可以很好的對(duì)程序進(jìn)行橫向擴(kuò)展。動(dòng)態(tài)代理:代理類在程序運(yùn)行時(shí)被創(chuàng)建的代理方式。關(guān)鍵在于動(dòng)態(tài),程序具有了動(dòng)態(tài)特性,可以在運(yùn)行期間根據(jù)不同的目標(biāo)對(duì)象生成動(dòng)態(tài)代理對(duì)象2023-02-02java 教你如何給你的頭像添加一個(gè)好看的國(guó)旗
這篇文章主要介紹了java 教你如何給你的頭像添加一個(gè)好看的國(guó)旗,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Mybatis第三方PageHelper分頁(yè)插件的使用與原理
提到插件相信大家都知道,插件的存在主要是用來(lái)改變或者增強(qiáng)原有的功能,MyBatis中也一樣,下面這篇文章主要給大家介紹了關(guān)于Mybatis第三方PageHelper分頁(yè)插件的使用與原理,需要的朋友可以參考下2022-02-02