显示函数图像

显示函数图像。编写程序,画出函数f(x)=x*x的图像。输出示例如下图所示。

package priv.lhw.function.image;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
public class Function extends JPanel {
    public static final double ACCURACY = 0.01;
    public static final int STEP = 10000;
    private double function(double x){
        return x * x;
    }
    @Override
    public void paintComponent(Graphics graphics){
        super.paintComponent(graphics);
        Graphics2D graphics2D = (Graphics2D) graphics;

        //原点
        double xCenter = (double) (getWidth() / 2);
        double yCenter = (double) getHeight() / 2;
        graphics2D.drawString("O", getWidth() / 2 - 30, getHeight() / 2 + 30);

        //x轴
        Point2D xStart = new Point2D.Double((double) getWidth() / 4,(double) getHeight() / 2);
        Point2D xEnd = new Point2D.Double((double) getWidth() / 4 * 3,(double) getHeight() / 2);
        Line2D xLine = new Line2D.Double(xStart, xEnd);
        graphics2D.draw(xLine);
        Line2D xArrow1 = new Line2D.Double(xEnd, new Point2D.Double(xEnd.getX() - 10, xEnd.getY() - 10));
        Line2D xArrow2 = new Line2D.Double(xEnd, new Point2D.Double(xEnd.getX() - 10, xEnd.getY() + 10));
        graphics2D.draw(xArrow1);
        graphics2D.draw(xArrow2);
        graphics2D.drawString("X", getWidth() / 4 * 3 - 30, getHeight() / 2 + 30);
        //y轴
        Point2D yStart = new Point2D.Double((double) getWidth()/2,0);
        Point2D yEnd = new Point2D.Double((double) getWidth()/2,getHeight());
        Line2D yLine = new Line2D.Double(yStart, yEnd);
        graphics2D.draw(yLine);
        Line2D yArrow1 = new Line2D.Double(yStart, new Point2D.Double(yStart.getX() - 10, yStart.getY() + 10));
        Line2D yArrow2 = new Line2D.Double(yStart, new Point2D.Double(yStart.getX() + 10, yStart.getY() + 10));
        graphics2D.draw(yArrow1);
        graphics2D.draw(yArrow2);
        graphics2D.drawString("Y", getWidth()/2 + 30, 30);

        graphics2D.drawString("Y = x ^ 2", 180, 120);

        double x, y;
        x = 0;
        y = function(x);
        Point2D p1 = new Point2D.Double(xCenter, yCenter);
        while (yCenter - y > ACCURACY){
            x = x + 1.0 / STEP;
            y = function(x) / 10;

            Point2D p2 = new Point2D.Double(xCenter + x, yCenter - y);
            graphics2D.draw(new Line2D.Double(p1, p2));
            graphics2D.draw(new Line2D.Double(new Point2D.Double(p1.getX() - 2 * x, p1.getY()),
                    new Point2D.Double(p2.getX() - 2 * x, p2.getY())));
            p1 = p2;
        }
    }
}

package priv.lhw.function.image;

import javax.swing.*;
import java.awt.*;

public class CenteredFrame extends JFrame {
    public CenteredFrame(){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();
        int screenHeight = dimension.height;
        int screenWidth = dimension.width;

        setSize(screenWidth/2, screenHeight/2);

        int x = (screenWidth - getWidth()) / 2;
        int y = (screenHeight - getHeight()) / 2;
        setLocation(x, y);

        Image image = toolkit.getImage("src/priv/lhw/function/image/icon.GIF");
        setIconImage(image);

        setTitle("函数图像");
    }
}

package priv.lhw.function.image;

import javax.swing.*;

public class Main {

    public static void main(String[] args) {
	// write your code here
        CenteredFrame centeredFrame = new CenteredFrame();
        centeredFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        centeredFrame.setVisible(true);

        Function function = new Function();
        centeredFrame.add(function);
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值