嗨,我在尝试绘制多边形时遇到了麻烦。首先,当我尝试使用 usingaddPoint(int x, int y)
方法绘制多边形并一个一个地给出坐标时,没有问题,可以完美地绘制多边形。但是,如果我将坐标作为数组(x 坐标和 y 坐标的整数数组)给出,编译器会出错。如您所见,这是工作代码,
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Polygon poly = new Polygon();
poly.addPoint(150, 150);
poly.addPoint(250, 100);
poly.addPoint(325, 125);
poly.addPoint(375, 225);
poly.addPoint(450, 250);
poly.addPoint(275, 375);
poly.addPoint(100, 300);
g2.drawPolygon(poly);
}
但是如果我使用xpoints
andypoints
数组(在多边形的 Graphics 类中定义)它不能正常工作。
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Polygon poly = new Polygon();
poly.xpoints[0]=150;
poly.xpoints[1]=250;
poly.xpoints[2]=325;
poly.xpoints[3]=375;
poly.xpoints[4]=450;
poly.xpoints[5]=275;
poly.xpoints[6]=100;
poly.ypoints[0]=150;
poly.ypoints[1]=100;
poly.ypoints[2]=125;
poly.ypoints[3]=225;
poly.ypoints[4]=250;
poly.ypoints[5]=375;
poly.ypoints[6]=300;
g2.drawPolygon(poly.xpoints, poly.ypoints, 7);
}
如果您能提供帮助,我将不胜感激,无论如何,谢谢。