塗りつぶしと描画のパフォーマンスを向上させるには、次の方法を使用してください。
ダブルバッファリング (たとえば、アニメーションではオフスクリーンにイメージを描画して全体を一度に読み込みます)
update() によるデフォルト値以外の使用 (オーバーライド)
public void update(Graphics g) {
paint(g);
}
カスタマイズした独自のレイアウトマネージャ の使用。独自の動作が必要な場合は、そのためのコードを作成することによって、最高の GUI パフォーマンスを得ることができます。
イベントの使用。JDK 1.1 には、1.0 に比べて効率的なイベントモデルが用意されています。
損傷を受けた部分だけの再描画 (ClipRect を使用)。
非同期の読み込みパフォーマンスを向上させるには、独自の imageUpdate() メソッドを使用して imageUpdate() をオーバーライドします。imageUpdate() は、必要以上に再描画を行うことがあります。
//wait for the width information to be loaded
while (image.getWidth(null) == -1 {
try {
Thread.sleep(200);
}
catch(InterruptedException e) {
}
}
if (!haveWidth) {
synchronized (im) {
if (im.getWidth(this) == -1) {
try {
im.wait();
}
catch (InterruptedException) {
}
}
}
//If we got this far, the width is loaded, we will never go thru
// all that checking again.
haveWidth = true;
}
...
public boolean imageUpdate(Image img, int flags, int x, int y, int width, int height) {
boolean moreUpdatesNeeded = true;
if ((flags&ImageObserver.WIDTH)!= 0 {
synchronized (img) {
img.notifyAll();
moreUpdatesNeeded = false;
}
}
return
moreUpdatesNeeded;
}
|
イメージのデコードは、読み込みより長い時間がかかります。PixelGrabber と MemoryImageSource を使用して事前にデコードすることによって、複数のイメージを 1 つのファイルにまとめ、最高の速度が得ることができます。この方法は、ポーリングを行うよりも効率的です。