疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

javafx

Posted on 2009-07-27 09:48 疯狂 阅读(2044) 评论(0)  编辑  收藏 所属分类: javafx

初学javafx 写了个小例子,没有使用图片, 代码较乱 如下:
预览:

/*
 * JDI.fx
 *
 * Created on 2009-7-22, 19:06:20
 
*/


package gui;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.scene.effect.Reflection;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.*;
import javafx.scene.effect.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.image.*;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
/**
 * 
@author ericjoe
 
*/

// var k = KeyEvent.CHAR_UNDEFINED.
var complete = false;

var background
=ImageView {
             
              focusTraversable:
true
              onKeyPressed: function( e: KeyEvent ):Void 
{
                  
if(complete==false)move(e);
              }

            }

var logot
=0;
var logarc
=Rectangle {
        x: 
0, y: 0
        width: 
200, height: 80
        fill: Color.BLUEVIOLET
    }

var logoline
=Line {
        startX: 
10, startY:40
        endX: 
690, endY: 40
        strokeWidth: 
1
        stroke: Color.RED
 }

var gamelogo
=Text {
      fill:Color.BLACK
       translateX:bind logot
      effect:Reflection 
{
              fraction: 
0.75
              topOffset: 
0.0
              topOpacity: 
0.4
              bottomOpacity: 
0.0
          }

        font : Font 
{
            size: 
30;
}

        x: 
-120, y: 30
        content: 
"move game"
    }

var goNum
=0;

var state
= bind if(complete)then "complete! use {goNum} steps" else "step:{goNum}";
var statex
=0;
var gamestate
=Text {
      fill:Color.YELLOWGREEN
        font : Font 
{
            size: 
24
        }

        x: 
800, y: 65
        content: bind state;
    }

 

var gameline
=Line {
        startX: 
10, startY:80
        endX: 
690, endY: 80
        strokeWidth: 
1
        stroke: Color.BLUEVIOLET
 }

 
var gameframe
=Rectangle {
        x: 
10, y: 100
        width: 
580, height: 580
        fill: Color.BLACK
        focusTraversable:
true
    }


 var t
=[1..8];
 var r
= new java.util.Random();
 var cards:Card[]
= getCards();
 
 function getCards():Card[]
{
     var n 
= 0;
     var cardsinit:Card[];
    
for(i in [0..7]){
      var card
=Card{
         recX:
10+(580/3)*n
         recY:
100+(580/3)*(i/3)
         cardTextX: 
10+(580/3)*n+70
         cardTextY:
100+(580/3)*(i/3)+120
         cardTextConten:getTextContent();
       }

     insert card into cardsinit;
     n
++;
     
if(n==3)then n=0;
  }

  t
=[1..8];
  cardsinit;
}

var main
=true;
var changeButton
=Button {
        text: 
"change"
        layoutX:
900
        layoutY:
10
        onMousePressed: function( e: MouseEvent ):Void 
{
             cards
=getCards();
              btx
=10+(580/3)*2;
              bty
=100+(580/3)*2;
              complete
=false;
              goNum
=0;
        }

       onKeyPressed: function( e: KeyEvent ):Void 
{
                  
if(complete==false)move(e);
              }

    }

 var htext
="help";
 var helpButton
=Button {
        text: bind htext;
        layoutX:
1000
        layoutY:
10
        width:
100
        action: function() 
{
            htext
=if(htext=="help") then "back to game" else "help";
            main
=if(main==true)then false else true;
        }

        onKeyPressed: function( e: KeyEvent ):Void 
{
                  
if(complete==false)move(e);
              }

    }

 var rightgroup
=Group {
    translateX:bind statex;
        content: [
          changeButton,
          gamestate,
          helpButton
        ]
    }


function getTextContent():Integer
{
  
//  println(t.size());
   var i = t[r.nextInt(sizeof t)];
   delete i from t;
   
return i;
    }

var btx:Number
=10+(580/3)*2;
var bty:Number
=100+(580/3)*2;
var cardBlank 
= Rectangle {
        x: bind btx ,
        y: bind bty ,
        width: 
580/3, height: 580/3
        fill: Color.BLACK
      }

 var cardsc
=0.0;
var cardsGroup
=  Group {
       opacity:bind cardsc;
       focusTraversable:
true
        content:bind [
             cards,
             cardBlank
        ]
    }

var helptext 
= Text {
         font : Font 
{
             size: 
24
         }

         fill:Color.YELLOWGREEN
         x: 
150, y: 300
         content: 
" use ↑↓← → to move  the  chess";

     }

  var scorrt
=0;
 var scorrtext 
= Text {
         font : Font 
{
             size: 
12
         }

         translateX:bind scorrt;
         fill:Color.YELLOWGREEN
         layoutX:
-110
         layoutY:
95
        
// x: 10, y: 95
         content: "a javafx example";

     }

     


var mainSence 
= Scene {
    width: 
600
    height:
700
    fill:Color.BLACK
        content:bind [
           background,
           rightgroup,
           logoline,
           scorrtext,
           logarc,
            gamelogo,
            gameline,
            gameframe,
            
if(main)then cardsGroup else helptext
        ]
    }

Stage 
{
    title: 
"move game"
    resizable:
false    
    scene: mainSence
}

 
function move(e:KeyEvent):Void
{
    changeButton.focusTraversable
=false;
     
if(e.code==KeyCode.VK_DOWN){
       
if(cardBlank.y==100){
            
// println("cont not move")
           }
else{
        
for(g in cards){
            
if(g.recY-cardBlank.y==-580/3 and g.recX==cardBlank.x){
                g.recY
+=580/3;
                g.cardTextY
+=580/3;
                bty
-=580/3;
                 
break;
                }

            }

            goNum
++;
      }

       }

      
if(e.code==KeyCode.VK_UP){
       
if(cardBlank.y==100+(580/3)*2){
          
//   println("cont not move")
           }
else{
        
for(g in cards){
            
if(g.recY-cardBlank.y==580/3 and g.recX==cardBlank.x){
                g.recY
-=580/3;
                g.cardTextY
-=580/3;
                bty
+=580/3;
                
break;
              }

            }

             goNum
++;
      }

       }

        
if(e.code==KeyCode.VK_LEFT){
       
if(cardBlank.x==10+(580/3)*2){
           
//  println("cont not move")
           }
else{
        
for(g in cards){
            
if(g.recX-cardBlank.x==580/3 and g.recY==cardBlank.y){
                g.recX
-=580/3;
                g.cardTextX
-=580/3;
                btx
+=580/3;
                
break;
                }

            }

             goNum
++;
      }

       }

        
if(e.code==KeyCode.VK_RIGHT){
       
if(cardBlank.x==10){
           
//  println("cont not move")
           }
else{
        
for(g in cards){
            
if(g.recX-cardBlank.x==-580/3 and g.recY==cardBlank.y){
                g.recX
+=580/3;
                g.cardTextX
+=580/3;
                btx
-=580/3;
                
break;
                }

            }

              goNum
++;
      }

       }

      checkComplete();
     
// showOk
 }

function checkComplete():Void
{
    var num
=0;
   
for(card in cards){
        
if(card.atRightPosition())then num++;
    }

    
if(num==8)then complete=true;
 }


class Card extends Group{
   
// public var  opac:Number;
    public var recX:Number;
    
public var recY:Number;
    
public var cardTextX:Number;
    
public var cardTextY:Number;
    
public var cardTextConten:Integer;
    var cardrec
= Rectangle {
        x: bind recX;
        y: bind recY;
        width: 
580/3, height: 580/3
        fill: Color.RED
        stroke:Color.BLACK
   }

    var cardTet
= Text {
            font : Font 
{
                size: 
90
                embolden:
true
            }

            x: bind cardTextX;,
            y: bind cardTextY;
            content: 
"{cardTextConten}";
        }

    override var content 
= bind [cardrec, cardTet];
    
//override var  opacity= bind cardsc;
    function getText():String{
         
return cardTet.content;
    }

    function atRightPosition():Boolean
{
         var nowP
= (this.recX-10)*3/580+1+((this.recY-100)*3/580)*3;
         var a:String
="{nowP}";
         var arr 
= a.split("[.]");
         
if(arr[1].equals("0"))then a=arr[0else a="{Integer.parseInt(arr[0])+1}";
         
return a==this.getText();
        }

    }

     
     Timeline 
{
          repeatCount:
1
          keyFrames : [
              KeyFrame 
{
                  time : 1s
                  canSkip : 
true
                   values : [
                     logot
=>140 tween Interpolator.LINEAR
                  ]
                  action: function()
{
                           cardstime.play();
                  }

              }

          ]
      }
.play();

Timeline 
{
    repeatCount: Timeline.INDEFINITE
    keyFrames : [
            at(10s)
{scorrt=>720 tween Interpolator.EASEIN}
    ]
}
.play();


var cardstime 
=Timeline {
          repeatCount:
1
          keyFrames : [
              KeyFrame 
{
                  time : 1s
                  canSkip : 
true
                   values : [
                     cardsc
=>1.0 tween Interpolator.EASEIN
                  ]
                  action: function() 
{
                      println(cardsc);
                          statetime.play();
                  }

              }

          ]
      }


var statetime 
= Timeline {
        repeatCount:
1
        keyFrames : 
{
            at(1s)
            
{
                statex
=>-580 tween Interpolator.LINEAR;
              }
;

            }
 }


只有注册用户登录后才能发表评论。


网站导航: