プロが教えるわが家の防犯対策術!

今晩は、ある画像を取込み、4分割に切抜き、ジグソーのようにラベルに張り付けたい
のですが、ラベルに読み込めません。
切り抜いた画像は、最後には、Imageにより、Image型としているのですが、何故読み込め
ないのでしょうか。

CropImageFilterとかCreateImageに加工すると読み込まないのでしょうか。

一枚ものの画像なら、読み込み表示もされますが、分割加工すると何故、Panel,Frameにも
張りつかないのでしょうか。

他に別の画像処理等が必要なのでしょうか、宜しくお願いします。

//=================================================================
public class LabelTest extends JFrame
{
private static final long serialVersionUID = 1L;

public Image img0 ; //元データー
public Image small_img0 ; //元データーを縮小
public ImageFilter fl0 , fl1 , fl2 , fl3 ;
public static int hh = 4 ;
public static Image[] ci = new Image[hh] ;
// FilteredImageの配列
public FilteredImageSource[] fis = new FilteredImageSource[hh] ;
public MediaTracker tracker;
public int w0 , h0 , wp3 , hp3 ;
public ImageIcon smallIcon00 ;
public Image img1 ;

//Constructor
public LabelTest()
{
img0 = Toolkit.getDefaultToolkit().getImage( "a.jpg" ) ;

tracker = new MediaTracker( this ) ;
tracker.addImage( img0 , 0 ) ;
try
{
//イメージのロードが完了するまで待機
tracker.waitForAll( ) ;
}
catch ( Exception ex )
{
ex.printStackTrace() ;
}

syukusyou();
bunkatuKurinuki();
makeCreateImage();
makeMainFrame();
}
//*****End of Constructor******
//*****イメージの縮小******
public void syukusyou()
{
//戻り値はImage型
small_img0 = img0.getScaledInstance
( ( int )( img0.getWidth( this ) * 0.8 ), -1 ,

Image.SCALE_SMOOTH ) ;

tracker.addImage( small_img0 , 0 ) ;
try
{
//イメージのロードが完了するまで待機
tracker.waitForAll( ) ;
}
catch ( Exception ex )
{
ex.printStackTrace() ;
}
}

//*****イメージの切抜(2分割)******
// ImageFilterの作成(fl ⇒ fis ⇒ createImage( fis )をcrpに代入
// ⇒ createImage( fis )をcrpに代入 ⇒ crpからcreateimg ⇒ creatimgをdrawする
// FilteredImageSource , fis0オブジェクトの作成
public void bunkatuKurinuki()
{
//(2分割)
if( small_img0 != null )
{
w0 = small_img0.getWidth( this ) ;
h0 = small_img0.getHeight( this ) ;
wp3 = w0/2 ; // small_img0 横分割
hp3 = h0/2 ; // small_img0 縦分割
}

//くりぬき(2分割)開始、FilteredImageSource , fis0オブジェクトの作成
//くりぬき(2分割)開始、ImageFilter型に返す

fl0 = new CropImageFilter( 0 , 0 , wp3 , hp3 ) ;
fis[0] = new FilteredImageSource( small_img0.getSource() , fl0 ) ;

fl1 = new CropImageFilter( wp3 , 0 , wp3 , hp3 ) ;
fis[1] = new FilteredImageSource( small_img0.getSource() , fl1 ) ;

fl2 = new CropImageFilter( wp3*2 , 0 , wp3 , hp3 ) ;
fis[2] = new FilteredImageSource( small_img0.getSource() , fl2 ) ;

fl3 = new CropImageFilter( 0 , hp3 , wp3 , hp3 ) ;
fis[3] = new FilteredImageSource( small_img0.getSource() , fl3 ) ;
}

// createImage( fis[] ) から"ci[i]" を作成 
public void makeCreateImage()
{
for( int i = 0 ; i < hh ; i ++ )
{
//ci[i]:Image型
ci[i] = createImage( fis[i] ) ;
}

for( int j = 0 ; j < hh ; j ++ )
{
tracker.addImage( ci[j] , 0 ) ;
}
}

public void makeMainFrame()
{
JFrame frame = new JFrame() ;
frame.setVisible( true ) ;
Container cnt = frame.getContentPane() ;

JPanel jpanel = new JPanel() ;
cnt.add(jpanel) ;

ImageIcon icon1 = new ImageIcon("ci[0]");
JLabel jlabel0 = new JLabel( icon1 ) ;
jpanel.add(jlabel0) ;

frame.setTitle( "Jigsaw" ) ;
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
frame.setSize( new Dimension( 400,400 ) ) ;
frame.setLocationRelativeTo( null ) ;
frame.setVisible( true ) ;
}
// main
public static void main( String[] args)
{
LabelTest ap = new LabelTest() ;
}
}

質問者からの補足コメント

  • HAPPY

    Tacosanさん、回答有難うございました。

    ご指摘の通り addNotify() メソッドの追加で動作確認が出来ました。
    有難うございました。

    今後ともよろしくお願いたします。

    No.2の回答に寄せられた補足コメントです。 補足日時:2015/09/29 09:58

A 回答 (2件)

とりあえず


ImageIcon icon1 = new ImageIcon("ci[0]");
はなんか違和感がある. 引数, これでいいんだろうか.
    • good
    • 0
この回答へのお礼

Tacosanさん、回答有難うございます。

実は、

ImageIcon icon1 = new ImageIcon("ci[0]");の部分は、
ImageIcon icon1 = new ImageIcon("ci[0].jpg");とか
ImageIcon icon1 = new ImageIcon(ci[0]);
ImageIcon icon1 = new ImageIcon(ci[0].jpg);
とかで試してはみましたが結果は同じでした。

他にどのような引数にすれば良いのかも思いつきません。
ほとほと困っています、宜しくご教示お願い致します。

お礼日時:2015/09/28 08:54

createImage でちょっと調べてみると


createImage を呼び出すタイミングによっては null を返すことがある
って出てくる. 今の場合と引数が違うのではずしてるかもしれんけど例えば
http://hiloshi.nce.buttobi.net/tips/gui/createim …
とか.

一応どこかのドキュメントにも書かれている, らしい.
この回答への補足あり
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!