JPanel を拡張し、paintComponent メソッドをオーバーライドするクラス A があります。今、クラス A のオブジェクトを作成する別のクラス B があります。B から A の update メソッドを呼び出す方法は? グラフィックを更新するだけで、最初から描画しないメソッド。
JLabel(クラスAオブジェクト)のupdate(Graphics g)メソッドを呼び出している場合でも、新しいオブジェクトから描画され、古いオブジェクトは保持されません。
クラス A のコード スニペット:
class MyLabel extends JLabel
{
int[] x,y;
int total;
MyLabel(ImageIcon i)
{
super(i);
total=0;
}
@Override
public void update(Graphics g)
{
super.update(g);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(total!=0)
{
g.setColor(Color.RED);
g.drawPolygon(x, y, total);
}
}
public void setPoints(int xx[], int yy[], int tt)
{
total = tt;
x = new int[tt];
y = new int[tt];
System.arraycopy(xx, 0, x, 0, tt);
System.arraycopy(yy, 0, y, 0, tt);
}
}
これはクラス B のコード スニペットです。
sf.map_label.setPoints(x,y,total);
sf.map_label.update(sf.map_label.getGraphics());
sf.map_label.setVisible(false);
sf.map_label.setVisible(true);