Need help getting height of image (Possible Inheritance Problem)

旧城冷巷雨未停 提交于 2019-12-13 06:59:28

问题


Ok so this is the KernelHandler Class

public class KernelHandler extends ImageHandler

I'm getting a cannot find method getWidth()

I know it has something to do with inheritance, but I need help, please

//Heres the contructor

 public KernelHandler(String nameOfFile)
{
   super(nameOfFile);


    }

// here's the super constructor
public ImageHandler(String  nameOfFile){


     URL imageURL = getClass().getClassLoader().getResource(nameOfFile);
    try
    {
     myImage = ImageIO.read(imageURL);
    }
    catch ( IOException e )
    {
        System.out.println( "image missing" );
    }

// Here's the method were trying to use

public static int numOfRedBoxes(String nameOfImg)

    {
        KernelHandler myHand = new KernelHandler(nameOfImg);
        for(int i = 0; i < myHand.getWidth(); i++)

            for(int j = 0; j < myHand.getHeight(); j++){
                if(img.getRGB(i, j) == red){
                    numOfRedBoxes++;
               }   
            }
       }
      return numOfRedBoxes;
   }

回答1:


getWidth() should not have private access in ImageHandler (or package private access if both classes are in different packages). Could you check that?

Simply put, getWidth() should be prefixed with public or protected.




回答2:


Is there a method named getWidth() in ImageHandler? I have seen some implementations of this class that do not. That could be another reason why it is not accessible.




回答3:


Try changing the type of myHand to that of the super class , it would be something like:

ImageHandler myHand = new KernelHandler(nameOfImg);


来源:https://stackoverflow.com/questions/5651499/need-help-getting-height-of-image-possible-inheritance-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!