java - Create a figure using Rectangle class -


i trying create line segment has trough , crust shown below : enter image description here

enter image description here

edit : i'm getting line segment figure in outline window not in diagram editor

however i'm not getting wanted . have create line segment using x , y value , width/height rectangle class.

below code : see outline shape method :

package pipenet.diagram.edit.parts;  import org.eclipse.draw2d.graphics; import org.eclipse.draw2d.shape; import org.eclipse.draw2d.geometry.rectangle; import org.eclipse.swt.swt;   public class line3 extends shape {     public line3() {         this.setantialias(swt.on);           }     @override     protected void fillshape(graphics graphics) {         graphics.pushstate();                graphics.setfillrule(swt.fill_winding);         graphics.popstate();     }      @override     protected void outlineshape(graphics graphics) {                 rectangle r = getbounds().getcopy();          // v 1          graphics.drawline(r.x, r.y+r.height, r.x, r.y);     graphics.drawline(r.x, r.y, r.x+r.width/2, r.y);      graphics.drawline(r.x+r.width/2, r.y, r.x+r.width/2, r.y+r.height);      graphics.drawline(r.x+r.width/2, r.y+r.height, r.x+r.width, r.y+r.height);      graphics.drawline(r.x+r.width, r.y+r.height, r.x+r.width, r.y);      }       @override     public void paintfigure(graphics graphics) {         // todo auto-generated method stub         super.paintfigure(graphics);             }   } 

edit part class :

package pipenet.diagram.edit.parts;  import java.util.arraylist; import java.util.collections; import java.util.linkedlist; import java.util.list;  import org.eclipse.draw2d.flowlayout; import org.eclipse.draw2d.graphics; import org.eclipse.draw2d.ifigure; import org.eclipse.draw2d.positionconstants; import org.eclipse.draw2d.rectanglefigure; import org.eclipse.draw2d.shape; import org.eclipse.draw2d.stacklayout; import org.eclipse.draw2d.geometry.dimension; import org.eclipse.gef.editpart; import org.eclipse.gef.editpolicy; import org.eclipse.gef.graphicaleditpart; import org.eclipse.gef.commands.command; import org.eclipse.gef.editpolicies.layouteditpolicy; import org.eclipse.gef.editpolicies.nonresizableeditpolicy; import org.eclipse.gef.handles.movehandle; import org.eclipse.gef.requests.createrequest; import org.eclipse.gmf.runtime.diagram.ui.editparts.abstractborderedshapeeditpart; import org.eclipse.gmf.runtime.diagram.ui.editparts.iborderitemeditpart; import org.eclipse.gmf.runtime.diagram.ui.editparts.igraphicaleditpart; import org.eclipse.gmf.runtime.diagram.ui.editpolicies.borderitemselectioneditpolicy; import org.eclipse.gmf.runtime.diagram.ui.editpolicies.editpolicyroles; import org.eclipse.gmf.runtime.diagram.ui.editpolicies.flowlayouteditpolicy; import org.eclipse.gmf.runtime.diagram.ui.editpolicies.resizableshapeeditpolicy; import org.eclipse.gmf.runtime.diagram.ui.figures.borderitemlocator; import org.eclipse.gmf.runtime.draw2d.ui.figures.polylineconnectionex; import org.eclipse.gmf.runtime.emf.type.core.ielementtype; import org.eclipse.gmf.runtime.gef.ui.figures.defaultsizenodefigure; import org.eclipse.gmf.runtime.gef.ui.figures.nodefigure; import org.eclipse.gmf.runtime.notation.view; import org.eclipse.swt.graphics.color;  import pipenet.diagram.edit.policies.transitionitemsemanticeditpolicy; import pipenet.diagram.part.pipenetvisualidregistry; import pipenet.diagram.providers.pipenetelementtypes;  /**  * @generated  */ public class transitioneditpart extends abstractborderedshapeeditpart {      /**      * @generated      */     public static final int visual_id = 2001;      /**      * @generated      */     protected ifigure contentpane;      /**      * @generated      */     protected ifigure primaryshape;      /**      * @generated      */     public transitioneditpart(view view) {         super(view);     }      /**      * @generated      */     protected void createdefaulteditpolicies() {         super.createdefaulteditpolicies();         installeditpolicy(editpolicyroles.semantic_role,                 new transitionitemsemanticeditpolicy());         installeditpolicy(editpolicy.layout_role, createlayouteditpolicy());         // xxx need scr runtime have abstract superclass let children add reasonable editpolicies         // removeeditpolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.editpolicyroles.connection_handles_role);     }      /**      * @generated      */     protected layouteditpolicy createlayouteditpolicy() {          flowlayouteditpolicy lep = new flowlayouteditpolicy() {              protected editpolicy createchildeditpolicy(editpart child) {                 view childview = (view) child.getmodel();                 switch (pipenetvisualidregistry.getvisualid(childview)) {                 case transitionnameeditpart.visual_id:                     return new borderitemselectioneditpolicy() {                          protected list createselectionhandles() {                             movehandle mh = new movehandle(                                     (graphicaleditpart) gethost());                             mh.setbackgroundcolor(new color(null, 20 ,80 , 90));                             mh.getbackgroundcolor();                             system.out.print(mh.getbackgroundcolor());                             mh.setborder(null);                             return collections.singletonlist(mh);                         }                     };                 }                  editpolicy result = child                         .geteditpolicy(editpolicy.primary_drag_role);                 if (result == null) {                     result = new nonresizableeditpolicy();                 }                 return result;                 //return super.createchildeditpolicy(child);             }              protected command createaddcommand(editpart child, editpart after) {                 return null;             }              protected command createmovechildcommand(editpart child,                     editpart after) {                 return null;             }              protected command getcreatecommand(createrequest request) {                 return null;             }         };         return lep;     }      /**      * @generated      */     protected ifigure createnodeshape() {         return primaryshape = new line3();     }      /**      * @generated      *       *       */  //  public transitionfigure getprimaryshape() { //      return (transitionfigure) primaryshape; //  }      /**      * @generated      */     protected void addborderitem(nodefigure borderitemcontainer,             iborderitemeditpart borderitemeditpart) {         if (borderitemeditpart instanceof transitionnameeditpart) {             borderitemlocator locator = new borderitemlocator(getmainfigure(),                     positionconstants.bottom);             locator.setborderitemoffset(new dimension(-2, -2));             borderitemcontainer.add(borderitemeditpart.getfigure(), locator);         } else {             super.addborderitem(borderitemcontainer, borderitemeditpart);         }     }      /**      * @generated //   */     protected nodefigure createnodeplate() {         defaultsizenodefigure result = new defaultsizenodefigure(240, 240);         result.setbackgroundcolor(new color(null , 90 , 90 , 90));         return result;     }      /**      * creates figure edit part.      *       * body of method not depend on settings in generation model      * may safely remove <i>generated</i> tag , modify it.      *       * @generated      */     protected nodefigure createmainfigure() {         nodefigure figure= createnodeplate();         figure.setlayoutmanager(new stacklayout());         ifigure shape = createnodeshape();         figure.add(shape);         contentpane = setupcontentpane(shape);         return figure;     }      /**      * default implementation treats passed figure content pane.      * respects layout 1 may have set generated figure.      * @param nodeshape instance of generated figure class      * @generated      */     protected ifigure setupcontentpane(ifigure nodeshape) {         return nodeshape; // use nodeshape contentpane     }      /**      * @generated      */     public ifigure getcontentpane() {         if (contentpane != null) {             return contentpane;         }         return super.getcontentpane();     }      /**      * @generated      */     protected void setforegroundcolor(color color) {         if (primaryshape != null) {             primaryshape.setforegroundcolor(color);         }     }      /**      * @generated      */     protected void setbackgroundcolor(color color) {         if (primaryshape != null) {             primaryshape.setbackgroundcolor(color);         }     }      /**      * @generated      */     protected void setlinewidth(int width) {         if (primaryshape instanceof shape) {             ((shape) primaryshape).setlinewidth(width);         }     }      /**      * @generated      */     protected void setlinetype(int style) {         if (primaryshape instanceof shape) {             ((shape) primaryshape).setlinestyle(style);         }     }      /**      * @generated      */     public editpart getprimarychildeditpart() {         return getchildbysemantichint(pipenetvisualidregistry                 .gettype(transitionnameeditpart.visual_id));     }      /**      * @generated      */     public list/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/getmareltypesonsource() {         arraylist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/types = new arraylist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/(                 1);         types.add(pipenetelementtypes.arch_4001);         return types;     }      /**      * @generated      */     public list/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/getmareltypesonsourceandtarget(             igraphicaleditpart targeteditpart) {         linkedlist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/types = new linkedlist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/();         if (targeteditpart instanceof pipenet.diagram.edit.parts.transitioneditpart) {             types.add(pipenetelementtypes.arch_4001);         }         if (targeteditpart instanceof placeeditpart) {             types.add(pipenetelementtypes.arch_4001);         }         return types;     }      /**      * @generated      */     public list/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/getmatypesfortarget(             ielementtype relationshiptype) {         linkedlist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/types = new linkedlist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/();         if (relationshiptype == pipenetelementtypes.arch_4001) {             types.add(pipenetelementtypes.transition_2001);             types.add(pipenetelementtypes.place_2002);         }         return types;     }      /**      * @generated      */     public list/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/getmareltypesontarget() {         arraylist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/types = new arraylist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/(                 1);         types.add(pipenetelementtypes.arch_4001);         return types;     }      /**      * @generated      */     public list/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/getmatypesforsource(             ielementtype relationshiptype) {         linkedlist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/types = new linkedlist/*[org.eclipse.gmf.runtime.emf.type.core.ielementtype]*/();         if (relationshiptype == pipenetelementtypes.arch_4001) {             types.add(pipenetelementtypes.transition_2001);             types.add(pipenetelementtypes.place_2002);         }         return types;     }  //  @override //  public editpolicy getprimarydrageditpolicy() { //      resizableshapeeditpolicy editpolicy = new resizableshapeeditpolicy(); //      editpolicy.setresizedirections(positionconstants.south); //      editpolicy.setresizedirections(positionconstants.north); //      //editpolicy.setdragallowed(false); //       //      return editpolicy; //  }      /**      * @generated      */     public class transitionfigure extends line3{          /**          * @generated          */           public transitionfigure() {              flowlayout layoutthis = new flowlayout();             layoutthis.setstretchminoraxis(true);             layoutthis.setminoralignment(flowlayout.align_lefttop);              layoutthis.setmajoralignment(flowlayout.align_lefttop);             layoutthis.setmajorspacing(15);             layoutthis.setminorspacing(15);             layoutthis.sethorizontal(true);              this.setlayoutmanager(layoutthis);          }       }  } 

there 5 lines in figure, you're drawing four. plus you're adding width y-value, draw line tall. should using half of width reach middle of rectangle

since each line connected previous one, can draw lines 1 one, starting previous 1 finished.

how this:

graphics.drawline(r.x, r.y+r.height, r.x, r.y); graphics.drawline(r.x, r.y, r.x+r.width/2, r.y);  graphics.drawline(r.x+r.width/2, r.y, r.x+r.width/2, r.y+r.height);  graphics.drawline(r.x+r.width/2, r.y+r.height, r.x+r.width, r.y+r.height);  graphics.drawline(r.x+r.width, r.y+r.height, r.x+r.width, r.y);  

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -