Cannot instantiate the type Image java?

只愿长相守 提交于 2019-12-11 00:25:11

问题


public Image  images[] = new Image[20];

    for(i=0; i<10; i++){
images[i]=new Image(getClass().getResource("/images/"+i+".jpg"));
        }

I am trying to add images to array but it gives the error Cannot instantiate the type Image j

What can be the reason?


回答1:


Abstract classes cant be instantiated directly. You could use ImageIO.read which returns BufferedImage, a sub-class of Image

void loadImages() throws IOException {

    for (int i = 0; i < 10; i++) {
        images[i] = ImageIO.read(getClass().getResource("/images/" + i + ".jpg"));
    }
}



回答2:


Image is an abstract class and thus cannot be instancied. You should use one of the class who extend Image, like BufferedImage or VolatileImage.

Source: Javadoc of Image



来源:https://stackoverflow.com/questions/18960758/cannot-instantiate-the-type-image-java

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