JAVA Frame 窗體背景圖片,首位相接滾動代碼實例
更新時間:2017年04月14日 09:46:50 作者:hongyu83916
這篇文章主要介紹了JAVA Frame 窗體背景圖片,首位相接滾動代碼示例,需要的朋友可以參考下復制代碼
背景圖片連續(xù)滾動,程序已經(jī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不湊效,其有自己先進的實現(xiàn)方式(自己猜的,有時間學學)
*/
@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方法中編寫的繪圖過程對圖形緩沖區(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)容代碼對您有所幫助
相關文章
gateway和jwt網(wǎng)關認證實現(xiàn)過程解析
這篇文章主要介紹了gateway和jwt網(wǎng)關認證實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
SpringBoot返回前端Long類型字段丟失精度問題及解決方案
Java服務端返回Long整型數(shù)據(jù)給前端,JS會自動轉(zhuǎn)換為Number類型,本文主要介紹了SpringBoot返回前端Long類型字段丟失精度問題及解決方案,感興趣的可以了解一下2024-03-03
spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)
這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
Java同步關鍵字synchronize底層實現(xiàn)原理解析
synchronized關鍵字對大家來說并不陌生,當我們遇到并發(fā)情況時,優(yōu)先會想到用synchronized關鍵字去解決,synchronized確實能夠幫助我們?nèi)ソ鉀Q并發(fā)的問題,接下來通過本文給大家分享java synchronize底層實現(xiàn)原理,感興趣的朋友一起看看吧2021-08-08
Spring Boot集成Druid出現(xiàn)異常報錯的原因及解決
Druid 可以很好的監(jiān)控 DB 池連接和 SQL 的執(zhí)行情況,天生就是針對監(jiān)控而生的 DB 連接池。本文講述了Spring Boot集成Druid項目中discard long time none received connection異常的解決方法,出現(xiàn)此問題的同學可以參考下2021-05-05

