JAVA Frame 窗体背景图片,首尾相接滚动(2)

来源:互联网 发布:stata 债券数据是啥 编辑:程序博客网 时间:2024/06/10 22:27
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;


/**
 *键盘方向键,控制背景图,上、下、左、右各个方位循环滚动 
 *
 */
@SuppressWarnings("serial")
public class GameFrame extends MyFrame {//子窗体
private Image img=GameUtil.imageLoad("image/solar11.jpg");//图片存放路径,bin/image/solar.jpg
double width=1280/*PropertiesUtil.getValue("Width", "game.properties")*/;
double height=768/*PropertiesUtil.getValue("Height", "game.properties")*/;
double movs,speed=5,h_movs;
double pWidth,pHeight
,bgWidth=new ImageIcon(img).getIconWidth()
,bgHeight=new ImageIcon(img).getIconHeight();
StrategyMov strategyMov;
boolean k_left,k_right;
boolean k_up;
boolean k_down;
//===================================
public static void main(String[] args) {
GameFrame gf=new GameFrame();
gf.launchFrame();
}
/**
* @param args
*/
@Override
public void launchFrame(){
super.launchFrame();
this.addKeyListener(new KeyMonitor());
}
/**
* @param g
*/
public void bgMov(Graphics g){
//===================================================
pWidth=width;
pHeight=height;
//假设背景图片宽度,比屏幕宽的情况
   //(x=0,y=0)坐标原点
if(movs<=0){//横坐标左边界--出界
if(-movs>=bgWidth){//初始化
movs+=bgWidth;
   }
if(h_movs>=0){//纵坐标上边界--不出界
strategyMov=new StrategyMimMaxZero();
strategyMov.bgMov(g);

}else{//h_movs<0      纵坐标上边界--出界   
if(-h_movs>=bgHeight){//初始化
h_movs+=bgHeight;
   }
strategyMov=new StrategyMimMinZero();
strategyMov.bgMov(g);
}
}
else{//movs>0横坐标左边界--不出界
if(movs>=bgWidth){//初始化
movs-=bgWidth;
}
if(h_movs>=0){//movs>0
if(h_movs>=bgHeight){//初始化
h_movs-=bgHeight;
}
strategyMov=new StrategyMaxMaxZero();
strategyMov.bgMov(g);
}else{// h_movs<0 纵坐标上边界--出界(不跨界 or 跨界)
if(-h_movs>=bgHeight){//初始化
h_movs+=bgHeight;
   }
strategyMov=new StrategyMaxMinZero();
strategyMov.bgMov(g);
}
}
}

//======
@Override
public void paint(Graphics g) {
this.move();//控制背景图片移动方向
this.bgMov(g);
}

//=====================方向键 
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
direction_controlInc(e);
}
@Override
public void keyReleased(KeyEvent e) {
direction_controlDec(e);
}
}
//==============
/**
* 控制航向
*/
public void direction_controlInc(KeyEvent e){
switch (e.getKeyCode()) {
case 37:
k_left=true;
break;
case 38:
k_up=true;
break;
case 39:
k_right=true;
break;
case 40:
k_down=true;
break;


default:
break;
}
}
public void direction_controlDec(KeyEvent e){
switch (e.getKeyCode()) {
case 37:
k_left=false;
break;
case 38:
k_up=false;
break;
case 39:
k_right=false;
break;
case 40:
k_down=false;
break;


default:
break;
}
}
/**
* 根据方向和速度,处理坐标
*/

public void move(){
if(k_left){
movs-=speed;
}
if(k_right){
movs+=speed;
}
if(k_up){
h_movs-=speed;
}
if(k_down){
h_movs+=speed;
}
}
//==========================
//为背景绘制背景提供不同算法, strategy /strætədʒi/策略
interface StrategyMov{
public void bgMov(Graphics g);
}
/**
* movs>0 h_movs>0
*
*/
class StrategyMaxMaxZero implements StrategyMov{
@Override
public void bgMov(Graphics g) {
if(pHeight+h_movs<bgHeight){//屏幕纵坐标--不跨界
if((pWidth+movs)<bgWidth){//屏幕横坐标--不跨界
g.drawImage(img, 0, 0, (int)pWidth,(int)pHeight, (int)movs, (int)h_movs, (int)(pWidth+movs), (int)(pHeight+h_movs), null);
}else{//屏幕横坐标--出界
g.drawImage(img, 0, 0, (int)(bgWidth-movs),(int)pHeight, (int)movs, (int)h_movs, (int)(bgWidth), (int)(pHeight+h_movs), null);
//出界长度=movs+pWidth-bgWidth;
//屏幕的坐标左上:pWidth-出界长度//背景图片取景坐标左上,右下
g.drawImage(img,(int)(bgWidth-movs), 0, (int)pWidth,(int)pHeight, 0, (int)h_movs, (int)(pWidth+movs-bgWidth), (int)(pHeight+h_movs), null);
}
}else{//屏幕纵坐标 --跨界 ,出界长度=pHeight+h_movs-bgHeight;
if((pWidth+movs)<bgWidth){//屏幕横坐标--不出界+屏幕纵坐标 --出界
//1
g.drawImage(img, 0, 0, (int)pWidth,(int)(bgHeight-h_movs), (int)movs, (int)h_movs, (int)(pWidth+movs), (int)(bgHeight), null);
//2 补充残缺部分
g.drawImage(img, 0,(int)(bgHeight-h_movs), (int)pWidth,(int)pHeight, (int)movs, 0, (int)(pWidth+movs), (int)(pHeight+h_movs-bgHeight), null);
}else{//if((pWidth+movs)>bgWidth)//背景图片右边空白出界,需要补充的长度
//横出界长度=movs+pWidth-bgWidth;
//纵出界长度=pHeight+h_movs-bgHeight;
//1
g.drawImage(img, 0, 0, (int)(bgWidth-movs),(int)(bgHeight-h_movs), (int)movs, (int)h_movs, (int)(bgWidth), (int)(bgHeight), null);
//2
g.drawImage(img, 0, (int)(bgHeight-h_movs), (int)(bgWidth-movs),(int)(pHeight), (int)movs, 0, (int)(bgWidth), (int)(pHeight+h_movs-bgHeight), null);
//3
g.drawImage(img, (int)(bgWidth-movs), 0, (int)(pWidth),(int)(bgHeight-h_movs), 0, (int)h_movs, (int)(pWidth+movs-bgWidth), (int)(bgHeight), null);
//4
g.drawImage(img, (int)(bgWidth-movs), (int)(bgHeight-h_movs), (int)(pWidth),(int)(pHeight), 0, 0, (int)(pWidth+movs-bgWidth), (int)(pHeight+h_movs-bgHeight), null);
}
}

}
}
//===============
/**
* movs>0 h_movs<0
*
*/
class StrategyMaxMinZero implements StrategyMov{


@Override
public void bgMov(Graphics g) {
if(-h_movs>pHeight){//但-纵坐标上边界--不跨界
if((pWidth+movs)<bgWidth){//屏幕横坐标--不跨界
g.drawImage(img, 0, 0, (int)pWidth,(int)pHeight, (int)movs, (int)(bgHeight+h_movs), (int)(pWidth+movs), (int)(bgHeight+pHeight+h_movs), null);
}else{//屏幕横坐标--跨界 ok
g.drawImage(img, 0, 0, (int)(bgWidth-movs),(int)pHeight, (int)movs, (int)(bgHeight+h_movs), (int)(bgWidth), (int)(pHeight+bgHeight+h_movs), null);

g.drawImage(img,(int)(bgWidth-movs), 0, (int)pWidth,(int)pHeight, 0, (int)(bgHeight+h_movs), (int)(pWidth+movs-bgWidth), (int)(bgHeight+h_movs+pHeight), null);
}
}else{//且-纵坐标上边界--跨界
if((pWidth+movs)<bgWidth){//屏幕横坐标右边界--不跨界
//1   ok
g.drawImage(img, 0, 0, (int)pWidth,(int)(-h_movs), (int)movs, (int)(bgHeight+h_movs), (int)(pWidth+movs), (int)(bgHeight), null);
//2 补充残缺部分   ok
g.drawImage(img, 0,(int)(-h_movs), (int)pWidth,(int)pHeight, (int)movs, 0, (int)(pWidth+movs), (int)(pHeight+h_movs), null);
}else{//屏幕横坐标右边界--跨界
g.drawImage(img, 0, (int)(-h_movs), (int)(bgWidth-movs),(int)(pHeight), (int)movs, 0, (int)(bgWidth), (int)(pHeight+h_movs), null);
//补1
g.drawImage(img, 0, 0, (int)(bgWidth-movs),(int)(-h_movs), (int)movs, (int)(bgHeight+h_movs), (int)(bgWidth), (int)(bgHeight), null);
//补2
g.drawImage(img, (int)(bgWidth-movs), 0, (int)(pWidth),(int)(-h_movs), 0, (int)(bgHeight+h_movs), (int)(pWidth+movs-bgWidth), (int)(bgHeight), null);
//补3
g.drawImage(img, (int)(bgWidth-movs), (int)(-h_movs), (int)(pWidth),(int)(pHeight), 0, 0, (int)(pWidth+movs-bgWidth), (int)(pHeight+h_movs), null);
}
}
}
}
  //===============
/**
* movs>0 h_movs<0
*
*/
class StrategyMimMaxZero implements StrategyMov{


@Override
public void bgMov(Graphics g) {
if(h_movs>=bgHeight){//初始化
h_movs-=bgHeight;
   }
if(pWidth>-movs){//横坐标--跨界,脚踏两只船  
if(pHeight+h_movs<bgHeight){//纵坐标下边界--不出界
//movs为负值,取背景图片坐标:左上,右下
g.drawImage(img, 0, 0, (int)(-movs),(int)pHeight, (int)(bgWidth+movs), (int)(h_movs), (int)(bgWidth), (int)(pHeight+h_movs), null);
g.drawImage(img, (int)(-movs), 0, (int)pWidth,(int)pHeight, 0, (int)(h_movs), (int)(pWidth+movs), (int)(pHeight+h_movs), null);
}else{//纵坐标下边界--出界   
//1  ---code ok
g.drawImage(img, (int)(-movs), 0, (int)(pWidth),(int)(bgHeight-h_movs), 0, (int)(h_movs), (int)(pWidth+movs), (int)(bgHeight), null);
//补1
g.drawImage(img, 0, 0, (int)(-movs),(int)(bgHeight-h_movs), (int)(bgWidth+movs), (int)(h_movs), (int)(bgWidth), (int)(bgHeight), null);
//补2
g.drawImage(img, 0, (int)(bgHeight-h_movs), (int)(-movs),(int)pHeight, (int)(bgWidth+movs), 0, (int)(bgWidth), (int)(pHeight+h_movs-bgHeight), null);
//补3
g.drawImage(img, (int)(-movs), (int)(bgHeight-h_movs), (int)pWidth, (int)pHeight, 0,0,(int)(pWidth+movs), (int)(pHeight+h_movs-bgHeight), null);
}
}else{//横坐标--不跨界  
if(pHeight+h_movs<bgHeight){//纵坐标下边界--不出界 
g.drawImage(img, 0, 0, (int)pWidth,(int)pHeight, (int)(bgWidth+movs), (int)(h_movs), (int)(bgWidth+movs+pWidth), (int)(pHeight+h_movs), null);
}else{//横坐标--不跨界+纵坐标下边界--出界
//1---code ok
g.drawImage(img, 0, 0, (int)pWidth,(int)(bgHeight-h_movs), (int)(bgWidth+movs), (int)(h_movs), (int)(bgWidth+movs+pWidth), (int)(bgHeight), null);
//补1
g.drawImage(img, 0, (int)(bgHeight-h_movs), (int)pWidth,(int)pHeight, (int)(bgWidth+movs), 0, (int)(bgWidth+movs+pWidth), (int)(pHeight+h_movs-bgHeight), null);
}
}
}
}
//===============
/**
* movs>0 h_movs<0
*
*/
class StrategyMimMinZero implements StrategyMov{


@Override
public void bgMov(Graphics g) {
if(pWidth>-movs){//横坐标--跨界,脚踏两只船 
if(-h_movs>pHeight){//纵坐标上边界--不跨界
//movs为负值,取背景图片坐标:左上,右下   check 1
g.drawImage(img, 0, 0, (int)(-movs),(int)pHeight, (int)(bgWidth+movs), (int)(bgHeight+h_movs), (int)(bgWidth), (int)(bgHeight+h_movs+pHeight), null);
g.drawImage(img, (int)(-movs), 0, (int)pWidth,(int)pHeight, 0, (int)(bgHeight+h_movs), (int)(pWidth+movs), (int)(bgHeight+h_movs+pHeight), null);
}else{//横坐标--跨界+纵坐标上边界--跨界  
//1   code ok
g.drawImage(img, (int)(-movs), (int)(-h_movs), (int)(pWidth),(int)(pHeight), 0, 0, (int)(pWidth+movs), (int)(pHeight+h_movs), null);
//补1
g.drawImage(img, 0, 0, (int)(-movs),(int)(-h_movs), (int)(bgWidth+movs), (int)(bgHeight+h_movs), (int)(bgWidth), (int)(bgHeight), null);
//补2
g.drawImage(img, 0, (int)(-h_movs), (int)(-movs),(int)pHeight, (int)(bgWidth+movs), 0, (int)(bgWidth), (int)(pHeight+h_movs), null);
//补3
g.drawImage(img, (int)(-movs), 0, (int)(pWidth), (int)(-h_movs), 0,(int)(bgHeight+h_movs),(int)(pWidth+movs), (int)bgHeight, null);
}
}else{//横坐标--不跨界 +  纵坐标上边界--出界    
if(-h_movs>pHeight){//纵坐标上边界--不跨界   code check ok
g.drawImage(img, 0, 0, (int)pWidth,(int)pHeight, (int)(bgWidth+movs), (int)(bgHeight+h_movs), (int)(bgWidth+movs+pWidth), (int)(bgHeight+h_movs+pHeight), null);
}else{//纵坐标上边界--跨界
//1
g.drawImage(img, 0, (int)(-h_movs), (int)pWidth,(int)pHeight, (int)(bgWidth+movs), 0, (int)(bgWidth+movs+pWidth), (int)(pHeight+h_movs), null);
//补1
g.drawImage(img, 0, 0, (int)pWidth,(int)(-h_movs), (int)(bgWidth+movs), (int)(bgHeight+h_movs), (int)(bgWidth+movs+pWidth), (int)(bgHeight), null);
}
}
}
}

}

//=====================父窗体

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
 * 父窗体
 *
 */
@SuppressWarnings("serial")
public class MyFrame extends Frame{
protected Image imgBuffer;
public void launchFrame(){
int wd=1280/*PropertiesUtil.getValue("Width", "game.properties")*/;
int ht=768/*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();
}
}
}
}
/**
 */


@Override
public void update(Graphics g) {
if(imgBuffer==null){
imgBuffer=createImage(this.getWidth(),this.getHeight());
}
Graphics gBuffer=imgBuffer.getGraphics();
    gBuffer.fillRect(0, 0, this.getWidth(), this.getHeight());
this.paint(gBuffer);

g.drawImage(imgBuffer, 0, 0, this);
}


}

//====================工具类

import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;


public class GameUtil {
public static Image imageLoad(String path){
URL u=GameUtil.class.getClassLoader().getResource(path);
return Toolkit.getDefaultToolkit().getImage(u);
}
}

阅读全文
0 0
原创粉丝点击