Freitag, 29. Juli 2016

Java object-orientated vs. Java with a functional touch

Since Java 8 it looks like you can use parts from the functional paradigmen in the object-orientated language. These parts are based on functional interfaces. Also lambda-expressions are just another syntax to implement functional interfaces. But this advantages is not as functional as it seems, because functional interfaces are bases - in the end - on messages which are send to objects, which is object-orientated.
So the only thing functional interfaces does is enable another syntax to implement object-calls, which in fact is much more shorter and readeble in some cases.

When I think about parts from functional paradigm in object-orientated languages, better performance and avoiding of side-effects comes to my mind. But if you work wich object-calls in functional interfaces, you will never gain this advantages.

So if you use the functional touch of java you have to take care about the functional implementation. Then you will gain this advantages.

Here is an example that shows that a functional implementation with native types is more performant that an object-orientated implementation. (Both in Java):

Object-Orientated

   Set set = new HashSet<>();
  for (int i = 0; i < 1000000; i++) {
   set.add(new AdditionInteger(i));
  }

  Iterator it = set.iterator();
  AdditionInteger z1 = it.next();
  long start = System.currentTimeMillis();
  for (int i = 1; i < 1000000; i++) {
            z1.add( it.next());
  }
  long end = System.currentTimeMillis();

  System.out.println(end - start);
  // tooks 30261 ms

  // What about itration
  it = set.iterator();
  start = System.currentTimeMillis();
  for (int i = 1; i < 1000000; i++) {
   it.next();
  }
  end = System.currentTimeMillis();

  System.out.println(end - start);
  // tooks 47 ms



Functional 

              
  long start = System.currentTimeMillis();
  for (int i = 0; i < 1000000; i++) {
   i + (i + 1);
  }
  long end = System.currentTimeMillis();

  System.out.println(end - start);
  // tooks 8301 ms

So the functional code just took a third of the time the object-orientated code needed.
The reason might be the object-calls and the handling of the value inside the object, where the add-method was called. In functional languages you also have a space where values are stores, especially when they are bound to variables. This is space is calles environment. The handling of variables and values are not included in this experiment.

Montag, 20. Juli 2015

JavaFX-3D-Editor - Update 12




It took a long time since the last commit.
You may say: Much time - many features
Or: Much time - big features

But it is: I had no time - so only small features.

The new Feature is the visibility of the edges of 3DObjects. But maybe I found some things that might be useful for the new 3D-models I intend to create.

JAR and sources: https://github.com/gundermann/JFX3D



Sonntag, 14. September 2014

JavaFX-3D-Editor - Update 11.1

This week I didn't commit new features, beacuse there where several bugs in the last update.

The bugfixes:

  •  API-comments to implement new painting tools
  • nested CGOs can be saved

JAR and sources: https://github.com/gundermann/JFX3D


Sonntag, 7. September 2014

JavaFx-3D-Editor - Update 11

It's been two weeks ago since you heard something of the Fx3De from myself.


The reason is that I tried to make the code of the program a bit more comfortable in order to make it easyer to implement new painting tools.
During this I change the notation of the .dxml-files. 

If you had already saved some 3d-models - sorry.
Joke! - If you have a .dxml-file you could use them with this update too.
The only thing you have to do for it, is to convert the files. 
Therefore you can use the converter-program I commited here: 
https://drive.google.com/file/d/0B1zC0qGiWz40ZDU2UUFMNXp4Mlk/edit?usp=sharing

Just choose your existing old .dxml-file and save it in the second step.
The editor will load the model with no difference.

Unfourtunally with this update you won't get any new tool.
But in this post you can read how to build your own painting tools:


If you want to try it - feel free to do it. But please sent me the code so that I can check this and give you the right to commit it ;-)

Have fun with the new update.

Source: https://github.com/gundermann/JFX3D

How to create new painting tools on Fx3De

Here I want to explain to you how to implement new painting tools for the Fx3De.

There are almost  steps to get a additional painting tool. I will show an example. (Ellipse)

1. This is the main steps. Here you have to define your tool. So first of all you need a class which extends from AbstractPaintableObject3D. 


 public class Ellipse3D extends AbstractPaintableObject3D

After this add all umimpleneted methods.

@Override

 public void changeHeightTo(double d) {
  // TODO Auto-generated method stub

  }

  @Override
 public void changeWidthTo(double d) {
  // TODO Auto-generated method stub

  }

  @Override
 public void paint(double x, double y, double initinalX, double initinalY) {
  // TODO Auto-generated method stub

  }

  @Override
 public DoubleProperty getHeightProperty() {
  // TODO Auto-generated method stub
  return null;
 }

  @Override
 public DoubleProperty getWidthProperty() {
  // TODO Auto-generated method stub
  return null;
 }

  @Override
 public Object3DFactory getFactory() {
  // TODO Auto-generated method stub
  return null;
 }

  @Override
 protected Class getShapeClass() {
  // TODO Auto-generated method stub
  return null;
 }

Done this you have to define the behavior by filling the mehtods in this class. The description of the methods will help you to da this.

If you implement the methods you can define additional properties of your new painting tool.
You do this by annotations. Notice that every property needs a Property-Annotation and a PropertyChange-Annotation. Consequently you need two methods. If you are not familliar with the Property-Framework please read this post:
http://javafxstuff.blogspot.de/2014/08/properiesonchange.html

Here is the example:

@Override

 public void changeHeightTo(double d) {
  // TODO Auto-generated method stub

  }

  @Override
 public void changeWidthTo(double d) {
  // TODO Auto-generated method stub

  }

  @Override
 public void paint(double x, double y, double initinalX, double initinalY) {
  // TODO Auto-generated method stub

  }

  @Override
 public DoubleProperty getHeightProperty() {
  // TODO Auto-generated method stub
  return null;
 }

  @Override
 public DoubleProperty getWidthProperty() {
  // TODO Auto-generated method stub
  return null;
 }

  @Override
 public Object3DFactory getFactory() {
  // TODO Auto-generated method stub
  return null;
 }

  @Override
 protected Class getShapeClass() {
  // TODO Auto-generated method stub
  return null;
 }


2. Implement a Factory. The Factory needs to entends the Object3DFactory. Just implement the unimplemented methods and the getInstance method:

public class Ellipse3DFactory extends Object3DFactory {

  private static Object3DFactory _instance;

  @Override
 public Object3D createPlainObject3D() {
  return new Ellipse3D();
 }

  public static Object3DFactory getInstance() {
  if(_instance == null)
   _instance = new Ellipse3DFactory();
  return _instance;
 }

  @Override
 public String getType() {
  return "Ellipse";
 }

}




3. Set up a Painting-Value for the new painting tool in the Painting-Enum:

Rectangle, Ellipse, None;


4. Connect the Painting-Value from the Painting-Enum with the Factory. You do this in the FactoryPaintingAssoziator. Just extends the map in the method getFactoryFromPaintingMode:

public Map getFactoryFormPaintingMode() {
  Map paintingStartListener = new HashMap();
  paintingStartListener.put(Painting.Rectangle,
    Rectangle3DFactory.getInstance());
  paintingStartListener.put(Painting.Ellipse,
    Ellipse3DFactory.getInstance());
  return paintingStartListener;
 }


6. To persist the new painting tool you also have to connect the factory with a preference-type. You do this by extendsing the map in the method getFactoryFromPreference of the FactoryPreferenceTypeAssoziator:


public Object3DFactory getFactoryFormPreference(String type) {
  Map paintingStartListener = new HashMap();
  paintingStartListener
    .put("Rectangle", Rectangle3DFactory.getInstance());
  paintingStartListener.put("Ellipse", Ellipse3DFactory.getInstance());
  paintingStartListener.put("complex",
    ComplexObject3DFactory.getInstance());
  return paintingStartListener.get(type);
 }


Notice that the String refered to the Factory has to be equal to the value returned by the method getType in the factory-class.

7. UI changes.
To choose your new painting tool you have to change the UI. So open the paintingmenu.fxml with the SceneBuilder and add a new button. During that son't forgett to define an On-Action-Method for this button. Done this you have to implement the On-Action-Method in the PaintingMenuController:

@FXML
 public void selectPaintEllipse() {
  actualPaintingArea.initPainting(Painting.Ellipse);
 }


I hope it works :-D
If not please sent me an email: gundermann.niels.ng@googlemail.com

Sonntag, 24. August 2014

JavaFX-3D-Editor - Update 10

Hey hey, this is update 10 of the JavaFX-3D-Editor (Fx3De).

With this update you got two new features. 



  1.  A loading bar in the menu bar.
  2.  The ability to zoom into the painting area.


Check out the sources and try it: github.com/gundermann/JFX3D

For a whole list of features check out the full description: javafxstuff.blogspot.de/p/javafx-3d-editor.html

Dienstag, 19. August 2014

ProperiesOnChange

This little feature is an extraction of my JavaFX-3D-Editor you can see on the post: http://javafxstuff.blogspot.de/p/javafx-3d-editor.html

It is about Objects including properties. Properties may has to be changed during the execution of a program. JavaFX gives a nice implementation for properties. One benefit of them is binding. That means that you can bind two properties. Let's say we bind property A to B.
If I change the value of property B, the value of A will be updated by the JavaFX Framework. I use this feature very often.
This little implementation (I don't want to call it framework, because it too small) provides a UI to change properties of an object. This properties can be bind to an other property and will be updated as soon as you change the property in the UI.

You can find the implementation on: https://drive.google.com/file/d/0B1zC0qGiWz40U2hWc0oyQWlLNEk/edit?usp=sharing

Just include the jar-file into your build-path.

The following little example shows the using of the PropertiesOnChange.



  1. Include the jar-file into your build-path.
  2. Set up you Class including the properties
    public class TestPropertyObject {
    
     DoubleProperty testProperty = new SimpleDoubleProperty();
    
     @Property(hasChildren=false, name="test")
     public DoubleProperty getTestProperty() {
      return testProperty;
     }
    
     @PropertyChange(hasChildren = false, name = "test")
     public void setTestProperty(double value) {
      this.testProperty.set(value);
      System.out.println(getTestProperty().get());
     }
    }
    
  3. Include the UI into a stage and set up the property-object to the UI.
    public class Lauch extends Application {
    
     @Override
     public void start(Stage stage) throws Exception {
      GUIManipulatingMenu guiManipulatingMenu = new GUIManipulatingMenu();
      guiManipulatingMenu.setPropertyObject(new TestPropertyObject());
      
      Scene s = new Scene(guiManipulatingMenu);
      stage.setScene(s);
      stage.show();
    
     }
     
     public static void main(String[] args) {
      launch(args);
     }
    
  4. Do some changes!


Give me some feedback, please ;-)