JAVA Frame 窗體背景圖片,首位相接滾動(dòng)代碼實(shí)例
背景圖片連續(xù)滾動(dòng),程序已經(jīng)跑過(guò)。前提!背景圖片寬度比窗體長(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方法中編寫的繪圖過(guò)程對(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)文章
gateway和jwt網(wǎng)關(guān)認(rèn)證實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了gateway和jwt網(wǎng)關(guān)認(rèn)證實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Java?Web項(xiàng)目中解決中文亂碼方法總結(jié)(三種最新方法)
這篇文章主要介紹了Java?Web項(xiàng)目中解決中文亂碼方法總結(jié),本文給大家分享三種最新解決方法,需要的朋友可以參考下2022-06-06
SpringBoot返回前端Long類型字段丟失精度問題及解決方案
Java服務(wù)端返回Long整型數(shù)據(jù)給前端,JS會(huì)自動(dòng)轉(zhuǎn)換為Number類型,本文主要介紹了SpringBoot返回前端Long類型字段丟失精度問題及解決方案,感興趣的可以了解一下2024-03-03
JavaEE中關(guān)于ServletConfig的小結(jié)
ServletConfig是針對(duì)特定的Servlet的參數(shù)或?qū)傩?。ServletConfig是表示單獨(dú)的Servlet的配置和參數(shù),只是適用于特定的Servlet。從一個(gè)servlet被實(shí)例化后,對(duì)任何客戶端在任何時(shí)候訪問有效,但僅對(duì)本servlet有效,一個(gè)servlet的ServletConfig對(duì)象不能被另一個(gè)servlet訪問2014-10-10
Java去掉小數(shù)點(diǎn)后面無(wú)效0的方案與建議
當(dāng)前小數(shù)點(diǎn)后面的位數(shù)過(guò)多的時(shí)候,多余的0沒有實(shí)際意義,下面這篇文章主要給大家介紹了關(guān)于Java去掉小數(shù)點(diǎn)后面無(wú)效0的方案與建議,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
spring @Validated 注解開發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Java同步關(guān)鍵字synchronize底層實(shí)現(xiàn)原理解析
synchronized關(guān)鍵字對(duì)大家來(lái)說(shuō)并不陌生,當(dāng)我們遇到并發(fā)情況時(shí),優(yōu)先會(huì)想到用synchronized關(guān)鍵字去解決,synchronized確實(shí)能夠幫助我們?nèi)ソ鉀Q并發(fā)的問題,接下來(lái)通過(guò)本文給大家分享java synchronize底層實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2021-08-08
Spring Boot集成Druid出現(xiàn)異常報(bào)錯(cuò)的原因及解決
Druid 可以很好的監(jiān)控 DB 池連接和 SQL 的執(zhí)行情況,天生就是針對(duì)監(jiān)控而生的 DB 連接池。本文講述了Spring Boot集成Druid項(xiàng)目中discard long time none received connection異常的解決方法,出現(xiàn)此問題的同學(xué)可以參考下2021-05-05

