簡體   English   中英

strType不匹配:無法從void轉換為Picture

[英]strType mismatch: cannot convert from void to Picture

標題是我編譯代碼時遇到的錯誤。 我正在編輯一張圖片四次,並將它們全部顯示為一張圖片。 錯誤是在主要方法中發生的,我說的是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 

看看你的方法簽名

public static void poster1( Picture p)

void表示此方法不返回任何內容。

您想要的是:

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

現在,這些方法將返回剛剛修改的Picture對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM