Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用PathIterator创建区域对象?_Java_Bufferedimage_Shapes_Area - Fatal编程技术网

Java 如何使用PathIterator创建区域对象?

Java 如何使用PathIterator创建区域对象?,java,bufferedimage,shapes,area,Java,Bufferedimage,Shapes,Area,我在这里显然遗漏了一个重要的概念。我已经编写了使用鼠标事件在现有BuffereImage上绘制边界(多边形)的代码。以下是相关章节: public void paintComponent(Graphics g) { super.paintComponent(g); //Paint parent's background //G3 displays the BufferedImage "Drawing" with each paint Graphics2D G3 =

我在这里显然遗漏了一个重要的概念。我已经编写了使用鼠标事件在现有BuffereImage上绘制边界(多边形)的代码。以下是相关章节:

public void paintComponent(Graphics g) 
{
    super.paintComponent(g);  //Paint parent's background

    //G3 displays the BufferedImage "Drawing" with each paint
    Graphics2D G3 = (Graphics2D)g;
    G3.drawImage(this.Drawing, 0, 0, null);
    G3.dispose();
} 

public void updateDrawing()
{               
    int x0, y0, x1, y1; // Vertex coordinates
    Line2D.Float seg;
    // grafix is painting the mouse drawing to the BufferedImage "Drawing"     
    if(this.pts.size() > 0)
    {                   
        for(int ip = 0; ip < pts.size(); ip++)
        {
            x0 = (int)this.pts.get(ip).x;
            y0 = (int)this.pts.get(ip).y;
            this.grafix.drawRect(x0 - this.sqw/2, y0 - this.sqh/2, + this.sqw, this.sqh);
            if (ip > 0)
            {
                x1 = (int)this.pts.get(ip-1).x;
                y1 = (int)this.pts.get(ip-1).y; 
                this.grafix.drawLine(x1, y1, x0, y0);
                seg = new Line2D.Float(x1, y1, x0, y0);
                this.segments.add(seg);
            }
        }
    }
    repaint();
}
这一切都很好,我可以保存图像和我的绘图: 顶点列表(PT)和线段(segments)都是描述区域/形状/多边形的。 我只希望从原始图像中提取边界内的区域。也就是说,我计划通过移动所有像素来创建一个新的BuffereImage,测试它们是否在图中,如果在图中,则保留它们。 因此,我想从绘制形状时收集的点和线段创建一个区域。所有内容都是:创建一个区域变量和“getPathIterator”。但是什么形状?我的区域变量将为空。路径迭代器如何访问列表中的点

我浏览了所有的文献和这个网站。
我遗漏了一些东西。

谢谢哈拉尔德的建议。在我看到你的帖子之前,我得出了一个类似的结论: 使用绘制操作中的顶点数组列表,我通过在“绘制”操作期间创建的点列表中循环,填充了一个名为“轮廓”的“Path2D.Float”对象。使用这个“轮廓”对象,我实例化了一个称为“干涉图”的区域。为了检查我的工作,我从该区域创建了另一个PathIterator“PI”,并将该区域“干涉图”分解为“段”,将结果发送到控制台。我在下面显示代码:

private void mnuitmKeepInsideActionPerformed(java.awt.event.ActionEvent evt)                                                 
{                                                     
    // Keeps the inner area of interest
    // Vertices is the "pts" list from Class MouseDrawing (mask)
    // It is already a closed path
    ArrayList<Point2D.Float> vertices = 
            new ArrayList<>(this.mask.getVertices()); 
    this.contour = new Path2D.Float(Path2D.WIND_NON_ZERO);

    // Read the vertices into the Path2D variable "contour"
    this.contour.moveTo((float)vertices.get(0).getX(), 
        (float)vertices.get(0).getY()); //Starting location

    for(int ivertex = 1; ivertex < vertices.size(); ivertex++)
    {
        this.contour.lineTo((float)vertices.get(ivertex).getX(), 
            (float)vertices.get(ivertex).getY());                           
    }      
    this.interferogram = new Area(this.contour);        
    PathIterator PI = this.interferogram.getPathIterator(null);

    //Test print out the segment types and vertices for debug
    float[] p = new float[6];
    int icount = 0;
    while( !PI.isDone())
    {
        int type = PI.currentSegment(p);
        System.out.print(icount);
        System.out.print(" Type " + type);
        System.out.print(" X " + p[0]);
        System.out.println(" Y " + p[1]);
        icount++;
        PI.next();
    }

    BufferedImage masked = Mask(this.image_out, this.interferogram);
    // Write image to file for debug
    String dir;
    dir = System.getProperty("user.dir");
    dir = dir + "\\00masked.png";
    writeImage(masked, dir, "PNG");

}                                                
public BufferedImage Mask(BufferedImage BIM, Area area)
{
    /** Loop through the pixels in the image and test each one for inclusion
    *   within the area.
    *   Change the colors of those outside
    **/

    Point2D p = new Point2D.Double(0,0);
    // rgb should be white
    int rgb = (255 << 24);
    for (int row = 0; row < BIM.getWidth(); row++)
    {
        for (int col = 0; col < BIM.getHeight(); col++)
        {
            p.setLocation(col, row);
            if(!area.contains(p))
            {
                BIM.setRGB(col, row, rgb);
            }
        }
    }
    return BIM;
}
public static BufferedImage deepCopy(BufferedImage B2M) 
{
    ColorModel cm = B2M.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = B2M.copyData(B2M.getRaster()
            .createCompatibleWritableRaster());
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
private void mnuitmKeepInsideActionPerformed(java.awt.event.ActionEvent evt)
{                                                     
//保持感兴趣的内部区域
//顶点是MouseDrawing(mask)类中的“pts”列表
//这已经是一条封闭的道路
阵列列表顶点=
新的ArrayList(this.mask.getVertices());
this.contour=新路径2D.Float(路径2D.WIND\u非零);
//将顶点读入Path2D变量“contour”
this.contour.moveTo((float)顶点.get(0).getX(),
(float)顶点。get(0.getY());//起始位置
对于(int-ivertex=1;ivertex
接下来,我使用下面的代码将遮罩应用于图像,测试每个像素是否包含在该区域中:

private void mnuitmKeepInsideActionPerformed(java.awt.event.ActionEvent evt)                                                 
{                                                     
    // Keeps the inner area of interest
    // Vertices is the "pts" list from Class MouseDrawing (mask)
    // It is already a closed path
    ArrayList<Point2D.Float> vertices = 
            new ArrayList<>(this.mask.getVertices()); 
    this.contour = new Path2D.Float(Path2D.WIND_NON_ZERO);

    // Read the vertices into the Path2D variable "contour"
    this.contour.moveTo((float)vertices.get(0).getX(), 
        (float)vertices.get(0).getY()); //Starting location

    for(int ivertex = 1; ivertex < vertices.size(); ivertex++)
    {
        this.contour.lineTo((float)vertices.get(ivertex).getX(), 
            (float)vertices.get(ivertex).getY());                           
    }      
    this.interferogram = new Area(this.contour);        
    PathIterator PI = this.interferogram.getPathIterator(null);

    //Test print out the segment types and vertices for debug
    float[] p = new float[6];
    int icount = 0;
    while( !PI.isDone())
    {
        int type = PI.currentSegment(p);
        System.out.print(icount);
        System.out.print(" Type " + type);
        System.out.print(" X " + p[0]);
        System.out.println(" Y " + p[1]);
        icount++;
        PI.next();
    }

    BufferedImage masked = Mask(this.image_out, this.interferogram);
    // Write image to file for debug
    String dir;
    dir = System.getProperty("user.dir");
    dir = dir + "\\00masked.png";
    writeImage(masked, dir, "PNG");

}                                                
public BufferedImage Mask(BufferedImage BIM, Area area)
{
    /** Loop through the pixels in the image and test each one for inclusion
    *   within the area.
    *   Change the colors of those outside
    **/

    Point2D p = new Point2D.Double(0,0);
    // rgb should be white
    int rgb = (255 << 24);
    for (int row = 0; row < BIM.getWidth(); row++)
    {
        for (int col = 0; col < BIM.getHeight(); col++)
        {
            p.setLocation(col, row);
            if(!area.contains(p))
            {
                BIM.setRGB(col, row, rgb);
            }
        }
    }
    return BIM;
}
public static BufferedImage deepCopy(BufferedImage B2M) 
{
    ColorModel cm = B2M.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = B2M.copyData(B2M.getRaster()
            .createCompatibleWritableRaster());
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
公共缓冲区图像屏蔽(缓冲区图像BIM,区域)
{
/**循环检查图像中的像素,并测试每个像素是否包含在内
*在区域内。
*改变外面的颜色
**/
点2dp=新点2d.Double(0,0);
//rgb应该是白色的

int rgb=(255在绘制时存储所有点,然后从这些点创建一个多边形。如果需要,从该点创建一个
区域(
新区域(多边形)
)。但是您也可以直接从
多边形
创建一个
路径迭代器
。很难确切地说出您为什么要这样做……也许一些关于您尝试做什么的伪代码会有所帮助。