gpt4 book ai didi

java - 使用 Rectangle 类创建图形

转载 作者:行者123 更新时间:2023-11-30 07:38:29 24 4
gpt4 key购买 nike

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

enter image description here

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

但是我没有得到我想要的。我必须仅使用 Rectangle 类中的 x 、 y 值和宽度/高度来创建此线段。

下面是代码:参见轮廓形状方法:

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

}


}

}

最佳答案

该图中有 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);

关于java - 使用 Rectangle 类创建图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35037928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com