ImageBuffer无法在JPanel

时间:2017-04-02 23:41:42

标签: java swing graphics jframe bufferedimage

我在类中有drawShapeAt方法,应该在给定位置打印形状

@Override
    public void drawShapeAt(int x, int y) {
        try {
            canvas.removeMouseListener(ml);
        } catch (Exception e) {
        }

        polygon = new RegularPolygon(x, y, Integer.parseInt(polygonRadius), Integer.parseInt(polygonLines));
        BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = image.createGraphics();
        Random rand = new Random();
        graphics.setColor(new Color(rand.nextInt(0xFFFFFF)));
        graphics.fill(polygon);
        System.out.println("Painting polygon..");
        canvas.paintComponents(graphics);
    }
}

如果我写例如canvas.setBackground,它将改变背景,但它不会打印形状。我的RegularPolygon如下:

public class RegularPolygon extends Polygon {
    public RegularPolygon(int x0, int y0, int radius, int sides) {
        double alpha = 2 * Math.PI / sides;
        for (int i = 0; i < sides; i++) {
            double x = x0 + radius * Math.cos(alpha * i);
            double y = y0 + radius * Math.sin(alpha * i);
            this.addPoint((int) x, (int) y);
        }
    }
}

我检查了错误的变量,但一切正常(x,y,polygonRadius,polygonLines)。

0 个答案:

没有答案