Home > OS >  How would I go about rendering a transparent and rotated image in Java?
How would I go about rendering a transparent and rotated image in Java?

Time:01-01

I cannot seem to figure out how to draw a transparent and rotated image. I need to be able to draw an image that is transparent and rotated to a certain degree.

I tried this code:

// draws an image that is rotated to a certain degree
public static void drawRotatedImage(BufferedImage image_, int x, int y, int degrees, float scale) {

    // graphics used for the utilities of drawing the image (processing)
    Graphics2D utilGraphics;

    // make rectangular image
    int radius = (int) Math.sqrt(image_.getWidth() * image_.getWidth()   image_.getHeight() * image_.getHeight());
    BufferedImage image1 = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_RGB);

    utilGraphics = image1.createGraphics();
// centers image
    utilGraphics.drawImage(image_, image1.getWidth() / 2 - image_.getWidth() / 2, image1.getHeight() / 2 - image_.getHeight() / 2, null);

    // scale image
    int nw = (int) (image1.getWidth() * scale);
    int nh = (int) (image1.getHeight() * scale);

    BufferedImage image = new BufferedImage(nw, nh, BufferedImage.TYPE_INT_RGB);
    utilGraphics.drawImage(image1, 0, 0, nw, nh, null);

    // Rotation information

    double rotationRequired = Math.toRadians (degrees);
    double locationX = image.getWidth() / 2;
    double locationY = image.getHeight() / 2;

    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

    ImageProducer filteredImgProd = new FilteredImageSource(op.filter(image, null).getSource(), filter);
    Image transparentImg = Toolkit.getDefaultToolkit().createImage(filteredImgProd);

    // Drawing the rotated image at the required drawing locations
    g2d.drawImage(Toolkit.getDefaultToolkit().createImage(transparentImg.getSource()), x, y, null);
}

The filter variable is defined as:

private static final ImageFilter filter = new RGBImageFilter() {
    int transparentColor = new Color(0, 0, 0, 0).getRGB() | 0x0000ffcc;

    public final int filterRGB(int x, int y, int rgb) {
        if ((rgb | 0x0000ffcc) == transparentColor) {
            return 0x0000ffcc & rgb;
        } else {
            return rgb;
        }
    }
};

CodePudding user response:

This ...

    BufferedImage image = new BufferedImage(nw, nh, BufferedImage.TYPE_INT_RGB);
    centeredGraphics.drawImage(image1, 0, 0, nw, nh, null);

You're creating a new BufferedImage (image), but you never actually paint anything to it, instead, you paint image1 to it's own Graphics context.

Now, if you wanted a transparent image, you should have used...

BufferedImage centeredImage = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_ARGB);

instead of...

BufferedImage centeredImage = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_RGB);

And I never used g2d.drawImage(Toolkit.getDefaultToolkit().createImage(transparentImg.getSource()), x, y, null); as it just doesn't make sense to me (transparentImg is already an Image

  • Related