Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda