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

  • Home
  • SEARCH
  • 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 8123955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:19:26+00:00 2026-06-06T06:19:26+00:00

In the following example, I have a custom JComponent being drawn on green background,

  • 0

In the following example, I have a custom JComponent being drawn on green background, but it does not appear. Why does this happen?

public class Test_Background {

    public static class JEllipse extends JComponent {

        private final Ellipse2D ellipse;

        public JEllipse(int width, int height) {
            ellipse = new Ellipse2D.Double(0, 0, width, height);

            setOpaque(true);
            setBackground(Color.GREEN);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension((int) ellipse.getBounds().getMaxX(),
                                 (int) ellipse.getBounds().getMaxY());
        }

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

            ((Graphics2D) g).draw(ellipse);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JEllipse e = new JEllipse(400, 300);

                JFrame f = new JFrame("Background Test");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(e);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}
  • 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-06T06:19:30+00:00Added an answer on June 6, 2026 at 6:19 am

    JComponent does not paint its background. You can either paint it yourself, or use JPanel which does paint its background

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

Sidebar

Related Questions

I have the following XML example: <?xml version=1.0 encoding=UTF-8?> <OpenSearchDescription xmlns=http://a9.com/-/spec/opensearch/1.1/ xmlns:custom=http://example.com/opensearchextensions/1.0/> <ShortName>Test</ShortName> <Description>Testing....</Description>
Is there a way to write custom comparator, following this example: There are at
I created a custom annotation for logging following the example in this blog post
In the following example I have two years worth of data denoted by data_2007
I'm trying to figure out how XSLT process namespace prefixes and have following example:
If I have the following example file where each number is represents a byte
If I have the following: /* * example.h */ #ifndef EXAMPLE #define EXAMPLE #include
Let's have a following example: public class X { } public class Y {
I have the following example, compiled in VS2005, warning level 4: int main(int argc,
I have the following example of reading from a buffered reader: while ((inputLine =

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.