Java - Create a shape from border around image

前端 未结 1 679
感情败类
感情败类 2021-01-20 16:55

i have a class that draws a shape from a png image, so that i can use the shape to draw the border of custom buttons that i need for my project. here\'s the code for the cla

相关标签:
1条回答
  • 2021-01-20 17:34

    For tips, look at Smoothing a jagged path. The algorithm to obtain the (crude) outline was relatively quick in the final versions. Creating a GeneralPath is astonishingly faster than appending Area objects.

    The important part is this method:

    public Area getOutline(Color target, BufferedImage bi) {
        // construct the GeneralPath
        GeneralPath gp = new GeneralPath();
    
        boolean cont = false;
        int targetRGB = target.getRGB();
        for (int xx=0; xx<bi.getWidth(); xx++) {
            for (int yy=0; yy<bi.getHeight(); yy++) {
                if (bi.getRGB(xx,yy)==targetRGB) {
                    if (cont) {
                        gp.lineTo(xx,yy);
                        gp.lineTo(xx,yy+1);
                        gp.lineTo(xx+1,yy+1);
                        gp.lineTo(xx+1,yy);
                        gp.lineTo(xx,yy);
                    } else {
                        gp.moveTo(xx,yy);
                    }
                    cont = true;
                } else {
                    cont = false;
                }
            }
            cont = false;
        }
        gp.closePath();
    
        // construct the Area from the GP & return it
        return new Area(gp);
    }
    
    0 讨论(0)
提交回复
热议问题