I have a class named MyClass and I want this class to extend Graphics2D (in java.awt). However when I type public class MyClass extends Graphics2D { .... } I have to add the unimplemented methods draw, drawImage, addRenderingHints, etc. because Eclipse shows this error and it won’t compile.
This is where the question comes to my mind: I just want to use draw, setBackground and other few methods of Graphics2D, I don’t want the rest of the code coming with other unimplemented methods which are mandatory.
Is there a way to avoid this? Because I’m extremely clean and simple when it comes to code and I don’t want other 100 lines of code which I don’t even use.
What are your suggestions?
You could have it extend Graphics, and whenever you need a Graphics2D, just cast.
Graphics g = this.create();Graphics2D g2d = (Graphics2D) g;You’d need to make a Graphics variable within however, as you cannot cast
this.So for “setBackground”:
Really, though, no matter what you do, it’s going to be messy. I’m not even sure if you can do the this.create() thing without bad stuff happening, so keep that in mind.