我正在开发一个java项目(创建我自己的图像编辑器),我的任务是允许用户将现有PPM图像的大小加倍。我必须通过使用RGB的二维数组来实现这一点。我的导师给了我一个暗示,我必须创建一个新的图像,其中第一个的宽度和高度加倍,并填充新的数组。我相信我已经正确地做到了这一点。但是,当我运行我的程序并单击界面上的双倍大小按钮时,我的原始图像的大小加倍,但它变为完全黑色。我相信我如何转换RGB值存在问题。我插入了print语句以查看代码何时出错(当我的RGB值变为零时)....
else if (action.equals(doubleSizeMenuItem.getActionCommand())) {
int gimmieRows = thePPMImage.getNumRows();
int gimmieCols = thePPMImage.getNumCols();
int[][] gimmieRed = thePPMImage.getRedPixels();
int[][] gimmieBlue = thePPMImage.getBluePixels();
int[][] gimmieGreen = thePPMImage.getGreenPixels();
int[][] doubleMyRedPixels = new int[gimmieRed.length * 2][gimmieRed.length * 2];
int[][] doubleMyBluePixels = new int[gimmieBlue.length * 2][gimmieBlue.length * 2];
int[][] doubleMyGreenPixels = new int[gimmieGreen.length * 2][gimmieGreen.length * 2];
int longer = gimmieRows * 2;
int wider = gimmieCols * 2;
for (int i = 0; i < longer; i++) {
for (int j = 0; i < wider; j++) {
doubleMyRedPixels[i * 2][j * 2]=gimmieRed[i][j];
doubleMyRedPixels[i * 2][(j * 2) + 1]= gimmieRed[i][j];
doubleMyRedPixels[(i * 2) + 1][j * 2]= gimmieRed[i][j];
doubleMyRedPixels[(i * 2) + 1][(j * 2) + 1]= gimmieRed[i][j];
doubleMyGreenPixels[i * 2][j * 2]=gimmieGreen[i][j];
doubleMyGreenPixels[i * 2][(j * 2) + 1]= gimmieGreen[i][j];
doubleMyGreenPixels[(i * 2) + 1][j * 2]= gimmieGreen[i][j];
doubleMyGreenPixels[(i * 2) + 1][(j * 2) + 1]= gimmieGreen[i][j];
System.out.println(doubleMyRedPixels[0][0]);
doubleMyBluePixels[i * 2][j * 2]=gimmieBlue[i][j];
doubleMyBluePixels[i * 2][(j * 2) + 1]= gimmieBlue[i][j];
doubleMyBluePixels[(i * 2) + 1][j * 2]= gimmieBlue[i][j];
doubleMyBluePixels[(i * 2) + 1][(j * 2) + 1]= gimmieBlue[i][j];
thePPMImage=new PPMImage(doubleMyRedPixels, doubleMyGreenPixels, doubleMyBluePixels);
System.out.println(doubleMyRedPixels[5][5]);
thePPMImage.setRedPixels(doubleMyRedPixels);
thePPMImage.setGreenPixels(doubleMyGreenPixels);
thePPMImage.setBluePixels(doubleMyBluePixels);
System.out.println(doubleMyRedPixels[2][7]);
imageLabel.setIcon( new SImage(
transposeArray(thePPMImage.getRedPixels()),
transposeArray(thePPMImage.getGreenPixels()),
transposeArray(thePPMImage.getBluePixels())));
System.out.println(doubleMyRedPixels[4][6]);
一旦我点击此线,图像就会变黑。
thePPMImage=new PPMImage(doubleMyRedPixels, doubleMyGreenPixels, doubleMyBluePixels);
System.out.println(doubleMyRedPixels[5][5]);
好的,我已经在嵌套的for循环中尝试了以下代码, 单独 ,我收到了每个代码的ArrayOutOfBounds异常。我对它的理解是因为如果我在(5,3)处有一个像素并且我想要加倍它,我需要它被转置并且在宽度和高度上加倍占据4个位置。 (10,6)左上(10,7)右上,(11,6)左下,(11,7)右下。所以每个r g和b我需要4个赋值语句吗?
IDK 我已经尝试了以下四个作为我的嵌套for循环的内容而没有任何运气。
//1st try
doubleMyRedPixels[i][j]=gimmieRed[i/2][j/2];
doubleMyGreenPixels[i ][j]=gimmieGreen[i/2][j/2];
doubleMyBluePixels[i ][j]=gimmieBlue[i/2][j/2];
//2nd try
doubleMyRedPixels[i][j]=gimmieRed[i/2][j/2];
doubleMyRedPixels[i][j]=gimmieRed[i / 2][(j / 2) - 1];
doubleMyRedPixels[i][j]=gimmieRed[(i / 2) - 1][j /2];
doubleMyRedPixels[i][j]=gimmieRed[(i / 2) - 1][(j /2) - 1];
doubleMyGreenPixels[i ][j]=gimmieGreen[i/2][j/2];
doubleMyGreenPixels[i][j]=gimmieGreen[i / 2][(j / 2) - 1];
doubleMyGreenPixels[i][j]=gimmieGreen[(i / 2) -1][j / 2];
doubleMyGreenPixels[i][j]=gimmieGreen[(i / 2) -1][(j / 2) - 1];
doubleMyBluePixels[i ][j]=gimmieBlue[i/2][j/2];
doubleMyBluePixels[i][j]=gimmieBlue[i /2][(j / 2) - 1];
doubleMyBluePixels[i][j]=gimmieBlue[(i / 2) - 1][j / 2];
doubleMyBluePixels[i][j]=gimmieBlue[(i / 2) -1][(j / 2) - 1];*/
//3rd try
doubleMyRedPixels[i ][j]=gimmieRed[i*2][j*2];
doubleMyRedPixels[i][j]=gimmieRed[i * 2][(j * 2) + 1];
doubleMyRedPixels[i][j]=gimmieRed[(i * 2) + 1][j * 2];
doubleMyRedPixels[i][j]=gimmieRed[(i * 2) + 1][(j * 2) + 1];
doubleMyGreenPixels[i ][j]=gimmieGreen[i*2][j*2];
doubleMyGreenPixels[i][j]=gimmieGreen[i * 2][(j * 2) + 1];
doubleMyGreenPixels[i][j]=gimmieGreen[(i * 2) + 1][j * 2];
doubleMyGreenPixels[i][j]=gimmieGreen[(i * 2) + 1][(j * 2) + 1];
doubleMyBluePixels[i ][j]=gimmieBlue[i*2][j*2];
doubleMyBluePixels[i][j]=gimmieBlue[i * 2][(j * 2) + 1];
doubleMyBluePixels[i][j]=gimmieBlue[(i * 2) + 1][j * 2];
doubleMyBluePixels[i][j]=gimmieBlue[(i * 2) + 1][(j * 2) + 1];
//4th try
doubleMyRedPixels[i * 2][j * 2]=gimmieRed[i][j];
doubleMyRedPixels[i * 2][(j * 2) + 1]= gimmieRed[i][j];
doubleMyRedPixels[(i * 2) + 1][j * 2]= gimmieRed[i][j];
doubleMyRedPixels[(i * 2) + 1][(j * 2) + 1]= gimmieRed[i][j];
doubleMyGreenPixels[i * 2][j * 2]=gimmieGreen[i][j];
doubleMyGreenPixels[i * 2][(j * 2) + 1]= gimmieGreen[i][j];
doubleMyGreenPixels[(i * 2) + 1][j * 2]= gimmieGreen[i][j];
doubleMyGreenPixels[(i * 2) + 1][(j * 2) + 1]= gimmieGreen[i][j];
doubleMyBluePixels[i * 2][j * 2]=gimmieBlue[i][j];
doubleMyBluePixels[i * 2][(j * 2) + 1]= gimmieBlue[i][j];
doubleMyBluePixels[(i * 2) + 1][j * 2]= gimmieBlue[i][j];
doubleMyBluePixels[(i * 2) + 1][(j * 2) + 1]= gimmieBlue[i][j];
好的,我想我明白了。
我的新2D数组应该已经像这样初始化了。
int[][] doubleMyRedPixels = new int[gimmieRed.length * 2][gimmieRed[0].length * 2];
int[][] doubleMyBluePixels = new int[gimmieBlue.length * 2][gimmieBlue [0].length * 2];
int[][] doubleMyGreenPixels = new int[gimmieGreen.length * 2][gimmieGreen[0].length * 2];
和我的for循环应该如下;
for (int i = 0; i < longer; i++) {
for (int j = 0; **j** < wider; j++) {
并且前面提到的第一次尝试允许我将任何ppm图像的大小加倍!