显示函数图像。编写程序,画出函数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);
}
}