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のマイラーベルには何も表示されませんか?
を、[SSCCE](http://sscce.org/を投稿)。 –
[JLabelの背景色を設定するにはどうすればいいですか?](http://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – trashgod
なぜあなたはJLabel? JPanelが適切に見えます。正しくサイズを設定する(preferredSizeを設定し、周囲のコンテナにlayoutManagerを使用するか、パネルのサイズ/位置を強制するか) –