2012-03-31 5 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のマイラーベルには何も表示されませんか?

+1

を、[SSCCE](http://sscce.org/を投稿)。 –

+0

[JLabelの背景色を設定するにはどうすればいいですか?](http://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – trashgod

+0

なぜあなたはJLabel? JPanelが適切に見えます。正しくサイズを設定する(preferredSizeを設定し、周囲のコンテナにlayoutManagerを使用するか、パネルのサイズ/位置を強制するか) –

答えて

2

次のコードは私の仕事です。コンストラクタ内のpreferredSizeがの設定に注意してください。

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

ラベルが不透明な場合は、にrepaint()を呼び出す必要があります。

+0

showData()ではrepaintを呼び出すのが適切でしょうか? super.repaint()として呼び出す必要がありますか? – Saurabh

+0

モデルが変更されるたびに 'repaint()'を呼び出します。 'super'の実装ではなく実装を使うべきです。 – trashgod

+1

@SaurabhとAndrewはSSCCEであなたの質問を編集していると言いますが、問題はあなたのコードの残りの部分(以前は+1) – mKorbel

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; 
     } 
     } 
     } 
} 
関連する問題