2015-04-03 50 views
0

標題是編譯我的代碼時出現的錯誤。我正在編輯一張照片四次,並將它們一起顯示爲一張照片。錯誤發生在主要方法中,我說p1和p2分別等於poster1和poster2。我所要求的只是如何解決這個錯誤,我認爲這可能是由於海報方法,但我不知道。strType不匹配:無法從void轉換爲圖片

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 

回答

0

你的方法簽名看看

public static void poster1(Picture p) 

void意味着該方法不返回任何東西。

你想要的是:

public static Picture poster1 (Picture p) 
{ 
    // Do some image processing. 
    return p; 
} 

現在的方法將返回他們剛剛修改的圖片對象。

相關問題