使用Rectangle类创建一个图形

时间:2016-01-27 12:51:39

标签: java line shape draw2d drawrectangle

我正在尝试创建一个具有槽和外壳的线段,如下所示:enter image description here

enter image description here

编辑:现在我在大纲窗口中获得了线段图,但没有在图编辑器中获得

然而,我并没有得到我想要的东西。我必须使用x , y value and width/height from Rectangle class创建此线段。

下面是代码:请参阅大纲形状方法:

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


}

编辑部分类:

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

        }


    }

}

2 个答案:

答案 0 :(得分:1)

该图中有5行,但你只画了4行。另外,您将宽度添加到y值,这会使线条太高。您应该使用宽度的一半来到达矩形的中间

由于每一行都连接到前一行,您可以逐行绘制这些行,从前一行开始。

这个怎么样:

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

答案 1 :(得分:0)

这是完整的解决方案。

@ mikeyq6&#39的解决方案:

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); 
  • 我的

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