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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:19:21+00:00 2026-06-14T10:19:21+00:00

Okay I have a Canvas (I am using this because it can implement BufferedStrategy

  • 0

Okay I have a Canvas (I am using this because it can implement BufferedStrategy).

I add my Canvas to my JFrame contentPane and than add HUDPanel ( which is a JPanel consisting of 2 other JPanels – with JProgressBars and JLabels – to show health and chakara bars) to my JFrames glassPane.

All of the components on HUD are opaque.

Now when I add the HUDPanel to my JFrames glassPane with a Canvas as its contentPane the 2 JPanels which hold components are not painted opaque:

enter image description here

But if I add a JPanel instead of Canvas to JFrame contentPane all works fine:

enter image description here

Why is this?
and how can I fix it?

Thank you in advance

HUDPanelTest.java

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class HUDPanelTest {

    public HUDPanelTest() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HUDPanelTest();
            }
        });
    }

    private void initComponents() {
        JFrame frame = new JFrame() {
            @Override
            public Dimension getPreferredSize() {//testing only
                return new Dimension(800, 600);
            }
        };
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        HUDPanel hudPanel = new HUDPanel();
        frame.setGlassPane(hudPanel);
        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                super.paintComponent(grphcs);
                grphcs.setColor(Color.yellow);
                grphcs.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        Canvas c = new Canvas() {
            @Override
            public void paint(Graphics grphcs) {//for testing ONLY I actually use a BufferStrategy
                super.paint(grphcs);

                grphcs.setColor(Color.yellow);
                grphcs.fillRect(0, 0, getWidth(), getHeight());
            }
        };

        frame.add(c);
        //frame.add(p);//this works as expected

        frame.pack();
        frame.setVisible(true);
        hudPanel.setVisible(true);

    }
}

HUDPanel.java

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class HUDPanel extends JPanel {

    HUDPanel.HealthPanel healthPanel;
    HUDPanel.ChakaraPanel chakaraPanel;

    public HUDPanel() {
        super(new BorderLayout(), true);
        setOpaque(false);

        healthPanel = new HUDPanel.HealthPanel();
        chakaraPanel = new HUDPanel.ChakaraPanel();

        initComponents();

        setHealthPlayer1(100);
        setHealthPlayer2(100);
        setChakaraPlayer1(0);
        setChakaraPlayer2(0);
        setLabelTextHealthPlayer1("Player 1 (Health):");
        setLabelTextHealthPlayer2("Player 2 (Health):");
        setLabelTextChakaraPlayer1("Player 1 (Chakara):");
        setLabelTextChakaraPlayer2("Player 2 (Chakara):");
    }

    private void initComponents() {
        add(healthPanel, BorderLayout.NORTH);
        add(chakaraPanel, BorderLayout.SOUTH);
    }

    public void setHealthPlayer1(int health) {
        healthPanel.setPlayer1ProgressBarValue(health);
    }

    public void setHealthPlayer2(int health) {
        healthPanel.setPlayer2ProgressBarValue(health);
    }

    public void setChakaraPlayer1(int chakara) {
        chakaraPanel.setPlayer1ProgressBarValue(chakara);
    }

    public void setChakaraPlayer2(int chakara) {
        chakaraPanel.setPlayer2ProgressBarValue(chakara);
    }

    public void setLabelTextHealthPlayer1(String text) {
        healthPanel.setPlayer1LabelText(text);
    }

    public void setLabelTextHealthPlayer2(String text) {
        healthPanel.setPlayer2LabelText(text);
    }

    public void setLabelTextChakaraPlayer1(String text) {
        chakaraPanel.setPlayer1LabelText(text);
    }

    public void setLabelTextChakaraPlayer2(String text) {
        chakaraPanel.setPlayer2LabelText(text);
    }

    private class HealthPanel extends JPanel {

        JProgressBar player1ProgressBar = new JProgressBar();
        JProgressBar player2ProgressBar = new JProgressBar();
        JLabel player1Label = new JLabel();
        JLabel player2Label = new JLabel();

        public HealthPanel() {
            super(new GridBagLayout(), true);
            setOpaque(false);

            initComponents();
        }

        private void initComponents() {
            GridBagConstraints gc = new GridBagConstraints();
            gc.fill = GridBagConstraints.NONE;
            gc.anchor = GridBagConstraints.WEST;
            gc.insets = new Insets(10, 10, 10, 10);
            //or else the anchoring wont work
            gc.weightx = 1;

            //gc.gridx = 0;//does not seem to make a difference
            //gc.gridy = 0;
            add(player1Label, gc);

            //gc.gridx = 1;
            //gc.gridy = 0;
            add(player1ProgressBar, gc);

            gc.anchor = GridBagConstraints.EAST;

            //gc.gridx = 2;
            //gc.gridy = 0;
            add(player2Label, gc);

            //gc.gridx = 3;
            //gc.gridy = 0;
            add(player2ProgressBar, gc);
        }

        public void setPlayer1ProgressBarValue(int val) {
            player1ProgressBar.setValue(val);
        }

        public void setPlayer2ProgressBarValue(int val) {
            player2ProgressBar.setValue(val);
        }

        public void setPlayer2LabelText(String text) {
            player2Label.setText(text);
        }

        public void setPlayer1LabelText(String text) {
            player1Label.setText(text);
        }
    }

    private class ChakaraPanel extends JPanel {

        JProgressBar player1ProgressBar = new JProgressBar();
        JProgressBar player2ProgressBar = new JProgressBar();
        JLabel player1Label = new JLabel();
        JLabel player2Label = new JLabel();

        public ChakaraPanel() {
            super(new GridBagLayout(), true);
            setOpaque(false);

            initComponents();
        }

        private void initComponents() {
            GridBagConstraints gc = new GridBagConstraints();
            gc.fill = GridBagConstraints.NONE;
            gc.anchor = GridBagConstraints.WEST;
            gc.insets = new Insets(10, 10, 10, 10);
            //or else the anchoring wont work
            gc.weightx = 1;
            gc.weighty = 1;

            //gc.gridx = 0;
            //gc.gridy = 0;
            add(player1Label, gc);

            //gc.gridx = 1;
            //gc.gridy = 0;
            add(player1ProgressBar, gc);

            gc.anchor = GridBagConstraints.EAST;

            //gc.gridx = 2;
            //gc.gridy = 0;
            add(player2Label, gc);

            //gc.gridx = 3;
            //gc.gridy = 0;
            add(player2ProgressBar, gc);
        }

        public void setPlayer1ProgressBarValue(int val) {
            player1ProgressBar.setValue(val);
        }

        public void setPlayer2ProgressBarValue(int val) {
            player2ProgressBar.setValue(val);
        }

        public void setPlayer2LabelText(String text) {
            player2Label.setText(text);
        }

        public void setPlayer1LabelText(String text) {
            player1Label.setText(text);
        }
    }
}
  • 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-14T10:19:22+00:00Added an answer on June 14, 2026 at 10:19 am
    • GlassPane is lightweight, then is always behind AWT Canvas,

    • this is quite common issue for JLayeredPane, GlassPane and Java and 3D, CAD/CAM, Open GL/Cl, these APIs are based on AWT Containers

    • same issue will be by using JLayer (lightweight container), but there you can to test isLightWe....() for peer from Native OS and/or its resources

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

Sidebar

Related Questions

Okay I have this RewriteRule which is supposed to redirect any request for the
Okay I have a list of devices where i can select which to edit.
Okay, this is getting complicated... Given situation: I have a canvas with the dimensions
Okay I have tried this from every angle I can think of and I
okay i have been trying to understand this for hours i am learning VB
Okay I have a series of objects based on a base class which are
Okay i have two models: posts and comments. as you can think comments has
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`
Okay I have a project in CMake structured like this: CMakeLists.txt /libfoo/CMakeLists.txt /frontend/qt/CMakeLists.txt libfoo
Okay I have searched and can't seem to come up with where to find

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.