Java Code Examples for org.eclipse.swt.graphics.GC#drawPath()

The following examples show how to use org.eclipse.swt.graphics.GC#drawPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphUtils.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Paints an looping arc from scr to tgt.
 * <p/>
 * <b>Assumption:</b> The tgt is located right/below of the src.
 */
public static float[] arcSelf(GC gc, Point src, Point tgt) {
	Path path = new Path(gc.getDevice());
	int diffH = 10;
	int diff = diffH * 3;
	path.moveTo((int) src.x, (int) src.y);
	path.cubicTo(
			(int) src.x + diff, (int) src.y - diffH,
			(int) tgt.x, (int) tgt.y - diff,
			(int) tgt.x, (int) tgt.y);

	gc.drawPath(path);

	float[] pp = path.getPathData().points;
	return pp;
}
 
Example 2
Source File: PolygonQuadraticCurveIntersection.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected AbstractControllableShape createControllableShape2(
		Canvas canvas) {
	return new AbstractControllableShape(canvas) {
		@Override
		public void createControlPoints() {
			addControlPoint(new Point(100, 100));
			addControlPoint(new Point(300, 100));
			addControlPoint(new Point(300, 300));
		}

		@Override
		public QuadraticCurve createGeometry() {
			return new QuadraticCurve(getControlPoints());
		}

		@Override
		public void drawShape(GC gc) {
			QuadraticCurve c = createGeometry();
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(c.toPath())));
		}
	};
}
 
Example 3
Source File: EllipseCubicCurveIntersection.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected AbstractControllableShape createControllableShape2(
		Canvas canvas) {
	return new AbstractControllableShape(canvas) {
		@Override
		public void createControlPoints() {
			addControlPoint(new Point(100, 150));
			addControlPoint(new Point(400, 200));
			addControlPoint(new Point(300, 400));
			addControlPoint(new Point(550, 300));
		}

		@Override
		public CubicCurve createGeometry() {
			return new CubicCurve(getControlPoints());
		}

		@Override
		public void drawShape(GC gc) {
			CubicCurve c = createGeometry();
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(c.toPath())));
		}
	};
}
 
Example 4
Source File: EllipseQuadraticCurveIntersection.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected AbstractControllableShape createControllableShape2(
		Canvas canvas) {
	return new AbstractControllableShape(canvas) {
		@Override
		public void createControlPoints() {
			addControlPoint(new Point(100, 150));
			addControlPoint(new Point(400, 200));
			addControlPoint(new Point(550, 300));
		}

		@Override
		public QuadraticCurve createGeometry() {
			return new QuadraticCurve(getControlPoints());
		}

		@Override
		public void drawShape(GC gc) {
			QuadraticCurve c = createGeometry();
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(c.toPath())));
		}
	};
}
 
Example 5
Source File: GraphUtils.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Paints an arc from src to tgt using the given control point ctr. */
public static float[] arc(GC gc, Point ctr, Point src, Point tgt) {
	Path path = new Path(gc.getDevice());
	path.moveTo((int) src.x, (int) src.y);
	path.quadTo((int) ctr.x, (int) ctr.y, (int) tgt.x, (int) tgt.y);
	gc.drawPath(path);

	float[] pp = path.getPathData().points;
	return pp;
}
 
Example 6
Source File: GraphUtils.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Paints an arc from src to tgt.
 * <p/>
 * <b>Assumption:</b> The tgt is located below of the src.
 */
public static float[] arcReversed(GC gc, Point src, Point tgt) {
	Path path = new Path(gc.getDevice());
	int ydiff = (int) ((tgt.y - src.y) / 3);
	path.moveTo((int) src.x, (int) src.y);
	path.cubicTo((int) src.x, (int) src.y + ydiff, (int) tgt.x, (int) tgt.y - ydiff * 2, (int) tgt.x, (int) tgt.y);
	gc.drawPath(path);

	float[] pp = path.getPathData().points;
	return pp;
}
 
Example 7
Source File: CubicInterpolationExample.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ControllableShape[] getControllableShapes() {
	return new ControllableShape[] { new ControllableShape() {
		{
			/*
			 * These are the anchor points for the cubic Bezier
			 * interpolation.
			 */
			addControlPoints(new Point(100, 200), new Point(150, 250),
					new Point(200, 150), new Point(250, 250),
					new Point(300, 150), new Point(350, 250),
					new Point(400, 200));
		}

		@Override
		public PolyBezier getShape() {
			/*
			 * Constructs the cubic Bezier interpolation through the defined
			 * anchor points as a PolyBezier.
			 */
			return PolyBezier.interpolateCubic(curveWidthCoefficient,
					getPoints());
		}

		@Override
		public void onDraw(GC gc) {
			/*
			 * Displays the cubic Bezier interpolation.
			 */

			// compute the interpolation
			PolyBezier curve = getShape();

			// display it as an SWT Path
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(curve.toPath())));
		}
	} };
}
 
Example 8
Source File: ConvexHullExample.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ControllableShape[] getControllableShapes() {
	return new ControllableShape[] { new ControllableShape() {
		{
			// These are the points which are displayed on the screen.
			// We will compute their convex hull later.
			addControlPoints(new Point(100, 100), new Point(150, 400),
					new Point(200, 300), new Point(250, 150),
					new Point(300, 250), new Point(350, 200),
					new Point(400, 350));
		}

		@Override
		public Polygon getShape() {
			// Compute the convex hull of the defined point list.
			// We return the convex hull as a Polygon.
			return new Polygon(Point.getConvexHull(getPoints()));
		}

		@Override
		public void onDraw(GC gc) {
			// This is the code to display the computed convex hull.

			// Compute the convex hull.
			Polygon convexHull = getShape();

			// Display the convex hull as an SWT Path.
			gc.drawPath(new org.eclipse.swt.graphics.Path(
					Display.getCurrent(),
					Geometry2SWT.toSWTPathData(convexHull.toPath())));
		}
	} };
}
 
Example 9
Source File: QuadraticCurvesIntersection.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private AbstractControllableShape createControllableQuadraticBezierCurveShape(
		Canvas canvas, Point... points) {
	final Point start = points[0];
	final Point ctrl = points[1];
	final Point end = points[2];

	return new AbstractControllableShape(canvas) {
		@Override
		public void createControlPoints() {
			addControlPoint(start);
			addControlPoint(ctrl);
			addControlPoint(end);
		}

		@Override
		public IGeometry createGeometry() {
			return new QuadraticCurve(getControlPoints());
		}

		@Override
		public void drawShape(GC gc) {
			QuadraticCurve curve = (QuadraticCurve) createGeometry();

			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(curve.toPath())));
		}
	};
}
 
Example 10
Source File: PolygonCubicCurveIntersection.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected AbstractControllableShape createControllableShape2(
		Canvas canvas) {
	return new AbstractControllableShape(canvas) {
		@Override
		public void createControlPoints() {
			addControlPoint(new Point(200, 100));
			addControlPoint(new Point(190, 310));
			addControlPoint(new Point(410, 90));
			addControlPoint(new Point(400, 300));
		}

		@Override
		public CubicCurve createGeometry() {
			Point[] controlPoints = getControlPoints();
			System.out.println("new CubicCurve(" + controlPoints[0] + ", "
					+ controlPoints[1] + ", " + controlPoints[2] + ", "
					+ controlPoints[3] + ")");
			return new CubicCurve(controlPoints);
		}

		@Override
		public void drawShape(GC gc) {
			CubicCurve c = createGeometry();
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(c.toPath())));
		}
	};
}
 
Example 11
Source File: CubicCurvesIntersection.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private AbstractControllableShape createControllableCubicBezierCurveShape(
		Canvas canvas, Point... points) {
	final Point start = points[0];
	final Point ctrl1 = points[1];
	final Point ctrl2 = points[2];
	final Point end = points[3];

	return new AbstractControllableShape(canvas) {
		@Override
		public void createControlPoints() {
			addControlPoint(start);
			addControlPoint(ctrl1);
			addControlPoint(ctrl2);
			addControlPoint(end);
		}

		@Override
		public IGeometry createGeometry() {
			return new CubicCurve(getControlPoints());
		}

		@Override
		public void drawShape(GC gc) {
			CubicCurve curve = (CubicCurve) createGeometry();

			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(curve.toPath())));
		}
	};
}
 
Example 12
Source File: DisplayOverlay.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
void paintScale(final GC gc) {
	gc.setBackground(IGamaColors.BLACK.color());
	final int BAR_WIDTH = 1;
	final int BAR_HEIGHT = 8;
	final int x = 0;
	final int y = 0;
	final int margin = 20;
	final int width = scalebar.getBounds().width - 2 * margin;
	final int height = scalebar.getBounds().height;
	final int barStartX = x + 1 + BAR_WIDTH / 2 + margin;
	final int barStartY = y + height - BAR_HEIGHT / 2;

	final Path path = new Path(WorkbenchHelper.getDisplay());
	path.moveTo(barStartX, barStartY - BAR_HEIGHT + 2);
	path.lineTo(barStartX, barStartY + 2);
	path.moveTo(barStartX, barStartY - BAR_HEIGHT / 2 + 2);
	path.lineTo(barStartX + width, barStartY - BAR_HEIGHT / 2 + 2);
	path.moveTo(barStartX + width, barStartY - BAR_HEIGHT + 2);
	path.lineTo(barStartX + width, barStartY + 2);

	gc.setForeground(IGamaColors.WHITE.color());
	gc.setLineStyle(SWT.LINE_SOLID);
	gc.setLineWidth(BAR_WIDTH);
	gc.drawPath(path);
	gc.setFont(coord.getFont());
	drawStringCentered(gc, "0", barStartX, barStartY - 6, false);
	drawStringCentered(gc, getScaleRight(), barStartX + width, barStartY - 6, false);
	path.dispose();
}
 
Example 13
Source File: CubicCurveDeCasteljauExample.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected ControllableShape[] getControllableShapes() {
	return new ControllableShape[] { new ControllableShape() {
		{
			/*
			 * These are the control points used to construct the CubicCurve
			 * later.
			 */
			addControlPoints(new Point(100, 200), new Point(200, 100),
					new Point(300, 300), new Point(400, 200));
		}

		@Override
		public CubicCurve getShape() {
			/*
			 * Constructs the CubicCurve of the defined control points.
			 */
			return new CubicCurve(getPoints());
		}

		@Override
		public void onDraw(GC gc) {
			/*
			 * Draws the CubicCurve and the de Casteljau construction for
			 * the current parameter value.
			 */

			// Construct the CubicCurve from the defined control points.
			CubicCurve curve = getShape();

			// Draw the CubicCurve as an SWT Path.
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(curve.toPath())));

			/*
			 * Retrieve control points to compute the linear interpolations
			 * of the de Casteljau algorithm.
			 */
			Point[] points = getPoints();

			/*
			 * Define the colors for the intermediate lines. We have three
			 * stages and therefore three different colors for a cubic
			 * Bezier curve. This is the case, because the de Casteljau
			 * algorithm reduces the number of control points in each
			 * iteration until it reaches the actual point on the curve.
			 */
			int[] colors = new int[] { SWT.COLOR_DARK_GREEN, SWT.COLOR_BLUE,
					SWT.COLOR_DARK_RED };

			for (int ci = 0; ci < colors.length; ci++) {
				for (int i = 0; i < 3 - ci; i++) {
					// set line color
					gc.setForeground(Display.getCurrent()
							.getSystemColor(colors[ci]));

					// draw line
					gc.drawLine((int) points[i].x, (int) points[i].y,
							(int) points[i + 1].x, (int) points[i + 1].y);

					// interpolate point for the next iteration
					points[i] = new Line(points[i], points[i + 1])
							.get(parameterValue);

					// set color to black
					gc.setForeground(Display.getCurrent()
							.getSystemColor(SWT.COLOR_BLACK));

					// draw point
					gc.drawOval((int) (points[i].x - 2),
							(int) (points[i].y - 2), 4, 4);
				}
			}
		}
	} };
}
 
Example 14
Source File: BezierApproximationExample.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected ControllableShape[] getControllableShapes() {
	return new ControllableShape[] { new ControllableShape() {
		{
			/*
			 * These are the control points used to construct the
			 * BezierCurve below.
			 */
			addControlPoints(new Point(100, 200), new Point(150, 250),
					new Point(200, 150), new Point(250, 250),
					new Point(300, 150), new Point(350, 250),
					new Point(400, 200));
		}

		@Override
		public BezierCurve getShape() {
			/*
			 * Here, we construct the BezierCurve from the defined Points.
			 */
			return new BezierCurve(getPoints());
		}

		@Override
		public void onDraw(GC gc) {
			/*
			 * This is the code to display the BezierCurve via SWT.
			 */

			// Construct the Bezier curve.
			BezierCurve curve = getShape();

			// Display the BezierCurve as a Path.
			gc.drawPath(
					new org.eclipse.swt.graphics.Path(Display.getCurrent(),
							Geometry2SWT.toSWTPathData(curve.toPath())));

			// Display the connection line of its control points.
			gc.setLineStyle(SWT.LINE_DOT);
			gc.drawPolyline(Geometry2SWT
					.toSWTPointArray(new Polyline(curve.getPoints())));

		}
	} };
}
 
Example 15
Source File: PieUtils.java    From BiglyBT with GNU General Public License v2.0 2 votes vote down vote up
public static void
drawPie(
	GC gc,Image image, int x, int y,int width,int height,int percent, boolean draw_border )
{
	Rectangle image_size = image.getBounds();

	int	width_pad 	= ( width - image_size.width  )/2;
	int	height_pad 	= ( height - image_size.height  )/2;

    int angle = (percent * 360) / 100;
    if(angle<4){
    	angle = 0; // workaround fillArc rendering bug
    }

	Region old_clipping = new Region();

	gc.getClipping(old_clipping);

	Path path_done = new Path(gc.getDevice());

	path_done.addArc(x,y,width,height,90,-angle);
	path_done.lineTo( x+width/2, y+height/2);
	path_done.close();

	gc.setClipping( path_done );

	gc.drawImage(image, x+width_pad, y+height_pad+1);

	Path path_undone = new Path(gc.getDevice());

	path_undone.addArc(x,y,width,height,90-angle,angle-360);
	path_undone.lineTo( x+width/2, y+height/2);
	path_undone.close();

	gc.setClipping( path_undone );

	gc.setAlpha( 75 );
	gc.drawImage(image, x+width_pad, y+height_pad+1);
	gc.setAlpha( 255 );

	gc.setClipping( old_clipping );

	if ( draw_border ){

		gc.setForeground(Colors.blue);

		if ( percent == 100 ){

			gc.drawOval(x , y , width-1, height-1);

		}else{

			if ( angle > 0 ){

				gc.drawPath( path_done );
			}
		}
	}

	path_done.dispose();
	path_undone.dispose();
	old_clipping.dispose();

}