josn1984

常用链接

统计

最新评论

2007年4月25日 #

一下为实现“走马灯”方式文字的代码

<script language="JavaScript">
   var MESSAGE  =   "  欢迎访问,谢谢!    "
   var POSITION = 150
   var DELAY    = 10
   var scroll = new statusMessageObject()
 function statusMessageObject(p,d) {
  this.msg = MESSAGE
  this.out = " "
  this.pos = POSITION
  this.delay = DELAY
  this.i     = 0
  this.reset = clearMessage}
function clearMessage() {
  this.pos = POSITION}
function scroller() {
  for (scroll.i = 0; scroll.i < scroll.pos; scroll.i++) {
    scroll.out += " "}
  if (scroll.pos >= 0)
   scroll.out += scroll.msg
  else scroll.out = scroll.msg.substring(-scroll.pos,scroll.msg.length)
  window.status = scroll.out
  scroll.out = " "
  scroll.pos--
  if (scroll.pos < -(scroll.msg.length)) {
   scroll.reset()}
  setTimeout ('scroller()',scroll.delay)}
function snapIn(jumpSpaces,position) {
  var msg = scroll.msg
  var out = ""
  for (var i=0; i<position; i++)
    {out += msg.charAt(i)}
  for (i=1;i<jumpSpaces;i++)
    {out += " "}
  out += msg.charAt(position)
  window.status = out
  if (jumpSpaces <= 1) {
    position++
    if (msg.charAt(position) == ' ')
      {position++ }
    jumpSpaces = 100-position
  } else if (jumpSpaces >  3)
       {jumpSpaces *= .75}
  else
    {jumpSpaces--}
  if (position != msg.length) {
    var cmd = "snapIn(" + jumpSpaces + "," + position + ")";
    scrollID = window.setTimeout(cmd,scroll.delay);
  } else {    window.status=""
    jumpSpaces=0
    position=0
    cmd = "snapIn(" + jumpSpaces + "," + position + ")";
    scrollID = window.setTimeout(cmd,scroll.delay);
    return false }
  return true}
snapIn(100,0);
</script>

posted @ 2010-11-06 08:57 香蕈阁 阅读(247) | 评论 (0)编辑 收藏

javabean由三部份组成

(1)属性(properties)
JAVABEAN提供了高层次的属性概念,属性在JAVABEAN中不只是传统的面向对象的概念里的属性,它同时还得到了属性读取和属性写入的API支持。属性值可以通过调用适当的BEAN方法进行。比如,可能BEAN有一个名字属性,这个属性的值可能需要调用String getName()方法读取,而写入属性值可能要需要调用void setName(String str)的方法。
每个JAVABEAN属性通常都应该遵循简单的方法命名规则,这样应用程序构造器工具和最终用户才能找到JAVABEAN提供的属性,然后查询或修改属性值,对bean进行操作。JAVABEAN还可以对属性值的改变作出及时的反应。比如一个显示当前时间的JAVABEAN,如果改变时钟的时区属性,则时钟立即重画,显示当前指定时区的时间。
(2)方法(method)
JAVABEAN中的方法就是通常的JAVA方法,它可以从其它组件或在脚本环境中调用。默认情况下,所有BEAN的公有方法都可以被外部调用,但BEAN一般只会引出其公有方法的一个子集。
由于JAVABEAN本身是JAVA对象,调用这个对象的方法是与其交互作用的唯一途径。公开BEAN方法在BEAN操作中降为辅助地位,国为两个高级BEAN特性--属性的事件是与BEAN交互的最好方式。
因为BEAN可以提供要让客户使用的public方法,但应当认识到,BEAN设计人员希望看到绝大部分BEAN的功能反映在属性和事件中,而不是在人工调用和各个方法中。
(3)事件(event)
BEAN与其它软件组件交流信息的主要方式是发送和接受事件。我们可以将BEAN的事件支持功能看作是集成电路中的输入输出引脚:工程师将引脚连接在一起组成系统,让组件进行通讯。有些引脚用于输入,有些引脚用于输出,相当于事件模型中的心头事件和接收事件。
事件为JAVABEAN组件提供了一种 发送通知给其它组件的方法。在AWT事件模型中,一个事件源可以注册事件监听器对象。当事件源检测到发生了某种事件时,它将调用事件监听器对象中的一个适当的事件处理方法来处理这个事件。

posted @ 2008-09-15 13:25 香蕈阁 阅读(1232) | 评论 (0)编辑 收藏

JAVABEAN定义

JAVABENA也是一种独立于平台和结构的应用程序编程接口(API).
就好像童年时玩的积木,把用组件搭起来的软件可以理解成用积木搭的形状不同的作品.因为组件是可重用的,所以肯定是经过很多应用程序的测试.
JAVABEAN的定义:JAVABEAN是可复用的平台独立的软件组件,开发者可以在软件构造器工具中对其直接进行可视化操作. 定义中"软件构造器"可以是WEB页面构造器,可视化应用程序构造器,GUI设计构造器或服务器应用程序构造器.而JAVABEAN可以是简单的GUI要素,如按钮和滚动条;也可以是复杂的可视化软件组件,如数据库视图.而JAVABEAN是没有GUI表现形式的,但这些JAVABEAN仍然可以使用应用程序构造器可视化地进行组合,比如JBuilder上的很多控件其实也是没有GUI形式的,但是你仍然可以拖放它们以在你的应用程序里生成相应的代码.一个JAVABEAN和一个JAVA APPLET很相似,是一个非常简单的遵循某种严格协议的JAVA类.

posted @ 2008-09-15 12:40 香蕈阁 阅读(540) | 评论 (0)编辑 收藏

JAVABEAN学习笔记

源自"清华大学网络系课程",只是本人在阅读时粗做的笔记

posted @ 2008-09-15 12:10 香蕈阁 阅读(188) | 评论 (0)编辑 收藏

java awt 图形用户界面

java.awt提供了基于java程序的GUI设计工具,主要包括三个概念:
Component——组件
Conainer——容器
LayoutManager——布局管理器


类java.awt.Component是许多组件类的父类,Component类中封装了组件通用的方法和属性,如图形的组件对象、大小、显示位置、前景色和背景色、边界、可见性等。相应的成员方法包括:
getComponentAt(intx,inty)
getFont()
getForeground()
getName()
getSize()
paint(Graphics g)
rapaint()
update()
setVisible(blooean b)
setSize(Dimension d)
setName(String name)等
Container也是一个类,实际上是Component的子类,其本身具有组件的性质,但是它的主要功能是容纳其它组件和容器。
LayoutManager是在需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器。


注意:
1.如果试图使用java语言提供的setLocation(),setSize(),setBounds()等方法,则都会被布局管理器覆盖。
2.如果要自己设置大小和位置,应取消布局管理器,方法为:
setLayout(null)

posted @ 2007-05-11 17:30 香蕈阁 阅读(220) | 评论 (0)编辑 收藏

此J2ME程序已经完善,贴出来大家看看(可惜我没能及时做注释,还请大家见谅)

这个是主文件(后面的是执行的操作文件):
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class LostInForest extends MIDlet implements CommandListener{
 
 private Display display;
 private Canvas canvas;
 private Command exitCmd;
 MenuScreen menuScreen = new MenuScreen();
 MyGameCanvas gameCanvas = new MyGameCanvas();
 
 private Command exitGame;
 Alert aAlert = new Alert("Game Method","Login the game,point up,down,left,right contron",null,AlertType.INFO);
 Alert bAlert = new Alert("Maker","our",null,AlertType.INFO);
 
 public LostInForest(){
  exitCmd = new Command("exit",Command.EXIT,2);
  exitGame = new Command("Exit Game",Command.EXIT,1);
  aAlert.setTimeout(10000);
  bAlert.setTimeout(10000);
  aAlert.addCommand(exitCmd);
  bAlert.addCommand(exitCmd);


  gameCanvas.addCommand(exitGame);
  gameCanvas.setCommandListener(this);
  aAlert.setCommandListener(this);
  bAlert.setCommandListener(this);
 }
 public void startApp() throws MIDletStateChangeException{
  canvas = new MenuScreen();
  display = Display.getDisplay(this);
  display.setCurrent(canvas);
 }
 public void pauseApp(){}
 public void destroyApp(boolean b){}
 public void commandAction(Command cmd,Displayable dis){
  if(cmd == exitCmd){
   display.setCurrent(menuScreen);
  }
  else if(cmd == exitGame){
   System.gc();
   destroyApp(false);
   notifyDestroyed();
  }
 }
 class MenuScreen extends Canvas implements Runnable{
  final Font lowFont = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
  final Font highFont = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE);
  final int lowColor = 0x000000AA;
  final int highColor = 0x00FF0000;
  final int highBGColor = 0x00AAAAAA;
  int width;
  int height;
  int startHeight;
  int menuHeight;
  final int spacing = highFont.getHeight()/2;
  final String[] mainMenu = {"Start Game","Game Method","Worker","Exit"};
  int menuIdx;
  Thread menuThread;
  
  public MenuScreen(){
   width = getWidth();
   height = getHeight();
   menuHeight = (highFont.getHeight() * mainMenu.length)+((mainMenu.length-1)*spacing);
   startHeight = (height - menuHeight)/2+4;
   menuIdx = 0;
   menuThread = new Thread(this);
   menuThread.start();
  }
  public void run(){
   while(true){
    repaint();
   }
  }
  public void paint(Graphics g){
   g.setColor(0x00FFFF00);
   g.fillRect(0,0,width,height);
   
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   Font label1 = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE);
   g.drawString("LostInForest",62,label1.getHeight()/2,Graphics.TOP|Graphics.LEFT);
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   g.drawString("LostInForest",0,24,Graphics.TOP|Graphics.LEFT);
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   g.drawString("Copyright@2007 SZPT",25,145,Graphics.LEFT|Graphics.TOP);
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   g.drawString("All Rights Reserved.",30,160,Graphics.LEFT|Graphics.TOP);
   
   for(int i = 0;i<mainMenu.length;i++){
    if(i == menuIdx){
     g.setColor(highBGColor);
     g.fillRect(0,startHeight + (i*highFont.getHeight())+spacing,width,highFont.getHeight());
     g.setFont(highFont);
     g.setColor(highColor);
     g.drawString(mainMenu[i],(width - highFont.stringWidth(mainMenu[i]))/2,startHeight+(i*highFont.getHeight())+spacing,20); 
    }
    else{
   g.setFont(lowFont);
   g.setColor(lowColor);
   g.drawString(mainMenu[i],(width - highFont.stringWidth(mainMenu[i]))/2,startHeight+(i*highFont.getHeight())+spacing,20);
          }
   }
  }
  protected void keyPressed(int code){
   if(getGameAction(code) == Canvas.UP&&menuIdx - 1 >= 0){
    menuIdx--;
   }
   else if(getGameAction(code) == Canvas.DOWN&&menuIdx + 1 < mainMenu.length){
    menuIdx ++;
   }
   if(getGameAction(code) == Canvas.FIRE){
    if(mainMenu[menuIdx] == "Start Game"){
     gameCanvas.start();
     display.setCurrent(gameCanvas);
     }
    else if(mainMenu[menuIdx] == "Game Method"){
     display.setCurrent(aAlert);
    }
    else if(mainMenu[menuIdx] == "Maker"){
     display.setCurrent(bAlert);
    }
    else if(mainMenu[menuIdx] == "Exit"){
     notifyDestroyed();
    }
   }
  } 
 }
}
这个是主操作文件:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.rms.*;

public class MyGameCanvas extends GameCanvas implements Runnable{
 private boolean isPlay;
 private long delay;
 private int currentX,currentY;
 private int width;
 private int height;
 Graphics g;
 public Display display;
 Image image = null;
 Image tileImages = null;
 private Sprite spriteRole;
 private static int INDEX_OF_UP = 0;
 private static int INDEX_OF_DOWN = 1;
 private static int INDEX_OF_LEFT = 3;
 private static int INDEX_OF_RIGHT = 2;
 private TiledLayer tiledBackground;
 private TiledLayer tiledLayer = null;
 private LayerManager layerManager;
 private RecordStore rs = null;
 private int gameLifeValueInt = 3;
 private String gameLifeValueString = null;
 private int firstGate = 0;
 private int[] map = {
 };
 
 public MyGameCanvas(){
  super(true);
  width = getWidth() - 20;
  height = getHeight() - 17;
  currentX = 160;
  currentY = 160;
  delay = 20;
  g = getGraphics();
  try{
   image = Image.createImage("/player.png");
   spriteRole = new Sprite(image,16,16);
   tileImages = Image.createImage("/tiles.png");
   tiledLayer = new TiledLayer(12,12,tileImages,16,16);
   tiledBackground = initBackground();
   layerManager = new LayerManager();
   layerManager.append(tiledBackground);
   layerManager.append(spriteRole);
   IntToString();
   createNewDB();
   setRecordToDB(1,gameLifeValueString,0,1);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 public void start(){
  isPlay = true;
  Thread t = new Thread(this);
  t.start();
 }
 public void stop(){
  isPlay = false;
 }
 public void run(){
  while(isPlay){
   input();
   drawScreen(g);
   try{
    Thread.sleep(delay);
   }
   catch(InterruptedException ie){}
  }
 }
 private void IntToString(){
  gameLifeValueString = gameLifeValueInt + "";
 }
 private void createNewDB(){
  try{
   rs = RecordStore.openRecordStore("GameDB",true);
  }
  catch(RecordStoreNotFoundException rsnfe){}
  catch(RecordStoreFullException rsfe){}
  catch(RecordStoreException rse){}
 }
 private void addRecordToDB(String str){
  byte rec[] = str.getBytes();
  try{
   rs.addRecord(rec,0,rec.length);
  }
  catch(RecordStoreFullException rse){}
  catch(RecordStoreException rse){}
 }
 private String getRecordFromDB(){
  byte data[] = new byte[5];
  int dataLength = 0;
  try{
   dataLength = rs.getRecord(1,data,0);
  }
  catch(RecordStoreException rse){}
  return new String(data,0,dataLength);
 }
 private void setRecordToDB(int recordID,String str,int offset,int len){
  try{
   byte data[] = str.getBytes();
   rs.setRecord(recordID,data,offset,len);
  }
  catch(RecordStoreException rse){}
 }
 private void input(){
  int keyStates = getKeyStates();
  try{
   if((keyStates&LEFT_PRESSED)!=0&&gameLifeValueInt > 0){
    int tempX = Math.max(0,currentX - 16);
    int tempPoint = tempX/16+currentY*12/16;
    if(map[tempPoint]==1){
     currentX = tempX;
    }
    else if(map[tempPoint]==5){
     currentX = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentX  = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_LEFT);
   }
  else if((keyStates&RIGHT_PRESSED)!=0&&gameLifeValueInt>0){
   int tempX = Math.min(width,currentX + 16);
   int tempPoint = tempX/16+currentY*12/16;
    if(map[tempPoint]==1){
     currentX = tempX;
    }
    else if(map[tempPoint]==5){
     currentX = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentX  = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_RIGHT);
  }
  else if((keyStates&UP_PRESSED)!=0&&gameLifeValueInt > 0){
   int tempY = Math.max(0,currentY - 16);
   int tempPoint = currentX/16+tempY*12/16;
    if(map[tempPoint]==1){
     currentY = tempY;
    }
    else if(map[tempPoint]==5){
     currentY = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentY = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }   
    else if(map[tempPoint]==2){
     currentY  = tempY;
     try{
      firstGate = 100;
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_UP);
  }
  else if((keyStates&DOWN_PRESSED)!=0&&gameLifeValueInt>0){
   if(currentY + 10<height){
   int tempY = Math.min(height,currentY + 16);
   int tempPoint = currentX/16+tempY*12/16;
    if(map[tempPoint]==1){
     currentY = tempY;
    }
    else if(map[tempPoint]==5){
     currentY = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentY  = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_DOWN);
   }
  }
   }
 catch(ArrayIndexOutOfBoundsException e){}
 }
 
 public void drawScreen(Graphics g){
 g.setColor(0x00FFFFFF);
 g.fillRect(22,22,getWidth(),getHeight());
 layerManager.append(tiledBackground);
 layerManager.paint(g,0,0);
 spriteRole.setPosition(currentX,currentY);
 spriteRole.paint(g);
 g.setColor(255,255,255);
 g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM));
 g.drawString("Life:"+gameLifeValueInt,110,0,Graphics.RIGHT|Graphics.TOP);
 if(gameLifeValueInt<=0){
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
  g.drawString("In forest you ...",150,80,Graphics.RIGHT|Graphics.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL));
  g.drawString("PLEASE RESTART",170,100,Graphics.RIGHT|Graphics.TOP);
 }
 else if(gameLifeValueInt>=1&&firstGate == 100){
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
  g.drawString("you do it",160,80,Graphics.RIGHT|Graphics.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL));
  g.drawString("MISSION COMPLETE",170,100,Graphics.RIGHT|Graphics.TOP);
 }
 flushGraphics();
  }
  private TiledLayer initBackground() throws Exception{
   for(int i = 0; i < map.length; i ++){
    int column = i % 12;
    int row = (i - column)/12;
    tiledLayer.setCell(column,row,map[i]);
   }
   return tiledLayer;
  }
}
至于地图,请大家自己看着程序自己随意构思一个好了。我没有在这里写入地图,呵呵!程序已经编译通过!

posted @ 2007-04-25 06:32 香蕈阁 阅读(506) | 评论 (0)编辑 收藏