java中滚到背景怎么用_在JAVA项目中使用Frame实现背景图连续滚动

本文介绍如何在JAVA项目中使用Frame实现背景图的连续滚动效果,详细讲解了关键代码逻辑,包括图像加载、双缓冲及滚动更新等技术。
摘要由CSDN通过智能技术生成

在JAVA项目中使用Frame实现背景图连续滚动

发布时间:2020-11-17 14:56:52

来源:亿速云

阅读:82

作者:Leah

这期内容当中小编将会给大家带来有关在JAVA项目中使用Frame实现背景图连续滚动,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

代码如下:

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);

}

}

上述就是小编为大家分享的在JAVA项目中使用Frame实现背景图连续滚动了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值