Sunday, September 14, 2008

JavaFX Presentation

Today we had a good BOJUG meet at Thoughtworks. I have presented my small JavaFX slides. Followed to that, Sathish has given a presentation on Bean Binding and Bean Validation. He talked about JSR 295 + JSR 303. Quite a handsome presentation. After this, Sriram has taken the presentation on Tomcat internal and it seems he really went into internal. He talked about how to write own ClassLoader in tomcat + how to provide a webapps on fly, many more. It was a nice presentation without presentation slides :-).

Here is my presentation. Please feel free to comment.

Filling eyes effect in JavaFX

This is one simulation of Golden Eyes :-D(with an ugly face). I tried to make one use of Gaussian Blur which is applied in the white color of eyes. Adding this spot in the eyes gives a real simulation.



package eyes;

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.geometry.Arc;
import javafx.scene.geometry.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.*;
import javafx.scene.effect.*;

var y: Number = 150;
Frame {
title: "Golden Eyes"
width: 500
height: 500
closeAction: function() { java.lang.System.exit( 0 );
}
visible: true

stage: Stage {
fill: Color.GRAY;
content: [

Circle {
centerX: 160, centerY: y
radius: 23
fill: LinearGradient {
startX: 0.0
startY: 0.0
endX: 1.0
endY: 0.0
proportional: true
stops: [
Stop { offset: 0.0 color: Color.GOLD },
Stop { offset: 1.0 color: Color.BLACK }
]
}
opacity: 0.9
},

Circle {
centerX: 160, centerY: y
radius: 10
fill: Color.BLACK
},
Circle {
centerX: 166, centerY: y - 5
radius: 5
fill: Color.WHITE;
effect : GaussianBlur {
radius: 6
}
},
Circle {
centerX: 250, centerY: y
radius: 23
fill: LinearGradient {
startX: 0.0
startY: 0.0
endX: 1.0
endY: 0.0
proportional: true
stops: [
Stop { offset: 0.0 color: Color.BLACK },
Stop { offset: 1.0 color: Color.GOLD }
]
}
opacity: 0.2
},


Circle {
centerX: 250, centerY: y
radius: 10
fill: Color.BLACK
},
Circle {
centerX: 244, centerY: y - 5
radius: 5
fill: Color.WHITE;
effect : GaussianBlur {
radius: 6
}
},
Circle {
centerX: 200, centerY: 180
radius: 100
fill: Color.SIENNA
opacity: 0.1

},
Polyline {
stroke:Color.BLACK
points: [
200,160,
220.0,220.0,
180.0,220.0
]
},
Path {
fill: LinearGradient {
startX: 0.0
startY: 0.0
endX: 1.0
endY: 0.0
proportional: true
stops: [
Stop { offset: 0.0 color: Color.BLACK },
Stop { offset: 1.0 color: Color.RED }
]
}
elements: [
MoveTo { x: 160 y: 240 },
ArcTo { x: 250 y: 240 radiusX: 100 radiusY: 100},

]
},

]
}
}

Tracing the ball path in Java FX !

Alright, So now again one post for JavaFX. Finally I am able to write tracing path code in JavaFX. I have seen this in lot of Physics Motions where they show motion with tracing effect. Have a look at the output :


package tracemotion;

import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.geometry.*;
import javafx.scene.effect.*;
import javafx.scene.paint.*;

import java.lang.System;
import java.lang.Math;

var t : Number = 0.0;

Frame {
var input : TracingBall = TracingBall {};
stage : Stage {
fill: Color.BLACK;
content : bind [
input
]
}

visible : true
title : "Tracing Ball"
width : 600
height : 600
closeAction : function() { java.lang.System.exit( 0 ); }
}

class TracingBall extends CustomNode {

attribute tracingballs : Circle[];
attribute length : Integer = 600;
attribute timer : Timeline = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames :
KeyFrame {
time : 100ms
action : function() {
update();
t = t+0.3;
}
}
}
public function update() : Void {
for( i in [0..length - 30] ) {
tracingballs[i].centerX = tracingballs[i+30].centerX;
tracingballs[i].centerY = tracingballs[i+30].centerY;
tracingballs[i].radius = tracingballs[i+30].radius;
tracingballs[i].opacity=0.4;
}
tracingballs[length] = Circle {
centerX : bind(100 + t * 30),
centerY : (300 + Math.cos(t) * 100),
radius : 30,
fill : bind LinearGradient {
proportional: true
stops: [
Stop { offset: 0.0 color: Color.RED },
Stop { offset: 1.0 color: Color.GAINSBORO },
]
},
opacity : 1.0

};
}
public function create(): Node {
return Group {
content : bind[tracingballs]
};
}
init {
for( i in [0..length] ) {
insert Circle { fill : bind LinearGradient {
proportional: true
stops: [
Stop { offset: 0.0 color: Color.RED },
Stop { offset: 1.0 color: Color.GAINSBORO },
]
},
} into tracingballs;

}
timer.start();
}
}

Some lines in code are filling those fancy colors in ball(some lines are hard coded as well).I am a real bad guy in filling nice catchy colors(this color looks like a sun and a moon combination). Any comments/improvements are welcome !

2+2=4 and so 2*2

Last week, our office celebrates OpenSource Mela. In code war, we got a programming contest in which the problem statement is :

5 * 4 / 2 - 5 + 2 = 7 (evaluated from left to right). So, input format is:

5 4 2 5 2 7

and output is to put into a valid expression format(all possible format). So, if its 2 2 4, then 2+2=4 and 2*2=4 is possible. I have written some code for this, which goes here :

package problemstatement1;

import java.util.List;

public class Main {

String input = "2 0 2";
String output = new String(); // 5 * 4 / 2 - 5 + 2 = 7
int resultVal = 0;
String operatorseq = new String();
int result = 0;
int totalcounter = 0;
boolean flag = true;

// converting input into easy format

String[] inputtoken = input.split(" ");
int[] numberseq = new int[inputtoken.length - 1];
int totaloperator = numberseq.length - 1;

public void isValid() {
if (inputtoken.length < 3) {
System.out.println("Usages ... Input Parameter should be three or more ");
System.exit(0);
}
}

public void processInput() {
try {
resultVal = Integer.parseInt(inputtoken[inputtoken.length - 1]);
} catch(NumberFormatException e) {
System.out.println("Parsing error" + " " + e);
System.exit(0);
}
for (int i = 0; i < numberseq.length; i++) {
try {
numberseq[i] = Integer.parseInt(inputtoken[i]);
} catch(NumberFormatException e) {
System.out.println("Parsing error" + " " + e);
System.exit(0);
}
}
}

public void showInput() {
for (int i = 0; i < numberseq.length; i++) {
// System.out.println(numberseq[i]);
}
}

public void getPermutation() {
GenerateOperators gen = new GenerateOperators("+-*/", totaloperator);
List v = gen.getVariations();
System.out.println("Possible Solutions");
for (int i = 0; i < v.size(); i++) {
operatorseq = v.get(i);
manupulate();
}
}

public void manupulate() {

result = 0;

for (int i = 0; i < operatorseq.length(); i++) {
if (i == 0) {
if ((operatorseq.charAt(i)) == '+') {
result = result + (numberseq[i] + numberseq[i + 1]);
}
if ((operatorseq.charAt(i)) == '-') {
result = result + (numberseq[i] - numberseq[i + 1]);
}
if ((operatorseq.charAt(i)) == '*') {
result = result + (numberseq[i] * numberseq[i + 1]);
}
if ((operatorseq.charAt(i)) == '/') {
try {
result = result + (numberseq[i] / numberseq[i + 1]);
} catch(Exception e) {
flag = false;
// don't do anything
}
}
} else {
if ((operatorseq.charAt(i)) == '+') {
result = result + numberseq[i + 1];
}
if ((operatorseq.charAt(i)) == '-') {
result = result - numberseq[i + 1];
}
if ((operatorseq.charAt(i)) == '*') {
result = result * numberseq[i + 1];
}
if ((operatorseq.charAt(i)) == '/') {
try {
result = result / numberseq[i + 1];
} catch(Exception e) {
flag = false;
// don't do anything
}
}
}
}
if (result == resultVal && flag == true) {
totalcounter++;
System.out.println("");
for (int i = 0; i < numberseq.length - 1; i++) {
System.out.print(numberseq[i] + "" + operatorseq.charAt(i));
}
System.out.print(numberseq[numberseq.length - 1]);
System.out.print("= " + result);
}
}

public void count() {
if(totalcounter == 0 ) {
System.out.println("NO EXPRESSION POSSIBLE");
System.exit(0);
}
else {
System.out.println("");
System.out.println("Total Possible Solution :" + totalcounter);
}
}
public static void main(String[] args) {
Main main = new Main();
main.isValid();
main.processInput();
main.showInput();
main.getPermutation();
main.count();
}
}


and one more file which is :

package problemstatement1;

import java.util.List;
import java.util.ArrayList;

public class GenerateOperators {

private String a;
private int n;

public GenerateOperators(String a, int n) {
this.a = a;
this.n = n;
}

public List getVariations() {
int l = a.length();
int permutations = (int) Math.pow(l, n);
char[][] storeCombination = new char[permutations][n];
for (int x = 0; x < n; x++) {
int temp = (int) Math.pow(l, x);
for (int p1 = 0; p1 < permutations;) {
for (int al = 0; al < l; al++) {
for (int p2 = 0; p2 < temp; p2++) {
storeCombination[p1][x] = a.charAt(al);
p1++;
}
}
}
}

List result = new ArrayList();
for (char[] permutation : storeCombination) {
result.add(new String(permutation));
}
return result;

}

public static void main(String[] args) {
GenerateOperators gen = new GenerateOperators("AAAMMBR", 7);
List v = gen.getVariations();
for (int i=0;i String s1 = v.get(i);
System.out.println(s1);
}

}
}

Feel free to use this code :-).