编写程序,画出函数f(x)=x*x的图像用java如何写?
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 03:48:27
编写程序,画出函数f(x)=x*x的图像用java如何写?
编写程序,画出函数f(x)=x*x的图像用java如何写?
编写程序,画出函数f(x)=x*x的图像用java如何写?
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class hanshu
{
\x09public static void main(String[] args)
\x09{
\x09\x09DrawFrame frame=new DrawFrame();
\x09\x09frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
\x09\x09frame.setVisible(true);
\x09}
}
class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle("DrawTest");
setSize(400,400);
// add panel to frame
DrawPanel panel = new DrawPanel();
add(panel);
}
}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int centerx=0;
int centery=0;
int minx=-150;
int maxx=150;
int miny=-150;
int maxy=150;
g2.translate(200,200);
g2.draw(new Line2D.Double(minx,centery,maxx,centery));
g2.draw(new Line2D.Double(maxx,centery,maxx-1,centery-1));
g2.draw(new Line2D.Double(maxx,centery,maxx-1,centery+1));
g2.drawString("X",151,0);
g2.draw(new Line2D.Double(centerx,miny,centerx,maxy));
g2.draw(new Line2D.Double(centerx,miny,centerx-1,miny+1));
g2.draw(new Line2D.Double(centerx,miny,centerx+1,miny+1));
g2.drawString("Y",-7,-145);
g2.drawString("Y=X*X",5,150);
int[] arrayy=new int[200];
int[] arrayx=new int[200];
\x09 arrayx[0]=-100;
\x09 for (int i=0;i