图形2D旋转在黑色/白色背景上

时间:2015-01-26 16:43:56

标签: java background rotation graphics2d

我有一个在g2组件上绘制旋转图片的功能但由于某种原因我不能给它任何彩​​色背景...... 我试图使用方法.setColor()和.setBackground()但没有用。 我在这里看到了一个类似的案例Graphics2D: Drawing black on white? 但它并没有真正帮助。这是我的功能:

        public void rotateImage1(double degrees, ImageObserver o){

        double sin = Math.abs(Math.sin(Math.toRadians(degrees)));
        double cos = Math.abs(Math.cos(Math.toRadians(degrees)));
        ImageIcon icon = new ImageIcon(this.spiral);
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        int neww = (int)Math.floor(w*cos+h*sin);
        int newh = (int)Math.floor(h*cos+w*sin);
        BufferedImage blankCanvas = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
        if(PhotoEdit.black)
            g2.setColor(Color.BLACK);
        else
            g2.setColor(Color.WHITE);
        g2.translate((neww-w)/2, (newh-h)/2);
        g2.rotate(Math.toRadians(degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);

        g2.drawImage(this.spiral, 0, 0, o);
        this.spiral = blankCanvas;
}

PhotoEdit.black是一个布尔变量,如果用户选中带有黑色背景选项的复选框,则为true。

1 个答案:

答案 0 :(得分:2)

您正在设置图形中的颜色,但您没有将其绘制到面板中。

您可以使用g2.setColor(...)g2.fillRect(...)并指定覆盖整个面板的坐标,然后在顶部绘制图像。

fillRect的文档:http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillRect%28int,%20int,%20int,%20int%29