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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:34:07+00:00 2026-05-31T17:34:07+00:00

I have a JPanel that implements custom drawing to draw a background. Over this,

  • 0

I have a JPanel that implements custom drawing to draw a background. Over this, the application can place JButtons to detect clicks on certain areas of the JPanel. However, when highlighting the (non-opaque) buttons with the mouse, the underlying JPanel’s graphics become all glitchy.

My game

These are 9 JPanels with the custom drawing, each of them with 2 filling JButtons (R and L). The top right block and the ones looking like it are ‘fresh’. The bottom right has both buttons highlighted, the middle one only the ‘R’, etc.

I create my buttons like this:

rotatePanel = new JPanel();
        rotatePanel.setOpaque(false);
        GridLayout rotateLayout = new GridLayout(1, 2);
        rotatePanel.setLayout(rotateLayout);

        rotateRight = new JButton("R");
        rotateRight.addActionListener(this);
        rotateRight.setOpaque(false);
        rotateRight.setContentAreaFilled(false);
        rotateRight.setBorderPainted(false);
        rotatePanel.add(rotateRight);

        rotateLeft = new JButton("L");
        rotateLeft.addActionListener(this);
        rotateLeft.setOpaque(false);
        rotateLeft.setContentAreaFilled(false);
        rotateLeft.setBorderPainted(false);
        rotatePanel.add(rotateLeft);

and this is my drawing code:

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

    Rectangle clipRect = g.getClipBounds();
    clipRect.grow(-4, -4);

    int thirdWidth = clipRect.width/3;
    int thirdHeight = clipRect.height/3;
    for (int x = 0; x < Board.DIM; x++) {
        //Draw the columns
        for (int y = 0; y < Board.DIM; y++) {
            //Draw the rows
            g.drawRect(thirdWidth * x, thirdHeight * y, thirdWidth, thirdHeight);
        }
    }

    g.setColor(Color.BLACK);
    g.drawRect(0, 0, clipRect.width, clipRect.height);
}
  • 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-31T17:34:08+00:00Added an answer on May 31, 2026 at 5:34 pm

    When changes to a child component require part of your panel to be redrawn, only the area overlapped by that component needs to be drawn. When you do g.getClipBounds(), you are getting the region to be painted, not the bounds of your whole panel. You probably just want to use getWidth() and getHeight().

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

Sidebar

Related Questions

How can i draw 2d in a JPanel that i have on my GUI?
I need to have a JToggleButton (that has custom background) that contains a JPanel
I have a JScrollPane with a JPanel that implements Scrollable as its viewport view.
I have an application that bounces Shapes around inside a JPanel. Whenever shapes hit
I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel
I have a JPanel that encapsulates two JPanels, one on top of the other.
I have a JPanel that handles a bunch of settings that I would like
I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
I have created a Hexagon component that extends JPanel . It draws a hexagon
Give that I have written a JPanel with many different components on it, is

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.