Search code examples
javaabstract-class

Passing Parameters In Abstract Method


I have an abstract class in Java that has this method called

public abstract void onImageRendered(BufferedImage render)

I want the user to access the rendered image with this method. I have another method called

public void render()

That renders the image. The buffered image in the abstract method should not be any image but the rendered image from this method.
How do I pass the rendered image from that method as the parameter of the abstract method?
Here is my code:

public static void render(Scene scene) throws MORISIllegalMethodCallException{
    if(propertiesAreSet){
        Camera activeCamera = scene.getActiveCamera();

        for (int x = 0; x < activeCamera.getWidth(); x++) {
            for (int y = 0; y < activeCamera.getHeight(); y++) {
                Ray ray = Ray.shootAt(activeCamera.getOrigin(), new Vector(x - activeCamera.getWidth()/2.0, activeCamera.getHeight()/2.0 - y, -(activeCamera.getHeight()/2.0)/Math.tan(activeCamera.getFov() * 0.5)).mul(activeCamera.getDirection()).normalize());
                for (Renderable object : scene.getSceneObjects()) {
                    if (object.intersects(ray)){
                        //Assign colors to buffered image object "this.image" and do other necessary things
                    }
                }
            }
        }
    }else {
        throw new MORISIllegalMethodCallException("The method moris.renderer.MORISRT.setProperties(int, double) should be executed before invoking the method moris.renderer.MORISRT.render(moris.scene.Scene)");
    }
}

abstract void onImageRendered(BufferedImage renderedImage);//Here, the BufferedImage object should be "this.image"

The rendered image is something like this: Here is the rendered image
I tried searching StackOverflow for similar questions and also checked some other sources but they didn't seem to help.
Any suggestions would be appreciated.
Thanks in advance.


Solution

  • abstract void onImageRendered(BufferedImage renderedImage);//Here, the BufferedImage object should be "this.image"
    

    First thing to say is that you are trying to add an implementation detail to an abstract method. That is wrong by definition. If it is an abstract method implementation is delegated to subclasses. But that just means that you are free to use this.image in every subclass. What you can do is, in your abstract class, is having a...

    protected BufferedImage image;
    

    ...that you can reference from every sublcass as this.image.

    But you will have to change the signature of the method render(). It cannot be static if you want to reference this. static methods can only access static attributes, and this always references an instance attribute.

    Update

    You can call the abstract method in the abstract class:

    public abstract class MyAbstractClass {
        private BufferedImage image;
    
        public void render() {
            // ...
            onImageRendered(this.image);
            // ...
        }
    
        public abstract void onImageRendered(BufferedImage renderedImage);
    }
    
    public class MyConcreteClass extends MyAbstractClass {
        @Override
        public abstract void onImageRendered(BufferedImage renderedImage) {
            // do stuff with renderedImage, which will always be the image
            // in the rendered metho called on the parent class
        }
    }