我已将图像信息保存在位图中:
for(int j=0; j<tinggi; j++) {
for(int i=0; i<lebar; i++) {
warna = pixels[j*lebar+i];
alpha = (warna >>24) &0xff;
red = (warna >>16) & 0xff;
bmpR[i][j] = red;
green = (warna >>8) &0xff;
bmpG[i][j] = green;
blue = blue = (warna ) &0xff;
bmpB[i][j] = blue;
}
}
我尝试使用以下代码旋转图像:
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
double xr = (i*Math.cos(r))-(j*Math.sin(r));
double yr = (i*Math.sin(r))+(j*Math.cos(r));
int xro = (int) Math.round(xr);
int yro = (int) Math.round(yr);
rotationR [i+ xro][j+ yro] = (bmpR[i][j]);
rotationG [i+ xro][j+ yro] = (bmpG[i][j]);
rotationB [i+ xro][j+ yro] = (bmpB[i][j]);
}
}
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
g.setColor(new Color(rotationR[i][j], rotationG[i][j], rotationB[i][j]));
g.drawLine(i+lebar+100, j+450, i+lebar+100, j+450);
}
}
但它没有输出任何东西(当翻译和缩放工作时)。
我的轮换代码出了什么问题?
答案 0 :(得分:1)
你的逻辑存在缺陷:
您必须将新位置存储在数组中,而不是更改数组indice
Point[][] rotatedLocations = ...
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
double xr = (i*Math.cos(r))-(j*Math.sin(r));
double yr = (i*Math.sin(r))+(j*Math.cos(r));
int xro = (int) Math.round(xr);
int yro = (int) Math.round(yr);
rotatedLocations[i][j] = new Point(xr, yr);
}
}
绘制像素时,只在相关位置绘制它们
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
//new (rotated) location for original x/y
Point rotatedLocation = rotatedLocations[i][j];
//color from the original
g.setColor(new Color(original[i][j], original[i][j], original[i][j]));
//but drawn on the new (rotated) location
g.drawLine(rotatedLocation.x, rotatedLocation.y, rotatedLocation.x, rotatedLocation.y);
}
}