JAVA Frame 窗体背景图片,首位相接滚动
2017-04-13 19:43
405 查看
//背景图片连续滚动,程序已经跑过。前提!背景图片宽度比窗体长些(1毫米也行,求求了).
//小白,初学中ing
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);
}
}
//小白,初学中ing
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 窗体背景图片,首位相接滚动代码实例
- JAVA Frame 窗体背景图片,首尾相接滚动(2)
- JAVA GUI学习 - 窗体背景图片设置方法:重写paintComponent(Graphics g)方法
- java 如何为一个窗体设置背景图片
- java给窗体或面板设置背景图片
- Java Swing实现窗体添加背景图片的2种方法详解
- Java.swing-如何为frame窗口设置背景图片
- [JAVA]①重绘JPanel面板,设置窗体背景图片
- Java使背景图片自适应窗体的办法
- java 如何为一个窗体设置背景图片
- 在Java中为frame窗口设置背景图片
- Java-如何为frame窗口设置背景图片
- java怎么给窗体或者面板设置背景图片
- 将图片作为窗体背景,但是显示不出的问题解决。java。
- 使用javaswing设定背景图片为窗体大小(原创)
- Java Swing 之设置窗体背景图片
- java frame窗口设置背景图片
- 如何为JAVA窗体程序添加背景图片
- [JAVA]②重绘JPanel面板,设置窗体背景图片跟随图片变化
- 相册图片头尾相接的滚动算法