我正在關注教科書,並陷入了一個特定的問題。旋轉BufferedImage
這是一個控制檯應用程序。
我有下面的類與旋轉圖像方法:
public class Rotate {
public ColorImage rotateImage(ColorImage theImage) {
int height = theImage.getHeight();
int width = theImage.getWidth();
ColorImage rotImage = new ColorImage(height, width); //having to create new obj instance to aid with rotation
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color pix = theImage.getPixel(x, y);
rotImage.setPixel(height - y - 1, x, pix);
}
}
return rotImage; //I want this to return theImage ideally so I can keep its state
}
}
旋轉的作品,但我必須建立一個新的ColorImage(以下級),這意味着我創建一個新的對象實例(rotImage )並失去我傳入的對象(theImage)的狀態。目前,ColorImage並沒有太多的東西,這並不是什麼大問題,但是如果我希望它能夠適應已經應用的旋轉次數或者我失去了所有這些東西的列表。
以下課程來自課本。
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();
for (int y=0; y < height; y++)
for (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);
}
}
我的問題是,我如何旋轉我傳入的圖像,以便我可以保持其狀態?
你真的不能沒有把它包裝在像你一樣的對象中,或者發明你自己的圖像格式,或者做類似於速記的事情,並將狀態信息隱藏在已經使用過的格式中。 – arynaq
@arynaq我認爲你的意思是'隱寫術'。 – EJP
@EJP我最肯定的是:D – arynaq