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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:56:38+00:00 2026-05-24T23:56:38+00:00

I have a JPanel on which some drawing is performed using paintComponent method and

  • 0

I have a JPanel on which some drawing is performed using paintComponent method and after that when ever a user clicks on the JPanel a string is drawn (or any drawing) on it and as the user moves mouse over the JPanel it shows the coordinates in the tooltip of the JPanel.

1) The problem is that when the tooltip comes over the drawn string it erases it but this tooltiptext has no erasing effect on the drawing part which I performed in paintComponent method. I am not able to understand that why this is happening.

2) And also when I draw string on click and then minimize and restore my application my drawn strings are gone.

Hope u all understand what I mean to say.

Here is the code :

@Override
public void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);
    Graphics2D graphics2D = (Graphics2D) graphics;
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    drawBorder(graphics2D);
    drawGrid(graphics2D);
}

private void drawBorder(Graphics2D graphics2D) {
    graphics2D.setColor(Color.ORANGE);
    graphics2D.setStroke(new BasicStroke(borderStroke));
    graphics2D.drawRoundRect(panelStartLoc.x, panelStartLoc.y, panelBorder.width,
            panelBorder.height, borderRoundness, borderRoundness);
}

private void drawGrid(Graphics2D graphics2D) {
    graphics2D.setColor(Color.ORANGE);
    graphics2D.setStroke(new BasicStroke(gridCellStroke));

    for (int row = gridStartLoc.x; row < panelBorder.getWidth(); row += cellWidth + cellHorGap) {
        for (int col = gridStartLoc.y; col < panelBorder.getHeight(); col += cellHeight + cellVerGap) {
            graphics2D.drawRoundRect(row, col, cellWidth, cellHeight, cellRoundness, cellRoundness);
        }
    }
}

public void drawSubjectAtClickLoc(int subjectCode) {
    Color color = getBackground();
    String drawString = null;
    int subjectDrawXLoc = cellClickLoc.x + 4;
    int subjectDrawYLoc = (cellClickLoc.y + cellHeight) - 3;
    Graphics2D graphics2D = (Graphics2D) getGraphics();

    if (subjectCode == SUBJECT_CLEAR) {
        graphics2D.setColor(getBackground());
        graphics2D.fillRoundRect(cellClickLoc.x + 2, cellClickLoc.y + 2, cellWidth - 4, 
                cellHeight - 4, cellRoundness, cellRoundness);
        return;
    }
    if (subjectCode == SUBJECT_HUMAN) {
        color = Color.WHITE;
        drawString = "H";
    }
    if (subjectCode == SUBJECT_RESOURCE) {
        color = Color.GREEN;
        drawString = "R";
    }

    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.setFont(new Font(null, Font.BOLD, 26));
    graphics2D.setColor(color);
    graphics2D.drawString(drawString, subjectDrawXLoc, subjectDrawYLoc);
}

thanx in advance….

  • 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-24T23:56:40+00:00Added an answer on May 24, 2026 at 11:56 pm

    When your screen is covered, Java calls paintComponent() to fix the screen. If you draw outside of the paintComponent() method, then when the screen is fixed up, your extra drawings will be erased.

    So don’t do it that way: do all of your drawing in paintComponent(). When the user clicks somewhere, add the string you want to draw and the coordinates to a data structure of some kind (i.e., a list of objects, each object containing a String and some coordinates), then call repaint(). In your paintComponent() method, look in that data structure and draw the strings.

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

Sidebar

Related Questions

I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I have a JPanel with two JTextFields. If the user writes some text into
I have a JPanel for which I set some image as the background. I
I have a class PanelFormes that extends JPanel, which I use as a container.
I have a JPanel onto which I'm trying to add some buttons. I have
1.Consider my code is on some line of a JPanel that I have, am
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel to which I'd like to add JPEG and PNG images
I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have a JPanel, which I would like to detect the following events (1)

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.