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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:23:55+00:00 2026-06-18T23:23:55+00:00

New Code package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ //——————————————————————————

  • 0

New Code

package test;

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


public class TestWindow extends JFrame{
//------------------------------------------------------------------------------
    public static void main(String[] args) {
        new TestWindow();
    }
//------------------------------------------------------------------------------
    public TestWindow(){
        setSize(300,300);
        this.setUndecorated(true);
        add(new Background());
        setVisible(true);
    }
//------------------------------------------------------------------------------

    private class Background extends JPanel{
        public Background(){
            add(b);
            repaint();
        }
//------------------------------------------------------------------------------    
        Bubble b = new Bubble();
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Color c = Color.cyan;
            g.setColor(c);
            g.fillRect(0, 0,getWidth(), getHeight());
        }
//------------------------------------------------------------------------------
        private class Bubble extends JPanel{
            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.green);
                g.drawOval(0, 0, Background.this.getWidth(), Background.this.getHeight());
            }
        }
//------------------------------------------------------------------------------
    }
}  

Output

enter image description here

Problem

The aim is to draw a cyan window with a green circle on it. Later I will add components to the green circle so it will look like there is a window with a cyan background and a green circle with components in it.
The output however is only the cyan background. No circle.

I tried setting XOR mode to cyan but that did not work either. Am I nesting the classes wrong?

  • 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-18T23:23:57+00:00Added an answer on June 18, 2026 at 11:23 pm

    The major problem is here…

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Color c = Color.cyan;
            g.setColor(c);
            g.fillRect(0, 0,getWidth(), getHeight());
            add(b);
            repaint();
        }
    

    Not only are adding components to your container within you paint method, you’re also calling repaint, all this is going to conspire against you.

    Paint is called by the repaint manager when ever your component needs to be updated, for all sorts of reasons. You should never call any methods that might invalidate it otherwise require the component to repainted, doing so will send you down slippery slope of CPU burn out.

    Instead.

    1. Add your Bubble component inside the constructor of the Background component
    2. Override the getPreferredSize methods of both these components and provide useful hints so the layout managers have some idea of how much space the components might actually like to use

    The major problem you are facing (other then the bad painting) is that the components are reporting themselves as requiring no height or width, meaning when the layout managers come to lay them out, they are effectively invisible

    Update

    I would recommend that you have a look at

    • Creating a GUI with Swing, paying special attention to the section on layout managers
    • Custom painting
    • Painting in AWT and Swing

    Easter Egg

    For taking advice and making an effort, let me give you a little help…

    enter image description here

    What I suggest you do is read through the code, go back to the Java Docs and tutorials and try and figure out what is going on 😉

    public class CircleControl {
    
        public static void main(String[] args) {
            new CircleControl();
        }
    
        public CircleControl() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                add(new Bubble());
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 300);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                Graphics2D g2d = (Graphics2D) g.create();
    
                Color startColor = brighten(Color.CYAN, 0.75f);
                Color endColor = brighten(Color.CYAN, 0.5f);
    
                LinearGradientPaint lgp = new LinearGradientPaint(
                                new Point(0, 0),
                                new Point(0, getHeight()),
                                new float[]{0f, 1f},
                                new Color[]{startColor, endColor});
    
                g2d.setPaint(lgp);
                g2d.fill(new Rectangle(getWidth(), getHeight()));
                g2d.dispose();
    
            }
        }
    
        public class Bubble extends JPanel {
    
            public Bubble() {
                Font font = UIManager.getFont("Label.font");
                setFont(font.deriveFont(Font.BOLD, 48));
                setForeground(Color.WHITE);
                setBackground(darken(Color.CYAN, 0.3f));
                setOpaque(false);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(150, 150);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
                g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                
                int x = (getWidth() - 150) / 2;
                int y = (getHeight() - 150) / 2;
    
                Color startColor = brighten(getBackground(), 0.05f);
                Color endColor = getBackground();
    
                LinearGradientPaint lgp = new LinearGradientPaint(
                                new Point(x, y),
                                new Point(x, y + 150),
                                new float[]{0f, 1f},
                                new Color[]{startColor, endColor});
                g2d.setPaint(lgp);
                g2d.fill(new Ellipse2D.Double(x, y, 150, 150));
                
                FontMetrics fm = g2d.getFontMetrics();
                x = x + ((150 - fm.stringWidth("22")) / 2);
                y = y + ((150 / 2) + fm.getAscent());
                g2d.setColor(getForeground());
                g2d.drawString("22", x, y);
    
            }
        }
    
        public static Color brighten(Color color, double fraction) {
            int red = (int) Math.round(Math.min(255, color.getRed() + 255 * fraction));
            int green = (int) Math.round(Math.min(255, color.getGreen() + 255 * fraction));
            int blue = (int) Math.round(Math.min(255, color.getBlue() + 255 * fraction));
            int alpha = color.getAlpha();
            return new Color(red, green, blue, alpha);
        }
    
        public static Color darken(Color color, double fraction) {
            int red = (int) Math.round(Math.max(0, color.getRed() - 255 * fraction));
            int green = (int) Math.round(Math.max(0, color.getGreen() - 255 * fraction));
            int blue = (int) Math.round(Math.max(0, color.getBlue() - 255 * fraction));
            int alpha = color.getAlpha();
            return new Color(red, green, blue, alpha);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: package survival; import javax.swing.*; import java.awt.*; public class Survival extends
Please have a look at the following code package test; import java.awt.*; import java.awt.event.*;
In Eclipse, when I run the code, this works: import javax.swing.ImageIcon; import javax.swing.JFrame; import
So i have the following code: package animation; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage;
I'm new to this kabeja package so please can some one provide code example
JFrameWithPanel is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener public
I have developed a Java Swing application, which uses the SwingWorker class to perform
I am trying to write some test code for my java application using Scalatest.
The new Code Bubble IDE has been in the news. I wonder if there
My new code with the updated changes. Clicking the button after inserting a number

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.