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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:53:53+00:00 2026-05-14T04:53:53+00:00

I’m using a custom JLayeredPane. I have several Shapes which needed to be drawn

  • 0

I’m using a custom JLayeredPane.
I have several Shapes which needed to be drawn on different layers in the JLayeredPane.

To test this I create a JPanel and ask its graphics. Then I draw a test rectangle on that JPanel (preparing the graphics) and in my paintComponent method from the JLayeredPane I finally draw everything. But this fails (NullPointerException).

public class MyCustomPanel extends JLayeredPane {

// test
JPanel testpane;
Graphics g2;
// test

// constructor
public MyCustomPanel() {
    testpane = new JPanel();
    this.add(testpane, new Integer(14));
    g2 = testpane.getGraphics();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g2.drawRect(10, 10, 300, 300);
}

}

// run:
//Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
//        at view.MyCustomPanel.paintComponent(MyCustomPanel.java:65)

Why can’t I draw on such a JPanel from within my JLayeredPane? I can draw directly on my JLayeredPane from within my paintComponent method but that’s on the default Panel from the JLayeredPane. I need to create and draw on several layers which are added in my JLayeredPane.

What am I doing wrong? :s

  • 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-14T04:53:53+00:00Added an answer on May 14, 2026 at 4:53 am

    You should use g2 casting the Graphics that is passed to you:

    Graphics2D g2 = (Graphics2D)g;
    

    Why don’t you try decoupling things?

    class InnerPanel extends JPanel
    {
      public void paint(Graphics g)
      {
         Graphics2D g2 = (Graphics2D)g;
         g2.drawRect(....);
      }
    }
    
    class MyLayered extends JLayeredPane()
    {
      MyLayered()
      {
        this.add(new InnerPanel(), 14);
      }
    }
    

    this makes more sense..

    Also because you are trying to do something that doesn’t agree with Swing behaviour.
    Swing will care by itself to call the appropriate paint methods over things that must be displayed, and to go with this protocol you should tell Graphics objects what to draw when Swing asks it to your objects (calling the paint) method, not when you want to do it.

    In this way whenever Swing wants to draw your JLayeredPane you just draw things on a Graphic object of other things without considering that Swing will call their appropriate methods when it’s the right time to do so.

    In conclusion: you can’t draw something on a Graphic object when you want it. You can do it just inside methods invoked by Swing, because otherwise Graphics of these objects doesn’t mean anything

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

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer mysql_query() sends a unique query (multiple queries are not supported)… May 16, 2026 at 4:31 am
  • Editorial Team
    Editorial Team added an answer You should just be able to use the overloads of… May 16, 2026 at 4:31 am
  • Editorial Team
    Editorial Team added an answer Found a way I'm happy with. Using Reed Copsey's pointer… May 16, 2026 at 4:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.