Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda