简体   繁体   English

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

[英]Graphics 2D rotating on black/white background

I have a function that draws a rotated picture on a g2 component but for some reason I can't give it any colored background... I was trying to use methods .setColor() and .setBackground() but with no use. 我有一个在g2组件上绘制旋转图片的函数,但是由于某种原因我无法给它提供任何彩色背景...我试图使用.setColor()和.setBackground()方法,但没有用。 I have seen a similiar case here Graphics2D: Drawing black on white? 我在这里看到过类似的情况Graphics2D:在白色上绘制黑色? but it didn't really help. 但这并没有真正的帮助。 Here is my function: 这是我的功能:

        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 is a boolean variable that is true if the user selected the checkbox with black background option. PhotoEdit.black是一个布尔变量,如果用户选择了“黑色背景”复选框,则为true。

You are setting the color in graphics, but you aren't drawing it to the panel. 您正在设置图形中的颜色,但没有将其绘制到面板上。

You can use g2.setColor(...) and g2.fillRect(...) and specify coordinates that cover the whole panel, and then draw your image on top. 您可以使用g2.setColor(...)g2.fillRect(...)并指定覆盖整个面板的坐标,然后在顶部绘制图像。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM