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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:56:30+00:00 2026-05-30T22:56:30+00:00

I am having trouble with the Graphics that I am drawing on a Panel

  • 0

I am having trouble with the Graphics that I am drawing on a Panel which has a JLabel on top of it. I am using a custom paintComponent(Graphics g) method to draw a String on the Panel. I want the string to show on top of the JLabel instead of under the JLabel, which is what is currently happening. Here is the paintComponent method if you want to see it:

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

    if(a.shouldAnnotate()){
        FontMetrics size= g.getFontMetrics(); 
        if(getWidth()>=(a.dispX()+size.stringWidth(a.annotationText()))){
            g.setColor(Color.white);
            g.fillRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
            g.setColor(Color.black);
            g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
            g.drawString(a.annotationText(), a.dispX(), a.dispY());
        }else{
            String sub="";
            int letters=0;
            g.setColor(Color.white);
            g.fillRect(a.dispX()-3,a.dispY()-12,getWidth(),15);
            g.setColor(Color.black);

            for(int i=0;i<a.annotationText().length();i++){
                if(a.dispX()+letters+16<=getWidth()){
                    sub+=a.annotationText().substring(i,i+1);
                    letters=size.stringWidth(sub);
                }else{
                    sub=sub+"...";
                    i=a.annotationText().length();
                }
            }
            g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(sub)+3,15);
            g.drawString(sub,a.dispX(),a.dispY());
        }
    }
}

I don’t like graphics much but I need to learn the fix of this problem.

  • 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-30T22:56:32+00:00Added an answer on May 30, 2026 at 10:56 pm

    To do this I think that you must either override paint or paintChildren, and draw your text there:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class DrawOverLabel extends JPanel {
       private static final int PREF_W = 500;
       private static final int PREF_H = 100;
       private static final Font TEXT_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 32);
       private static final int LABEL_COUNT = 40;
    
       public DrawOverLabel() {
          for (int i = 0; i < LABEL_COUNT; i++) {
             JLabel label = new JLabel("Label");
             label.setForeground(Color.green);
             label.setFont(label.getFont().deriveFont(Font.BOLD, 20));
             add(label);
          }
       }
    
       @Override
       public void paint(Graphics g) {
          super.paint(g);
    
          g.setColor(Color.blue);
          g.setFont(TEXT_FONT);
          g.drawString("in paint", 20, 30);
    
    //      super.paint(g);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
    
          g.setColor(Color.red);
          g.setFont(TEXT_FONT);
          g.drawString("in paintComponent", 20, 60);
    
    //      super.paintComponent(g);
       }
    
       @Override
       protected void paintChildren(Graphics g) {
          super.paintChildren(g);
    
          g.setColor(Color.gray);
          g.setFont(TEXT_FONT);
          g.drawString("in paintChildren", 20, 100);
    
    //       super.paintChildren(g);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("DrawOverLabel");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new DrawOverLabel());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    But a better solution is to use a JLayeredPane or the glasspane for this effect.

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

Sidebar

Related Questions

I am having trouble inserting an image on top of a paint method that
I'm having trouble with a gradient drawing call. I have a Form that looks
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having trouble with a mysql grant statement. I want a user who has: readonly
I'm having trouble with printing a rectangle to a shell using PaintListeners and GCs
having some trouble with a Thread (CanvasThread) that is intermittently pausing at random points
I'm not quite sure exactly how Graphics work in Java so I having trouble
Having trouble with Delphi XE2 (update2) using MS SQL 2008 R2 (sp 2) or
Having trouble using libstatgrab -- I receive the following error at compile time: libstatgrabTest.cpp:16:
I'm trying to teach myself about 3D graphics, but I'm having trouble visualizing the

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.