当我运行这个类时,它会在框架的北中心打印图形,我不知道如何将它居中。我尝试以多种不同的方式调整 x 值,并注意到抛物线在正确的区域移动。
class FunctionPlotPanel extends JPanel
{
protected void paintComponent(Graphics g){
super.paintComponent(g);
int x1=0;
int y1=100;
int x2=200;
int y2=100;
g.drawLine( x1, y1, x2, y2);
int x11=100;
int y11=0;
int x22=100;
int y22=200;
g.drawLine(x11, y11, x22, y22);
Polygon p = new Polygon();
double scalefactor = 0.1;
for(int x=-100;x<=100;x++)
{
p.addPoint(x+100,100-(int)(scalefactor*x*x));
}
int[] xPoints=p.xpoints;
int[] yPoints=p.ypoints;
int nPoints=p.npoints;
g.drawPolyline(xPoints,yPoints,nPoints);
}
}