java - Rotate BufferedImage -
i'm following textbook , have become stuck @ particular point.
this console application.
i have following class rotate image method:
public class rotate { public colorimage rotateimage(colorimage theimage) { int height = theimage.getheight(); int width = theimage.getwidth(); colorimage rotimage = new colorimage(height, width); //having create new obj instance aid rotation (int y = 0; y < height; y++) { (int x = 0; x < width; x++) { color pix = theimage.getpixel(x, y); rotimage.setpixel(height - y - 1, x, pix); } } return rotimage; //i want return theimage ideally can keep state } } the rotation works, have create new colorimage (class below) , means creating new object instance (rotimage) , losing state of object pass in (theimage). presently, it's not big deal colorimage not house much, if wanted house state of, say, number of rotations has had applied or list of i'm losing that.
the class below textbook.
public class colorimage extends bufferedimage { public colorimage(bufferedimage image) { super(image.getwidth(), image.getheight(), type_int_rgb); int width = image.getwidth(); int height = image.getheight(); (int y=0; y < height; y++) (int x=0; x < width; x++) setrgb(x, y, image.getrgb(x,y)); } public colorimage(int width, int height) { super(width, height, type_int_rgb); } public void setpixel(int x, int y, color col) { int pixel = col.getrgb(); setrgb(x, y, pixel); } public color getpixel(int x, int y) { int pixel = getrgb(x, y); return new color(pixel); } } my question is, how can rotate image pass in can preserve state?
unless limit square images or 180° rotations, need new object, dimensions have changed. dimensions of bufferedimage object, once created, constant.
if wanted house state of, say, number of rotations has had applied or list of i'm losing that
you can create class hold other information along colorimage/bufferedimage, limit colorimage/bufferedimage class holding pixels. example:
class imagewithinfo { map<string, object> properties; // meta information file file; // on-disk file loaded image colorimage image; // pixels } then can replace pixels object freely, while preserving other state. it's helpful favor composition on inheritance. in brief means, instead of extending class, create separate class contains original class field.
also note rotation implementation book seems learning purposes. it's fine that, show performance limitations if manipulate big images or continuous graphics rotation @ animation speeds.
Comments
Post a Comment