Hello,
I am trying to figure out the best way to provide an interface that hides the implementation details, so it will be easier to implement another alternative in the future.
Inheritance is a possibility, but since Graphics2D is an abstract class, it would be a lot of work.
Composition/delegation seems like a good fit, but I cannot figure out how to use the nifty “by” delegation in Kotlin because Graphics2D is an abstract class. The best solution that I could figure out is to manually implement delegation, such as the following… Sanity check… Is there any way to use “by” in this situation?
Thank you for your help and time!
interface IGraphics {
fun drawRect(x: Int, y: Int, width: Int, height: Int)
}
import java.awt.Graphics2D
import java.awt.image.BufferedImage
class Graphics(val width: Int, val height: Int) : IGraphics {
private val image = BufferedImage(width, height, 0)
private val graphics: Graphics2D = image.createGraphics()
override fun drawRect(x: Int, y: Int, width: Int, height: Int) {
graphics.drawRect(x, y, width, height)
}
}