下面列出了java.awt.geom.Path2D#moveTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void draw(DrawRequest r) {
Graphics2D g = r.getGraphics();
Decoration d = r.getDecoration();
Path2D shape = new Path2D.Double();
float w = (float) strokeWidth / 4.0f;
if (spanningTree == null) {
HashSet<Point2D> points = new HashSet<>();
Collection<VisualBundledTransition> transitions = ((VisualPolicy) r.getModel()).getTransitionsOfBundle(this);
for (VisualBundledTransition t: transitions) {
Point2D point = TransformHelper.getTransformToRoot(t).transform(t.getCenter(), null);
points.add(point);
}
spanningTree = buildSpanningTree(points);
}
for (Line2D l: spanningTree) {
shape.moveTo(l.getX1(), l.getY1());
shape.lineTo(l.getX2(), l.getY2());
}
g.setColor(ColorUtils.colorise(color, d.getColorisation()));
g.setStroke(new BasicStroke(w, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, new float[]{10 * w, 10 * w}, 0.0f));
g.draw(shape);
}
@Test
public void testLineRectangleIntersections() {
// Arrange
Path2D line = new Path2D.Double(Path2D.WIND_NON_ZERO, 3);
line.moveTo(1, 10);
line.lineTo(20 - 1, 10);
line.lineTo(20 - 1 + SMALL_DELTA, 10 + SMALL_DELTA);
line.closePath();
Rectangle2D.Double rectangle = new Rectangle2D.Double(4, 8, 4, 4);
// Act
Area intersectionArea = new Area(line);
intersectionArea.intersect(new Area(rectangle));
// Assert
assertThat(intersectionArea.isEmpty()).isFalse();
Rectangle2D bounds2D = intersectionArea.getBounds2D();
assertThat(bounds2D.getX()).isCloseTo(4d, offset(PRECISION));
assertThat(bounds2D.getY()).isCloseTo(10d, offset(PRECISION));
assertThat(bounds2D.getX() + bounds2D.getWidth()).isCloseTo(8d, offset(PRECISION));
assertThat(bounds2D.getY() + bounds2D.getHeight()).isCloseTo(10d, offset(PRECISION));
}
@Override public void mouseDragged(MouseEvent e) {
JList<?> l = (JList<?>) e.getComponent();
l.setFocusable(true);
Point destPoint = e.getPoint();
Path2D rb = getRubberBand();
rb.reset();
rb.moveTo(srcPoint.x, srcPoint.y);
rb.lineTo(destPoint.x, srcPoint.y);
rb.lineTo(destPoint.x, destPoint.y);
rb.lineTo(srcPoint.x, destPoint.y);
rb.closePath();
int[] indices = IntStream.range(0, l.getModel().getSize())
.filter(i -> rb.intersects(l.getCellBounds(i, i))).toArray();
l.setSelectedIndices(indices);
l.repaint();
}
/**
* Fills a polygon.
*
* @param g the graphics
* @param xPoints the x polygon points
* @param yPoints the y polygon points
* @param nPoints the number of points
* @param strokeType the stroke type
* @param strokeWidth the stroke width
* @param valueAA overrides current {@link RenderingHints#KEY_ANTIALIASING} to {@code valueAA}
*/
public static void fillPolygon(@Nonnull final Graphics2D g, double[] xPoints, double[] yPoints, int nPoints, StrokeType strokeType, double strokeWidth, @Nonnull Object valueAA) {
// [tav] todo: mind strokeWidth and strokeType
final Path2D path = new Path2D.Double(Path2D.WIND_EVEN_ODD);
path.moveTo(xPoints[0], yPoints[0]);
for (int p = 1; p < nPoints; p++) {
path.lineTo(xPoints[p], yPoints[p]);
}
path.closePath();
PaintUtil.paintWithAA(g, valueAA, new Runnable() {
@Override
public void run() {
g.fill(path);
}
});
}
/**
* Draws a sequence of connected lines defined by
* arrays of <i>x</i> and <i>y</i> coordinates.
* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.
* The figure is not closed if the first point
* differs from the last point.
* @param xPoints an array of <i>x</i> points
* @param yPoints an array of <i>y</i> points
* @param nPoints the total number of points
* @see java.awt.Graphics#drawPolygon(int[], int[], int)
* @since 1.1
*/
public void drawPolyline(int[] xPoints, int[] yPoints,
int nPoints) {
if (nPoints == 2) {
draw(new Line2D.Float(xPoints[0], yPoints[0],
xPoints[1], yPoints[1]));
} else if (nPoints > 2) {
Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD, nPoints);
path.moveTo(xPoints[0], yPoints[0]);
for(int i = 1; i < nPoints; i++) {
path.lineTo(xPoints[i], yPoints[i]);
}
draw(path);
}
}
private void drawFilledWedge(Point2d a, Point2d b, Point2d c, Graphics2D g) {
Path2D path = new Path2D.Double();
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.closePath();
g.fill(path);
}
/**
* Draw the graph with the heap samples. It is a simple area chart.
*
* @param g Where to draw
* @param width The width of the chart
* @param height The height of the chart
*/
private void paintSamples(Graphics2D g, int width, int height) {
Path2D path = new Path2D.Double();
path.moveTo(0, height);
for (int i = 0; i < GRAPH_COUNT; ++i) {
int index = (i + graphIndex) % GRAPH_COUNT;
double x = (double) i / (double) (GRAPH_COUNT - 1) * (double) width;
double y = (double) height * (1.0 - (double) graph[index] / (double) lastTotal);
path.lineTo(x, y);
}
path.lineTo(width, height);
path.closePath();
g.fill(path);
}
/**
* Get the path of the line string
*
* @param simplifyTolerance
* simplify tolerance in meters
* @param boundingBox
* @param transform
* @param lineString
*/
private Path2D getPath(double simplifyTolerance, BoundingBox boundingBox,
ProjectionTransform transform, LineString lineString) {
Path2D path = null;
// Try to simplify the number of points in the LineString
List<Point> lineStringPoints = simplifyPoints(simplifyTolerance,
lineString.getPoints());
for (Point point : lineStringPoints) {
Point projectedPoint = transform.transform(point);
float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
projectedPoint.getX());
float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
projectedPoint.getY());
if (path == null) {
path = new Path2D.Double();
path.moveTo(x, y);
} else {
path.lineTo(x, y);
}
}
return path;
}
private static void drawCellBusy(Graphics2D g, Coordinate dy, Coordinate dx) {
Path2D shape = new Path2D.Double();
shape.moveTo(dx.getValue() - 0.1, dy.getValue() - 0.1);
shape.lineTo(dx.getValue() + 0.1, dy.getValue() + 0.1);
shape.moveTo(dx.getValue() + 0.1, dy.getValue() - 0.1);
shape.lineTo(dx.getValue() - 0.1, dy.getValue() + 0.1);
g.setColor(Color.RED);
g.setStroke(new BasicStroke(1.0f * (float) CircuitSettings.getWireWidth()));
g.draw(shape);
}
public Shape getUpPointerShape() {
Path2D shape = new Path2D.Double();
shape.moveTo(+0.50 * SIZE, -0.28 * SIZE);
shape.lineTo(+0.50 * SIZE + 0.7 * POINTER_SIZE, -0.28 * SIZE + POINTER_SIZE);
shape.lineTo(+0.50 * SIZE - 0.7 * POINTER_SIZE, -0.28 * SIZE + POINTER_SIZE);
shape.closePath();
return shape;
}
private Shape getShapeFall(double w, double h, double s) {
Path2D shape = getShapeEmpty();
double sw2 = 0.5 * VisualCommonSettings.getStrokeWidth();
double w2 = 0.5 * w;
double s2 = 0.5 * s;
shape.moveTo(0.0, -s2);
shape.lineTo(0.0, +s2 - h);
shape.moveTo(0.0, +s2 - sw2);
shape.lineTo(-w2, +s2 - h);
shape.lineTo(+w2, +s2 - h);
shape.closePath();
return shape;
}
@Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// // TEST: WindowsLookAndFeel
// if (c instanceof JPopupMenu) {
// Container top = ((JPopupMenu) c).getTopLevelAncestor();
// if (top instanceof JWindow) {
// Composite cmp = g2.getComposite();
// g2.setComposite(AlphaComposite.Clear);
// g2.setPaint(new Color(0x0, true));
// g2.clearRect(x, y, width, height);
// g2.setComposite(cmp);
// }
// }
double r = ARC;
double w = width - 1d;
double h = height - 1d;
Path2D p = new Path2D.Double();
p.moveTo(x, y);
p.lineTo(x, y + h - r);
p.quadTo(x, y + h, x + r, y + h);
p.lineTo(x + w - r, y + h);
p.quadTo(x + w, y + h, x + w, y + h - r);
p.lineTo(x + w, y);
p.closePath();
// Area round = new Area(p);
g2.setPaint(c.getBackground());
g2.fill(p);
g2.setPaint(c.getForeground());
g2.draw(p);
g2.setPaint(c.getBackground());
g2.drawLine(x + 1, y, x + width - 2, y);
g2.dispose();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
final Rectangle bounds = getBounds();
final int height = bounds.height;
if (height <= 20) {
return;
}
final Graphics2D g2 = (Graphics2D)g;
final float msPerPixel = (2.0f * 1000000.0f / 60.0f) / height;
final float y = displayRefreshRateManager.getTargetMicrosPerFrame() / msPerPixel;
final Stroke oldStroke = g2.getStroke();
try {
g2.setStroke(STROKE);
final Path2D path = new Path2D.Float();
// Slight left indent to allow space for [targetFrameTimeLabel].
path.moveTo(34, height - y);
path.lineTo(bounds.width, height - y);
g2.draw(path);
}
finally {
g2.setStroke(oldStroke);
}
}
private static void drawCellHorisontalBlock(Graphics2D g, Coordinate dy, Coordinate dx) {
Path2D shape = new Path2D.Double();
shape.moveTo(dx.getValue() - 0.1, dy.getValue());
shape.lineTo(dx.getValue() + 0.1, dy.getValue());
g.setColor(Color.RED);
g.setStroke(new BasicStroke(1.5f * (float) CircuitSettings.getWireWidth()));
g.draw(shape);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (heapState == null) {
return;
}
final int height = getHeight() - 1;
final int width = getWidth();
final long now = System.currentTimeMillis();
final long maxDataSize = Math.round(heapState.getCapacity() / (double)TEN_MB) * TEN_MB + TEN_MB;
final Graphics2D graphics2D = (Graphics2D)g;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setColor(getForegroundColor());
graphics2D.setStroke(GRAPH_STROKE);
Path2D path = null;
for (HeapSample sample : heapState.getSamples()) {
final double x = width - (((double)(now - sample.getSampleTime())) / ((double)heapState.getMaxSampleSizeMs()) * width);
final double y = (double)height * sample.getBytes() / maxDataSize;
if (path == null) {
path = new Path2D.Double();
path.moveTo(x, height - y + 1);
}
else {
path.lineTo(x, height - y + 1);
}
}
graphics2D.draw(path);
}
/**
* Tests {@link ShapeUtilities#toPrimitive(Shape)}.
*/
@Test
public void testToPrimitive() {
final Path2D path = new Path2D.Double();
path.moveTo(4, 5);
path.lineTo(7, 9);
Shape p = ShapeUtilities.toPrimitive(path);
assertInstanceOf("toPrimitive", Line2D.class, p);
assertEquals("P1", new Point2D.Double(4, 5), ((Line2D) p).getP1());
assertEquals("P2", new Point2D.Double(7, 9), ((Line2D) p).getP2());
path.reset();
path.moveTo(4, 5);
path.quadTo(6, 7, 8, 5);
p = ShapeUtilities.toPrimitive(path);
assertInstanceOf("toPrimitive", QuadCurve2D.class, p);
assertEquals("P1", new Point2D.Double(4, 5), ((QuadCurve2D) p).getP1());
assertEquals("CtrlPt", new Point2D.Double(6, 7), ((QuadCurve2D) p).getCtrlPt());
assertEquals("P2", new Point2D.Double(8, 5), ((QuadCurve2D) p).getP2());
path.reset();
path.moveTo(4, 5);
path.curveTo(6, 7, 8, 6, 9, 4);
p = ShapeUtilities.toPrimitive(path);
assertInstanceOf("toPrimitive", CubicCurve2D.class, p);
assertEquals("P1", new Point2D.Double(4, 5), ((CubicCurve2D) p).getP1());
assertEquals("CtrlP1", new Point2D.Double(6, 7), ((CubicCurve2D) p).getCtrlP1());
assertEquals("CtrlP2", new Point2D.Double(8, 6), ((CubicCurve2D) p).getCtrlP2());
assertEquals("P2", new Point2D.Double(9, 4), ((CubicCurve2D) p).getP2());
}
private static Path2D getPath(int x, int y, int len) {
Path2D p = new Path2D.Double();
p.moveTo(x, y);
p.quadTo(x + len, y, x + len, y + len);
return p;
}
/**
* Uses the points to build a polyline
*
* @param baseShape the points of the polyline
* @return a polyline shape or null if it can't be build from the current configuration
*/
protected Shape buildPolyline(ShapePoints baseShape)
{
Path2D path = null;
List<Point> points = baseShape.getPoints();
if (points != null && !points.isEmpty())
{
float scaleFactorX = 1.0f;
float scaleFactorY = 1.0f;
Rectangle2D bounds = getBounds(baseShape);
Integer width = getWidth();
Integer height = getHeight();
if (width != null) {
scaleFactorX = (float)(width / bounds.getWidth());
}
if (height != null) {
scaleFactorY = (float)(height / bounds.getHeight());
}
path = new Path2D.Double();
if (points.size() > 1)
{
Point offset = getOffset(bounds);
Point point = points.get(0);
path.moveTo(
(point.getX() - offset.getX()) * scaleFactorX,
(point.getY() - offset.getY()) * scaleFactorY
);
for (int i = 1; i < points.size(); i++)
{
point = points.get(i);
path.lineTo(
(point.getX() - offset.getX()) * scaleFactorX,
(point.getY() - offset.getY()) * scaleFactorY
);
}
}
}
return path;
}
private static Path2D getPath(int x, int y, int len) {
Path2D p = new Path2D.Double();
p.moveTo(x, y);
p.quadTo(x + len, y, x + len, y + len);
return p;
}
private static void testNegativeScale(final BufferedImage image, final SCALE_MODE mode) {
final Graphics2D g2d = (Graphics2D) image.getGraphics();
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, SIZE, SIZE);
g2d.setColor(Color.BLACK);
// Bug in TransformingPathConsumer2D.adjustClipScale()
// non ortho scale only
final double scale = -1.0;
final AffineTransform at;
switch (mode) {
default:
case ORTHO:
at = AffineTransform.getScaleInstance(scale, scale);
break;
case NON_ORTHO:
at = AffineTransform.getScaleInstance(scale, scale + 1e-5);
break;
case COMPLEX:
at = AffineTransform.getScaleInstance(scale, scale);
at.concatenate(AffineTransform.getShearInstance(1e-4, 1e-4));
break;
}
g2d.setTransform(at);
// Set cap/join to reduce clip margin:
g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
final Path2D p = new Path2D.Double();
p.moveTo(scale * 10, scale * 10);
p.lineTo(scale * (SIZE - 10), scale * (SIZE - 10));
g2d.draw(p);
if (SAVE_IMAGE) {
try {
final File file = new File("ScaleClipTest-testNegativeScale-" + mode + ".png");
System.out.println("Writing file: " + file.getAbsolutePath());
ImageIO.write(image, "PNG", file);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
// Check image:
// 25, 25 = black
checkPixel(image.getData(), 25, 25, Color.BLACK.getRGB());
} finally {
g2d.dispose();
}
}