Image オブジェクトではなく画像ファイルを表すために ImageIcon を使用したいのはどのような場合ですか? 私は検索しており、GUI の一部となる画像を扱うときに ImageIcon オブジェクトを使用すると言う人を見てきましたが、これが意味することはまだわかりません。言い換えれば、2 つのオブジェクト タイプの実際の違いは何で、それぞれがどのような状況に適しているのでしょうか?
3 に答える
AnImage
は、画像のデータ モデルを表すオブジェクトです。anは、画面上にImageIcon
を描画する Swing コンポーネントであり、適切な描画を提供する必要があります (既存の を渡すか、画像を見つけてロードするのに十分な情報を与えることによって)。Image
Image
Image
この関係は aString
と aの関係に似ていJTextField
ます。1 つはデータの表現であり、もう 1 つはデータを画面に描画するグラフィカル コンポーネントです。
Image
is an object that represents a bitmap: an array of pixels of different colors.
Icon
is an object that can draw a rectangular piece of graphics. Since it is an interface (and a simple one too), you can imagine many different types of icons: drawing predefined vector graphics, generating images on the fly etc. Therefore it is a useful abstraction and is used by Swing components (buttons, labels).
ImageIcon
is an object that IS an Icon
, but HAS-A Image
. That is - it draws graphics based on a specific image.
When you say "why should I be using an ImageIcon instead of Image" you miss the point: in fact you are using an Image
either way.
実装は、Swing スレッドを停止しないことです。
URL、ファイル名、またはバイト配列から作成された画像は、MediaTracker を使用してプリロードされ、画像のロード状態を監視します。
基本的に、事前にボタンを強制的に読み込まなくても、ボタンの ImageIcon を設定できます。
これは、非常に大きなアイコンを持ち、フレームのアイコンをこの画像に設定することで確認できます。可視に設定すると、実際に表示されるまでに数秒かかる場合があります。