Posts mit dem Label PropertiesOnChange werden angezeigt. Alle Posts anzeigen
Posts mit dem Label PropertiesOnChange werden angezeigt. Alle Posts anzeigen

Sonntag, 7. September 2014

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

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 ;-)