我有一个A类,它扩展了JPanel并覆盖了paintComponent方法。现在我有另一个类B创建一个A类对象。现在如何从B调用A的更新方法?一种只更新图形而不从开始绘制的方法。
即使我正在调用JLabel(A类对象)的update(Graphics g)方法,它也会从new绘制并且不会保留旧对象。
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);