我写了一个程序,将GPS中的坐标打印到屏幕上作为散点。 这是代码:
class POSCanvas extends JPanel{
int cnt = 1;
private static final long serialVersionUID = 1L;
List<Vector2D> pnts = new ArrayList<Vector2D>();
public void getNewPoint (double xnew, double ynew){
Vector2D pnt = new Vector2D(cnt*xnew, cnt*ynew);
pnts.add(pnt);
cnt++;
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (!pnts.isEmpty()){
super.paintComponent(g);
g2.setColor(Color.white);
Dimension size = getSize();
Insets insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
for (int i = 0; i < pnts.size(); i++ ){
Ellipse2D.Double OV = new Ellipse2D.Double(pnts.get(i).getX() % w, size.height - pnts.get(i).getY() % h, 5, 5);
g2.draw(OV);
repaint();
}
}
}
}
绘图的问题在于,当坐标较大时,绘图无意义的面板尺寸,绘图再次从左侧开始。这是一个例子:
我想动态调整面板的大小,以便坐标始终与其边界保持一致。我怎么能这样做?