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

Keine Kommentare:

Kommentar veröffentlichen