スレッドを使用していくつかのアニメーションを描画しているため、フレームごとにラベルを再描画する必要があります。ちらつきなしでこれを行うために、バックバッファ グラフィックス オブジェクトでラベルを更新しています (lbl.update(bufferedGraphics); メソッドを使用) が、これを行うと、ラベルは Graphics オブジェクトの左上に再描画されます。 setLocation が指定されました。
ラベルを所有するパネル内ではなく、グラフィックス内でラベルの位置を指定するにはどうすればよいですか?
SSCCE は次のとおりです。
import javax.swing.*;
import java.awt.*;
public class LabelSSCCE extends JApplet implements Runnable {
JPanel pnl;
JLabel lbl;
Image buffer;
Graphics bufferedGraphics;
Thread t;
public void init (){
pnl = new JPanel (null);
lbl = new JLabel ();
lbl.setText ("Text");
lbl.setOpaque(true);
add(pnl);
pnl.add (lbl);
lbl.setLocation(100, 100);
lbl.setBounds (100, 100, 200, 20);
buffer = createImage (500, 500);
bufferedGraphics = buffer.getGraphics ();
t = new Thread (this, "Label");
t.start ();
}// init method
public void paint (Graphics g){
if (g != null)
g.drawImage (buffer, 0, 0, this);
}//paint
public void update (Graphics g){
paint (g);
}//update
public void render (){
bufferedGraphics.setColor (Color.WHITE);
bufferedGraphics.fillRect (0, 0, 500, 500);
lbl.update (bufferedGraphics);
update(getGraphics());
}//render
public void run (){
while (true){
try{
render ();
t.sleep (20);
} catch (InterruptedException e){
e.printStackTrace ();
}//catch
}//while
}//run
}//LabelSSCCE