java - Rotating a BufferedImage and Saving it into a pixel array -
java - Rotating a BufferedImage and Saving it into a pixel array -
i trying rotate sword in 2d game. have sword image file, , wish rotate image @ player's location. tried using graphics2d , affinetransform, problem player moves on different coordinate plane, screen class, , graphics uses literal location of pixels on jframe. so, realized need render sword rotating image itself, , saving pixel array screen class render. however, don't know how this. here code screen rendering method:
public void render(double d, double yoffset2, bufferedimage image, int colour, int mirrordir, double scale, spritesheet sheet) { d -= xoffset; yoffset2 -= yoffset; boolean mirrorx = (mirrordir & bit_mirror_x) > 0; boolean mirrory = (mirrordir & bit_mirror_y) > 0; double scalemap = scale - 1; (int y = 0; y < image.getheight(); y++) { int ysheet = y; if (mirrory) ysheet = image.getheight() - 1 - y; int ypixel = (int) (y + yoffset2 + (y * scalemap) - ((scalemap * 8) / 2)); (int x = 0; x < image.getwidth(); x++) { int xpixel = (int) (x + d + (x * scalemap) - ((scalemap * 8) / 2)); int xsheet = x; if (mirrorx) xsheet = image.getwidth() - 1 - x; int col = (colour >> (sheet.pixels[xsheet + ysheet * sheet.width])) & 255; if (col < 255) { (int yscale = 0; yscale < scale; yscale++) { if (ypixel + yscale < 0 || ypixel + yscale >= height) continue; (int xscale = 0; xscale < scale; xscale++) { if (x + d < 0 || x + d >= width) continue; pixels[(xpixel + xscale) + (ypixel + yscale) * width] = col; } } } } } }
here 1 of poor attempts phone call render method sword class:
public void render(screen screen) { affinetransform @ = new affinetransform(); at.rotate(1, image.getwidth() / 2, image.getheight() / 2); affinetransformop op = new affinetransformop(at, affinetransformop.type_bilinear); image = op.filter(image, null); screen.render(this.x, this.y, image, swordcolor, 1, 1.5, sheet); hitbox.setlocation((int) this.x, (int) this.y); (entity entity : level.getentities()) { if (entity instanceof mob) { if (hitbox.intersects(((mob) entity).hitbox)) { // ((mob) entity).health--; } } } }
thank help can provide, , please sense free tell me if theres improve way this.
you can rotate()
image around anchor point, seen here in graphics2d
context. method concatenates translate()
, rotate()
, translate()
operations, seen here explicit transformations.
addendum: it rotates image, how save pixels of image array?
once filter()
image, utilize 1 of imageio.write()
methods save resulting renderedimage
, example.
java swing graphics rotation bufferedimage
Comments
Post a Comment