Java-多边形未在输出图像上绘制

Java-多边形未在输出图像上绘制,java,image,image-processing,Java,Image,Image Processing,我试图在使用imageIO调用的图像上绘制自定义多边形。添加多边形后,应输出图像 下面是我的代码: public static void setPoints(List<Integer> pointArrayX, List<Integer> pointArrayY,File dest) throws IOException{ BufferedImage image = ImageIO.read(new File(dest+"")); Graphics2D g2d = im

我试图在使用imageIO调用的图像上绘制自定义多边形。添加多边形后,应输出图像

下面是我的代码:

public static void setPoints(List<Integer> pointArrayX, List<Integer> pointArrayY,File dest) throws IOException{

BufferedImage image = ImageIO.read(new File(dest+""));
Graphics2D g2d = image.createGraphics();


            g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
            g2d.setColor(Color.RED);
            BasicStroke bs = new BasicStroke(2);
            g2d.setStroke(bs);

                    int[] xPoly = new int[pointArrayX.size()];
                    int[] yPoly = new int[pointArrayY.size()];

            Polygon poly = new Polygon(xPoly,yPoly,xPoly.length);
            poly.getBounds();
            g2d.setPaint(Color.RED);
            g2d.drawPolygon(poly);
            g2d.fillPolygon(xPoly, yPoly, xPoly.length);
            g2d.drawPolygon(xPoly, yPoly, xPoly.length);
            g2d.setStroke(bs);
            g2d.drawPolyline(xPoly, yPoly, xPoly.length);
            g2d.drawOval(100, 100, 200, 200);

            g2d.draw(poly);


            File outputfile = new File(dest+"");
            ImageIO.write(image, "png", outputfile);
publicstaticvoid设置点(List pointArrayX、List pointArrayY、File dest)引发IOException{
buffereImage image=ImageIO.read(新文件(dest+“”));
Graphics2D g2d=image.createGraphics();
g2d.fillRect(0,0,image.getWidth(),image.getHeight());
g2d.setColor(Color.RED);
基本行程bs=新的基本行程(2);
g2d.调整行程(bs);
int[]xPoly=newint[pointArrayX.size()];
int[]yPoly=new int[pointArrayY.size()];
多边形多边形=新多边形(xPoly,yPoly,xPoly.length);
poly.getBounds();
g2d.setPaint(颜色为红色);
g2d.绘制多边形(多边形);
g2d.fillPolygon(xPoly,yPoly,xPoly.length);
g2d.drawPolygon(xPoly,yPoly,xPoly.length);
g2d.调整行程(bs);
g2d.绘制多段线(xPoly、yPoly、xPoly.长度);
g2d.drawOval(100100200200);
g2d.draw(poly);
File outputfile=新文件(dest+“”);
写入(图像,“png”,输出文件);

运行后,输出图像中显示的唯一形状是我定义的椭圆形。它只是没有显示的多边形。

您不填充阵列

int[] xPoly = new int[pointArrayX.size()]; //create an array and set its size
int[] yPoly = new int[pointArrayY.size()]; //create an array and set its size
Polygon poly = new Polygon(xPoly,yPoly,xPoly.length); //use the created array
您需要将数据添加到
xPoly
yPoly
。请尝试:

int[] xPoly = new int[pointArrayX.size()]; //create an array and set its size
int[] yPoly = new int[pointArrayY.size()]; //create an array and set its size

//loop i added to copy the elements from your method arguments to the new arrays
for(int i = 0; i < xPoly.size(); i++) {
    xPoly[i] = pointArrayX.get(i);
    yPoly[i] = pointArrayY.get(i);
}

Polygon poly = new Polygon(xPoly,yPoly,xPoly.length); //use the created array
int[]xPoly=new int[pointArrayX.size()];//创建一个数组并设置其大小
int[]yPoly=new int[pointArrayY.size()];//创建一个数组并设置其大小
//我添加的循环将元素从方法参数复制到新数组
对于(int i=0;i
您从未填充过
xPoly
yPoly
。它们只是零数组。抱歉,我没有在代码中显示。我确实通过方法传递数据。将更新。
我确实通过方法传递数据。
数据可能会传递到方法,但从未用于填充xPoly/yPoly数组。哇,你是一个传奇!非常感谢切!我应该注意到-我错了。@brownez没问题,很乐意帮忙