简体   繁体   English

使用Rectangle类创建图形

[英]Create a figure using Rectangle class

I am trying to create a line segment which has trough and crust as shown below : 我正在尝试创建具有低谷和硬皮的线段,如下所示: 在此处输入图片说明

在此处输入图片说明

EDIT : Now I'm getting the line segment figure in outline window but not in the diagram editor 编辑:现在我在大纲窗口中而不是在图编辑器中获取线段图

However I'm not getting exactly what i wanted . 但是我没有得到我想要的东西。 I have to create this line segment only using x , y value and width/height from Rectangle class . 我只需要使用x , y value and width/height from Rectangle class创建此线段。

Below is the 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 an SCR to runtime to have another abstract superclass that would 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 for this edit part.
     * 
     * Body of this method does not depend on settings in generation model
     * so you may safely remove <i>generated</i> tag and 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 as content pane.
     * Respects layout one may have set for generated figure.
     * @param nodeShape instance of generated figure class
     * @generated
     */
    protected IFigure setupContentPane(IFigure nodeShape) {
        return nodeShape; // use nodeShape itself as 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 are 5 lines in that figure, but you're only drawing four. 该图中有5条线,但是您只画了4条线。 Plus you're adding the width to the y-value, which would draw the line too tall. 另外,您还要将宽度添加到y值,这会使线条太高。 You should be using half of the width to reach the middle of the rectangle 您应该使用一半的宽度才能到达矩形的中间

Since each line is connected to the previous one, you can draw the lines one by one, starting from where the previous one finished. 由于每条线都连接到上一条线,因此您可以从上一条线的结尾处开始一条一条地画线。

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

Here it's the complete solution. 这是完整的解决方案。

@mikeyq6's solution: @ mikeyq6的解决方案:

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

Rectangle r = getBounds().getShrinked(100, 80);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM