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 6880797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:01:12+00:00 2026-05-27T05:01:12+00:00

Consider the case where I have a JFrame and a JPanel object, with the

  • 0

Consider the case where I have a JFrame and a JPanel object, with the JPanel object embedded inside the JFrame. My understanding was that each graphics component had its own graphics context. So my JFrame has its own graphics context and my JPanel has its own graphics context and these contexts are different. I put together a little test which seems to imply otherwise though:

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.add(panel);

    Graphics frameContext = frame.getGraphics();
    Graphics panelContext = panel.getGraphics();

    if (frameContext == panelContext){
        System.out.println("The contexts are the same.");
    } else {
        System.out.println("The contexts are different.");
    }

The output is “The contexts are the same.”. Why is this the case? Is it possible to have different graphics context for the JPanel and JFrame? I want to be able to draw to the JPanel graphics context, but not the JFrame context.

This question is related to Wrong JPanel displayed in CardLayout. Issues with getGraphics(). I believe the answer to this question might somehow solve the other.

  • 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-05-27T05:01:13+00:00Added an answer on May 27, 2026 at 5:01 am

    Wow. Dumb mistake on my part. The equality test was returning true because both were null.

    The code should be changed to:

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        frame.setVisible(true);
    
        Graphics frameContext = frame.getGraphics();
        Graphics panelContext = panel.getGraphics();        
    
        if (frameContext.equals(panelContext)){
            System.out.println("The contexts are the same.");
        } else {
            System.out.println("The contexts are different.");
        }
    

    After this modification, the output of the test is “The contexts are different.” Therefore, this doesn’t answer the question at Wrong JPanel displayed in CardLayout. Issues with getGraphics() .

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

Sidebar

Related Questions

A pretty normal case: We have a 'portlet' composite component that has two states:
EDIT: I missed a crucial point: .NET 2.0 Consider the case where I have
Consider the following case: I have a single Excel workbook with 4 sheets in
Having the SOLID principles and testability in mind, consider the following case: You have
Consider the case of class which does not have a destructor and constructor explicitly
Consider a hypothetical case where I have to retrieve some details from the database
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Consider this case: public Class1 { public static final String ONE = ABC; public
Consider the case of blogspot.com the domain is the one blogspot.com only but suppose
I'm using LINQ to query data. Consider a case where the user only wants

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.