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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:54:26+00:00 2026-06-01T09:54:26+00:00

I am trying to show a histogram for a image in jlabel, but its

  • 0

I am trying to show a histogram for a image in jlabel, but its not working.

//hist : array containing histogram values
//wid: width of image
//ht: height of image
mylabel.add(new showData(hist,wid,ht));

The code that I am using to display histogram is:

class showData extends JLabel{
int w,h;
int hist[] = new int[256];
int max_hist=0;
public showData(int[] histValue,int w, int h) {
    System.arraycopy(histValue, 0, hist, 0, 256);
    this.w = w;
    this.h = h;
    for (int i = 0; i < 256; i++) {
        if(hist[i]>max_hist)
            max_hist=hist[i];
    }
}


@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int x = (w - 256) / 2;
int lasty = h - h * hist[0] / max_hist;
for (int i=0; i<256; i++, x++) {
  int y = h - h * hist[i] / max_hist;
  g.setColor(new Color(i, i, i));
  g.fillRect(x, y, 1, h);
  g.setColor(Color.red);
  g.drawLine(x-1,lasty,x,y);
  lasty = y;
}
}
}

When debugged, I found that showData() method was getting invoked, but paintComponent() doesn’t. Why is it so? The Jlabel ‘mylabel’ doesn’t show anything?

  • 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-01T09:54:27+00:00Added an answer on June 1, 2026 at 9:54 am

    The following code works for me. Pay attention to the setting of the preferredSize in the constructor:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class ShowData extends JLabel {
        int w, h;
        int hist[];
        int max_hist = 0;
    
        public ShowData(int[] histValue, int w, int h) {
            hist = new int[histValue.length];
            System.arraycopy(histValue, 0, hist, 0, histValue.length);
            this.w = w;
            this.h = h;
            setPreferredSize(new Dimension(w, h * 2));
            for (int i = 0; i < hist.length; i++) {
                if (hist[i] > max_hist) {
                    max_hist = hist[i];
                }
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            int x = (w - hist.length) / 2;
            int lasty = h - h * hist[0] / max_hist;
            for (int i = 0; i < hist.length; i++, x++) {
                int y = h - h * hist[i] / max_hist;
                g.setColor(new Color(i, i, i));
                g.fillRect(x, y, 1, h);
                g.setColor(Color.red);
                g.drawLine(x - 1, lasty, x, y);
                lasty = y;
            }
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ShowData data = new ShowData(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 100,
                 100);
            frame.add(data);
            frame.pack();
            frame.setVisible(true);    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to show ProgressDialog, but getting: Can't create handler inside thread that has not
HI Im trying to show an image in a gridview, its held on the
Im trying to show an image in an webview but my app crashes as
I am trying to show the loop data in Lineedit but it is not
I'm trying to show custom UILabel in UITableViewCell, but something wrong. My code: -
I am trying to show five product-related thumbnails just below the product image (to
I am trying to show a alertdialog but i keep getting this error 01-24
Im trying to show a square polygon with the 100% of the width of
I am trying to generate a color histogram of an image. I am using
I'm trying show on views (each row) my custom form. It works, but 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.