I am in the process of creating a class which extends Graphics2D. In this class, I create some of my own methods based off of existing ones, such as a method to draw a circle centered at a point. I wish to do this so I will not have to call several different classes for rendering shapes - instead, I will only call a single, unified one.
My problem at the moment is with creating this object, which I call GraphicsEngine. In a Game thread which extends Canvas, I create a graphics object which I then attempt to cast to a GraphicsEngine object (slightly expanded below for clarity):
GraphicsEngine graphicsEngine;
private void render(){
BufferStrategy bufferStrategy = getBufferStrategy();
if(bufferStrategy == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bufferStrategy.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
graphicsEngine = (GraphicsEngine)g2d;
}
When I try to initialize the GraphicsEngine object, I get a ClassCastException. (
Link to my GraphicsEngine class to prevent clutter
I am wondering why I am getting this exception, since I have not introduced any new variables - only methods, which I do not believe to matter in casting. Since Graphics can be cast to Graphics2D, I had assumed it could also be cast to a subclass of Graphics2D. I would appreciate any help in understanding why this is not the case or what error I am making in this process. Furthermore, I would appreciate any assistance in getting the initialization of the GraphicsEngine object to work. Cheers!