重複の可能性:
Java Swing:Image
JFrameの取得
私は小さなドラッグアンドドロップのJavaGUIビルダーに取り組んでいます。これまでのところ機能しますが、ドラッグアンドドロップするウィジェットは、キャンバス上に動的に描画している単なる長方形です。
JButtonのようなウィジェットを表す長方形がある場合、JButtonを作成し、サイズを設定して、画面に描画された場合はそのJButtonの画像を取得する方法はありますか?そうすれば、退屈な長方形だけでなく、画像を画面にペイントすることができます。
たとえば、私は現在、(赤い)長方形を描くためにこれを行っています:
public void paint(Graphics graphics) {
int x = 100;
int y = 100;
int height = 100;
int width = 150;
graphics.setColor(Color.red);
graphics.drawRect(x, y, height, width);
}
どうすれば次のようなことができますか?
public void paint(Graphics graphics) {
int x = 100;
int y = 100;
int height = 100;
int width = 150;
JButton btn = new JButton();
btn.setLabel("btn1");
btn.setHeight(height); // or minHeight, or maxHeight, or preferredHeight, or whatever; swing is tricky ;)
btn.setWidth(width);
Image image = // get the image of what the button will look like on screen at size of 'height' and 'width'
drawImage(image, x, y, imageObserver);
}