2012-03-31 104 views
1

我想在jlabel中显示图像的直方图,但它不工作。paintComponent方法在JLabel上不显示任何内容?

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

,我使用来显示直方图的代码是:

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; 
} 
} 
} 

当调试,我发现,是越来越调用showData()方法,但的paintComponent()没有。为什么这样? Jlabel'mylabel'没有显示任何内容?

+1

为了更好地帮助越早,张贴[SSCCE](http://sscce.org/ )。 – 2012-03-31 14:37:41

+0

可能的重复[如何设置JLabel的背景颜色?](http://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – trashgod 2012-03-31 14:41:46

+0

为什么你使用JLabel的? JPanel看起来更合适。确保正确调整大小(或者设置preferredSize并在周围容器中使用layoutManager,或者强制面板的大小/位置) – 2012-03-31 14:43:10

回答

2

以下代码适用于我。在构造函数中要注意首选大小的设置:

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);  
    } 
} 
2

如果标签不透明,您可能需要在showData()中调用repaint()

+0

在showData()中的哪个位置适合调用repaint?它应该被称为super.repaint()正确吗? – Saurabh 2012-03-31 14:53:00

+0

每次模型更改时调用'repaint()';你应该使用你的实现,而不是'super'。 – trashgod 2012-03-31 14:58:55

+1

@Saurabh和Andrew提到用SSCCE编辑你的问题,问题可能在代码的其他部分(以前为+1) – mKorbel 2012-03-31 15:05:30

1

看一看这样的:

/** 
* 
*/ 
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; 
     } 
     } 
     } 
}