添加位置

  • 首先需要在net/sf/openrocket/rocketcomponent下建立对应的java类,

    • 类的内容可以参考其目录下的Glider.java进行参考。
  • 然后添加界面:

    • core/resources/l10n/messages_zh_CN.properties文件中插入需要添加图标的ASCII码和对应的类名。

      1
      2
      compaddbuttons.Glider                  = \u6ed1\u7fd4\u673a
      compaddbuttons.Cavitation = \u5706\u76d8\u7a7a\u5316\u5668```
    • core/resources/l10n/messages.properties文件中插入对应的类别(一般为Body Tube)。

      1
      2
      compaddbuttons.Glider = Body tube
      compaddbuttons.Cavitation = Body tube```
  • 然后添加逻辑:

    • 首先找到swing/src/net/sf/openrocket/gui/main/ComponentAddButtons.java文件。

    • protected void fireActionPerformed(ActionEvent event)方法下找到RocketComponent component;下面的try代码,然后仿造这个try写一个新的try逻辑,其中if中的build函数后面要更换,这里我们假设其为f函数。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      RocketComponent component;
      try {

      //待添加的组件
      component = (RocketComponent) constructor.newInstance();
      if (component.getClass() == Glider.class) {
      //获取当前选择的组件 默认为:主发动机
      //获取当前组件的树形结构
      build(OpenRocketDocumentFactory.mydoc.getRocket(),0);
      }
      } catch (InstantiationException e) {
      throw new BugException("Could not construct new instance of class " + constructor, e);
      } catch (IllegalAccessException e) {
      throw new BugException("Could not construct new instance of class " + constructor, e);
      } catch (InvocationTargetException e) {
      throw Reflection.handleWrappedException(e);
      }```
    • 模仿,找到下面代码中有Glider名称的那几个if判定,将我们新加的组件的逻辑加入这几个if判定中去。

      1
      2
      3
      4
      5
      6
      if (component.getClass() != Glider.class && component.getClass() != Cavitation.class ) {
      if (position == null)
      c.addChild(component);
      else
      c.addChild(component, position);
      }
      1
      2
      3
      4
      // Select new component and open config dialog
      if (component.getClass() != Glider.class && component.getClass() != Cavitation.class) {
      selectionModel.setSelectionPath(ComponentTreeModel.makeTreePath(component));
      }
      1
      2
      3
      4
      if (component.getClass() != Glider.class && component.getClass() != Cavitation.class) {

      ComponentConfigDialog.showDialog(parent, document, component, false, true);
      }
    • 最后,在文件的最后,参照build方法构建自己的f函数。这里的操作是将组件插入第0级的位置,被插入的组件可以在这里定义参数,可以插入多个组件,可以命名新的级别,(对着写就好了)

      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
      31
      32
      33
      34
      35
      36
      public void build(RocketComponent component,int flag){
      System.out.println(component);
      //头锥
      try {
      //stage
      Constructor<AxialStage> stageConstructor = AxialStage.class.getConstructor();
      AxialStage stage = stageConstructor.newInstance();
      Constructor<NoseCone> coneConstructor = NoseCone.class.getConstructor();
      //头锥
      NoseCone noseCone = coneConstructor.newInstance();
      //箭体
      Constructor<BodyTube> bodyTubeConstructor = BodyTube.class.getConstructor();
      BodyTube bodyTube = bodyTubeConstructor.newInstance();
      //箭体2
      Constructor<BodyTube> bodyTubeConstructor2 = BodyTube.class.getConstructor();
      BodyTube bodyTube2 = bodyTubeConstructor2.newInstance();
      //自由曲面稳定
      Constructor<FreeformFinSet> freeformFinSetConstructor = FreeformFinSet.class.getConstructor();
      FreeformFinSet freeformFinSet = freeformFinSetConstructor.newInstance();
      //椭圆
      Constructor<EllipticalFinSet> ellipticalFinSetConstructor = EllipticalFinSet.class.getConstructor();
      EllipticalFinSet ellipticalFinSet = ellipticalFinSetConstructor.newInstance();
      bodyTube.addChild(ellipticalFinSet);
      bodyTube2.addChild(freeformFinSet);
      stage.addChild(noseCone,flag);
      stage.addChild(bodyTube,flag+1);
      stage.addChild(bodyTube2,flag+2);
      stage.setName("滑翔机");
      component.addChild(stage,0);

      // children.add(flag+1,bodyTube);
      } catch (Exception e) {
      e.printStackTrace();
      }

      }