Tuesday Aug 05, 2008

Reflection in JavaFX - Crying baby !

On the way to exploring little more API of JavaFX, I reached to a fair animation. That is reflection. Here is the output of "crying baby". I always scare of using cartoon character because those are Copyright Image. 

 And lots of guys kicked me in the last post because code size was too big, but now its Java FX and I can also show the power of scripting :-).  Here is the code:

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.geometry.Circle;
import javafx.scene.paint.Color;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.effect.\*;

Frame {
    title: "Crying Baby"
    width: 400
    height: 700
    closeAction: function() { java.lang.System.exit( 0 ); 
    }
    visible: true

    stage: Stage {
        fill: Color.WHITE
        content: [
            ImageView {
                x: 100
                y:10
                image: Image {
                    url: "http://www.powermixradio.com/GGG-SAD-CARTOON.jpg"
                }
                effect: Reflection { 
                    bottomOpacity:0.1
                    fraction: 1 
                    topOffset: 50
                }  
            },
            ImageView {
                x:0
                y:300
                image: Image {
                    url: "http://www.toyotapartsstore.com/images/71111_2_%20mirror_8CE1.jpg"
                }
                opacity:0.5
            }
        ] 
    }   
}


Things to watch:

1. Effect: Reflection -

bottomOpacity:0.1 - set the opacity of image in reflection.

fraction: 1 - set how much part of the image should come in reflection. 

topOffset: 50 - How far image from the focus. There are some more useful parameters in this.

2. Opacity of second Imageview allow me to see the reflection of first image, else it will cover the reflection.

On the way to make some moving animation, but I don't know what happened to the support of gif image. gif image works like static image, don't know why ?

Saturday Mar 22, 2008

Final Variables

It can be little surprising but final variables can be changed from JDK 1.5 onwards. How ? Via reflection API. I still need to dig more into the code, off course OpenJDK code :) to see how it is possible. Here is a small code to see how final variable can be changed with reflection API:

import java.lang.reflect.Field;
class Sample {
final int finalInt ;
final String finalString ;

Sample(int a , String s) {
this.finalInt = a;
this.finalString = s;
}
public String toString(){
return(finalInt + " \*\*" + finalString);
}
}

public class FinalTest {
public static void changeFinal(Sample f, String finalString, Object value)
throws NoSuchFieldException, IllegalAccessException  {
Field field = Sample.class.getDeclaredField(finalString);
field.setAccessible(true);
field.set(f, value);
}
public static void main(String[] args)throws Exception {
Sample ft = new Sample(10,"Hello World");
changeFinal(ft,"finalString","Hello");
System.out.println(ft);
}
}
Searching little on Web explained me that this behavior is not consistent throughout. JDK1.2 used to give the same output for this code, whereas JDK 1.3 and 1.4 throws IllegalAccessException. And now again JDK 1.5, you can change the variable. I will try to see more on this part.

About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today