注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

AGLlib库之platform(J2SE)  

2011-07-10 21:14:55|  分类: AGLlib |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
该包位于com.robin.lib.platform,它有两个文件GameApplication.java,GameView.java。前者用于从J2SE平台启动我们的游戏,并从J2SE平台接受各类事件,后者用于把我们的游戏在J2SE平台上进行显示。
GameApplication.java文件
package com.robin.lib.platform;
import java.awt.Dimension;
import java.awt.Toolkit;

import com.robin.game.LineManager;
import com.robin.game.SpriteDemo;
import com.robin.lib.face.Application;
import com.robin.lib.face.GameEntity;
import com.robin.lib.core.Game;
import com.robin.lib.core.GameEngine;

public class GameApplication implements Application{
MainView view=null;
GameEngine gameEngine;
 /**
  * @param args
  */
 public GameApplication(GameEntity gameEntity,int width,int height) {
  // TODO Auto-generated method stub
         view=new MainView();
        Dimension   dmnScreen   =   Toolkit.getDefaultToolkit().getScreenSize();
        Dimension   dmnMain   =   new   Dimension(width+(MainView.kBorderW<<1), height+(MainView.kBorderH<<1)+MainView.kTitleH);

        view.setTitle( "画图测试 ");
        view.setSize(dmnMain);
        view.setLocation(
                (dmnScreen.width   -   dmnMain.width)   /   2,
                (dmnScreen.height   -   dmnMain.height)   /   2); 
        view.setVisible(true);
        Game.width=width;
        Game.height=height;
        gameEngine=new GameEngine(this,view,gameEntity);
        gameEngine.start();
 }
 public void destroy()
 {
  System.exit(0);
 }

}
GameView.java文件
package com.robin.lib.platform;
import com.robin.lib.core.Game;
import com.robin.lib.face.GameView;
import com.robin.lib.neutral.MotionEvent;
import com.robin.lib.neutral.Image;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
class MainView extends JFrame  implements GameView,WindowListener,WindowStateListener
,MouseListener,MouseMotionListener
{
//
 public MainView() {
  super();
  this.addWindowListener(this);
  this.addWindowStateListener(this);
  this.addMouseListener(this);
  this.addMouseMotionListener(this);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 boolean blServiceDraw=false;
 final static int kServiceDrawTimeMax=1000;
 final static String tag="hubin";
 BufferedImage bufferedImage;
 Image img;
 final static int kBorderW=5;
 final static int kBorderH=5;
 final static int kTitleH=26;
 final static  int offsetX=kBorderW;
 final static  int offsetY=kBorderH+kTitleH;
 public com.robin.lib.neutral.Graphics getViewGraphics()
 {
  if(bufferedImage==null)
  {
   bufferedImage=new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_ARGB_PRE);
  }
  if(img==null)
   img=Image.createImage(Game.width, Game.height);
  return img.getGraphics();
 }
 public boolean isReady()
 {
  return true;
 }
 public void repaint()
 {
  if(img!=null)
  {
   int data[]=img.getRGBData();
   for(int i=0;i<data.length;i++)
    data[i]=data[i]|0xFF000000;
   bufferedImage.setRGB(0, 0, Game.width, Game.height, data, 0, Game.width);
  }
  blServiceDraw=true;
  super.repaint();
 }
 public boolean servicePaint()
 {
  long startTime=System.currentTimeMillis();
  while(blServiceDraw)
  {
   try{
    Thread.sleep(10);
   }catch(InterruptedException e){
    e.printStackTrace();
    return false;
   }
   if(System.currentTimeMillis()-startTime>kServiceDrawTimeMax)
   {
    blServiceDraw=false;
    System.out.println("the time for Serviceing Draw is too long");
    return false;
   }
  }
  return true;
 }
 ArrayBlockingQueue <com.robin.lib.neutral.MotionEvent> motionEvents=
  new ArrayBlockingQueue <com.robin.lib.neutral.MotionEvent>(10);
 public Queue<com.robin.lib.neutral.MotionEvent> getMotionEvents()
 {
  com.robin.lib.neutral.MotionEvent event;
  while(!motionEventBuffer.isEmpty())
  {
   event=motionEventBuffer.remove();
   motionEvents.add(event);
  }
  return motionEvents;
 }
 int cnt=0;
 public void  paint  (Graphics g)
 {
  if(bufferedImage==null)
  {
g.setColor(new Color(0xFFFFFFFF));
g.fillRect(0, 0,getWidth(), getHeight());
   
  }
  else
 g.drawImage(bufferedImage, offsetX, offsetY, null);
  /*g.setColor(new Color(0xFF));
  g.drawLine(0, 0, 200, 300);*/
  blServiceDraw=false;
 }
 ConcurrentLinkedQueue<MotionEvent> motionEventBuffer=new ConcurrentLinkedQueue<MotionEvent>();
    public   void   mouseDragged(MouseEvent   event)
    {
     if   (SwingUtilities.isLeftMouseButton(event))
     {
      motionEventBuffer.add(new MotionEvent (MotionEvent.ACTION_DRAGGED,event));
     }
    }
    public   void   mouseMoved(MouseEvent   arg0)
    {

    } 
    public   void   mouseEntered(MouseEvent   arg0)
    {

    }
    public   void   mouseExited(MouseEvent   arg0)
    {

    } 
    public   void   mousePressed(MouseEvent   event)
    {
     motionEventBuffer.add(new MotionEvent (MotionEvent.ACTION_DOWN,event));
    }

    public   void   mouseReleased(MouseEvent   event)
    {
     motionEventBuffer.add(new MotionEvent (MotionEvent.ACTION_UP,event));
    } 
    public   void   mouseClicked(MouseEvent   event)
    {
    }
    public void windowStateChanged(WindowEvent e)
    {
     //System.out.println("window state:"+e.getNewState());
    }
    public void windowDeactivated(WindowEvent e)
    {
     //System.out.println("window is deactivated");
    }
    public void windowDeiconified(WindowEvent e)
    {
     //System.out.println("window is Deiconified"); 
    }
    public void windowActivated(WindowEvent e)
    {
    //System.out.println("window is actived");
    }
    public void windowOpened(WindowEvent e)
    {
     //System.out.println("window is Opened");
    }
    public void windowClosing(WindowEvent e)
    {
     //System.out.println("window is Closing");
    }
    public void windowClosed(WindowEvent e)
    {
     //System.out.println("window is Closed");
    }
    public void windowIconified(WindowEvent e)
    {
     //System.out.println("window is Closing");
    }
}

  评论这张
 
阅读(517)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017