1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Saurabh
  • 121
  • 1
  • 2
  • 12
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 31 '12 at 14:37
  • possible duplicate of [How do I set a JLabel's background color?](http://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – trashgod Mar 31 '12 at 14:41
  • Why do you use a JLabel? a JPanel looks more appropriate. Make sure to size it correctly (either set the preferredSize and use a layoutManager in the surrounding containers, either force the size/location of the panel) – Guillaume Polet Mar 31 '12 at 14:43
  • @trashgod: opacity is set to true, if i display a bufferedImage on it works absolutely fine, but histogram of same image doesn't get dsplayed!! – Saurabh Mar 31 '12 at 14:46
  • Did you set the size of your JLabel? or did you forced its preferredSize? If not, then the size of the label will be 0,0 and it will never get painted – Guillaume Polet Mar 31 '12 at 15:11

3 Answers3

2

If the label is opaque, you may need to invoke repaint() in showData().

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Where in showData() would it be appropriate to call repaint? and it should be called as super.repaint() right? – Saurabh Mar 31 '12 at 14:53
  • Call `repaint()` each time your model changes; you should use your implementation, not that of `super`. – trashgod Mar 31 '12 at 14:58
  • 1
    @Saurabh and Andrew mentioned edit your question with SSCCE, issue(s) could be somewhere in the rest of your code (previously +1) – mKorbel Mar 31 '12 at 15:05
2

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);    
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
1

Have a look at this:

/**
 * 
 */
package org.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @author Sinisa
 *
 */
public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel jLabel = new JLabel();
        Test t = new Test();
//      jLabel.add();
        frame.add(t.new showData(new int[]{1, 2, 3},200,200));
        frame.setVisible(true);
        frame.setSize(new Dimension(800, 600));
    }


    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, 3);
            this.w = w;
            this.h = h;
//          this.setText("sds");
            for (int i = 0; i < 256; i++) {
                if(hist[i]>max_hist)
                    max_hist=hist[i];
            }
        }


        /**
        * {@inheritDoc}
        */
        @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;
        }
        }
        }
}
Sinisha Mihajlovski
  • 1,811
  • 1
  • 20
  • 34