Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:35:45+00:00 2026-06-17T21:35:45+00:00

My program code is here: package Chapter12; import java.awt.*; import javax.swing.*; public class Pv2

  • 0

My program code is here:

package Chapter12;

import java.awt.*;
import javax.swing.*;

public class Pv2 extends JPanel {

    public static void main (String []args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setContentPane(new Pv2());




    }

    @Override
    public void paintComponent(Graphics g){

        Graphics2D g2 = (Graphics2D) g;
    }



}

Eclipse shows this error when I’m trying to compile the code.

Cannot cast from Graphics to Graphics2D

This error occurs on this line :

Graphics2D g2 = (Graphics2D) g;

Tried jre 1.6 and jre 1.7 – the same thing. Why the cast isn’t allowed? I even tried to download some prepared codes and the error was the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:35:47+00:00Added an answer on June 17, 2026 at 9:35 pm

    You code is not broken and this cast is surely possible. The following code works:

    import java.awt.*;
    import javax.swing.*;
    
    public class Pv2 extends JPanel {
        public static void main (String []args){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(200,200);
            frame.setVisible(true);
            frame.setContentPane(new Pv2());
        }
        public void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D) g;
            g2.drawLine(0,0, getWidth(), getHeight());
        }
    }
    

    Graphics cast works

    One of the possible explanations, you have the class named exactly Graphics or Graphics2D in the same package (Chapter12). Then, with imports as you have, this class will have priority; if it is not compatible, the cast will be rejected by the compiler. I was able to reproduce this by crating the empty class in the same package and naming it Graphics2D.

    To solve, use explicit imports: replace your import statements by

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    

    This will tell the compiler which Graphics and Graphics2D do you mean. Even with confusing Graphics2D class, I do not longer see the error after rewriting imports as shown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code is- package textmessenger; import java.sql.*; public class Main { public static void
I've got the following simple code: package main; import java.util.concurrent.*; public class Main {
First, here is the C# code and the disassembled IL: public class Program<T> {
Here is a part of my code: package com.admobsdk_dfp_handler; import com.google.ads.*; import com.google.ads.doubleclick.*; import
Here is my program code. I am new to Android and Java so it
Please have a look at the following code package normal; import java.io.*; import java.util.*;
I am writing a program for Bully Algorithm in Java Here is the code:
Here's some code (full program follows later in the question): template <typename T> T
I've got a program that worked until recently. The offending code is shown here:
here is the server side code of my program, the problem is, its accepting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.