从线生成多边形

VOX*_*VOX 7 c# graphics geometry drawing 2d

我想画一条厚度为j2me的线.通过将Pen width设置为厚度值,可以在桌面java中轻松实现.但是在j2me中,Pen类不支持宽度.我的想法是从我拥有的线生成一个多边形,类似于我想要绘制的厚度线.在图片中,左边是我所拥有的,带有点的线.在右边是我想要的,一个多边形,当填充时,一条线有厚度.谁能知道如何从线生成多边形?

alt text http://www.freeimagehosting.net/uploads/140e43c2d2.gif

Ste*_*ada 4

啊,如果你进行预处理,那应该会让你的生活更轻松。这是我使用 Graphics2D(使用 J2SE)编写的一些代码。我不喜欢输出包含额外的内部部分,但当它被填充时,它看起来相当不错。

import java.awt.BasicStroke;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;

public class StrokePath
{
    public static void main(String[] args)
    {
        // set line width to 6, use bevel for line joins
        BasicStroke bs = new BasicStroke(6.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL);

        // create a path for the input
        Path2D p = new Path2D.Float();
        p.moveTo(50.0, 50.0);
        p.lineTo(65.0, 100.0);
        p.lineTo(70.0, 60.0);
        p.lineTo(120.0, 65.0);
        p.lineTo(40.0, 200.0);

        // create outline of wide lines by stroking the path with the stroke
        Shape s = bs.createStrokedShape(p);
        // output each of the segments of the stroked path for the output polygon
        PathIterator pi = s.getPathIterator(null);
        while (!pi.isDone())
        {
            pi.next();
            double[] coords = new double[6];
            int type = pi.currentSegment(coords);
            switch (type)
            {
            case PathIterator.SEG_LINETO:
                System.out.println(String.format("SEG_LINETO %f,%f", coords[0], coords[1]));
                break;
            case PathIterator.SEG_CLOSE:
                System.out.println("SEG_CLOSE");
                break;
            case PathIterator.SEG_MOVETO:
                System.out.println(String.format("SEG_MOVETO %f,%f", coords[0], coords[1]));
                break;
            default:
                System.out.println("*** More complicated than LINETO... Maybe should use FlatteningPathIterator? ***");
                break;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是渲染这些坐标后的结果:

未填充 填充