Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Casting a Graphics object to a subclass of Graphics2D

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!

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!