标题是我在编译代码时遇到的错误。我正在编辑一张图片四次,并将其全部显示为一张图片。错误发生在我说 p1 和 p2 分别等于 poster1 和 poster2 的主要方法中。我要问的是如何解决错误,我认为这可能是由于海报方法,但我不知道。
import java.awt.Color;
public class PP3kthoma34
{
public static void main (String[] args)
{
Picture p; // create the variable
String filename;
filename = FileChooser.pickAFile();
FileChooser.setMediaPath ( filename );
System.out.println (filename);
p = new Picture( filename );
//Create four different posterizations based on the original picture
//arrange them on a grid based on the original pictures height and width
Picture p1;
p1 = poster1( p );
Picture p2;
p2 = poster2( p );
Picture p3;
p3 = poster3( p );
Picture p4;
p4 = poster4( p );
//Obtaining data from the original picture
int width = p.getWidth();
int height = p.getHeight();
//creating the base for the final image
Picture finalPicture = new Picture ( width * 2, height * 2);
//calling the method to place the images
finalPicture ( finalPicture , p1, 0, 0);
finalPicture ( finalPicture , p2, width, 0);
finalPicture ( finalPicture , p3, 0, height);
finalPicture ( finalPicture , p4, width, height);
finalPicture.explore();
}
//Method for poster one in quadrant 1
public static void poster1 (Picture p)
{
int x;
int y;
// loop for all of the columns in the picture
for ( x = 0 ; x < p.getWidth() ; x++ )
{
// access column x in the picture
for ( y = 0 ; y < p.getHeight() ; y++ )
{
Pixel pix = p.getPixel(x, y);
int r = pix.getRed();
int g = pix.getGreen();
int b = pix.getBlue();
int grayAmount = (int)(r*0.299 + g*0.587 + b*0.114);
if ( grayAmount > 191 ) // 192 to 255
{
pix.setRed (255);
pix.setGreen (150);
pix.setBlue (40);
}
else if ( grayAmount > 127 ) // 128 to 191
{
pix.setColor (Color.BLUE);
}
else if ( grayAmount > 63 ) // 64 to 127
{
pix.setColor (Color.CYAN);
}
else // 0 to 63
{
pix.setColor (Color.ORANGE);
}
}
}
}
public static void poster2 (Picture p)
{
int x;
int y;
// loop for all of the columns in the picture
for ( x = 0 ; x < p.getWidth() ; x++ )
{
// access column x in the picture
for ( y = 0 ; y < p.getHeight() ; y++ )
{
Pixel pix = p.getPixel(x, y);
int r = pix.getRed();
int g = pix.getGreen();
int b = pix.getBlue();
int grayAmount = (int)(r*0.299 + g*0.587 + b*0.114);
if ( grayAmount > 191 ) // 192 to 255
{
pix.setColor (Color.GRAY);
}
else if ( grayAmount > 127 ) // 128 to 191
{
pix.setColor (Color.MAGENTA);
}
else if ( grayAmount > 63 ) // 64 to 127
{
pix.setColor (Color.BLACK);
}
else // 0 to 63
{
pix.setColor (Color.GREEN);
}
}
}
}
public static void poster3 (Picture p)
{
int x;
int y;
// loop for all of the columns in the picture
for ( x = 0 ; x < p.getWidth() ; x++ )
{
// access column x in the picture
for ( y = 0 ; y < p.getHeight() ; y++ )
{
Pixel pix = p.getPixel(x, y);
int r = pix.getRed();
int g = pix.getGreen();
int b = pix.getBlue();
int grayAmount = (int)(r*0.299 + g*0.587 + b*0.114);
if ( grayAmount > 191 ) // 192 to 255
{
pix.setColor (Color.PINK);
}
else if ( grayAmount > 127 ) // 128 to 191
{
pix.setRed (40);
pix.setGreen (150);
pix.setBlue (200);
}
else if ( grayAmount > 63 ) // 64 to 127
{
pix.setColor (Color.LIGHT_GRAY);
}
else // 0 to 63
{
pix.setColor (Color.CYAN);
}
}
}
}
public static void poster4 (Picture p)
{
int x;
int y;
// loop for all of the columns in the picture
for ( x = 0 ; x < p.getWidth() ; x++ )
{
// access column x in the picture
for ( y = 0 ; y < p.getHeight() ; y++ )
{
Pixel pix = p.getPixel(x, y);
int r = pix.getRed();
int g = pix.getGreen();
int b = pix.getBlue();
int grayAmount = (int)(r*0.299 + g*0.587 + b*0.114);
if ( grayAmount > 191 ) // 192 to 255
{
pix.setColor (Color.CYAN);
}
else if ( grayAmount > 127 ) // 128 to 191
{
pix.setColor (Color.BLUE);
}
else if ( grayAmount > 63 ) // 64 to 127
{
pix.setColor (Color.ORANGE);
}
else // 0 to 63
{
pix.setColor (Color.WHITE);
}
}
}
}
//Method for arranging the pictures
public static void finalPicture ( Picture finalPicture , Picture p, int offsetX, int offsetY )
{
int width = p.getWidth();
int height = p.getHeight();
int rwidth = finalPicture.getWidth();
int rheight = finalPicture.getHeight();
int x;
int y;
// loop for all of the columns in the picture
for ( x = 0 ; x < width ; x++ )
{
// access column x in the picture
for ( y = 0 ; y < height ; y++ )
{
Pixel pix = p.getPixel(x, y);
Color c1 = pix.getColor();
int resultX = (int)(x + offsetX);
int resultY = (int)(y + offsetY);
// verify that resultX, resultY is a valid coordinate in pResult
if ( (( resultX >= 0 ) && ( resultX < rwidth )) &&
(( resultY >= 0 ) && ( resultY < rheight)) )
{
Pixel resultPix = finalPicture.getPixel ( resultX, resultY );
resultPix.setColor ( c1 );
}
}
}
}
} // end