JAVA实现数学函数图像
基础知识
在java.lang包中有个public final Math类,包含了基本的数字操作,如指数、对数、平方根和三角函数。
【Java的默认包是java.lang,即该包中的所有类不需要导包,不需要再写import,是默认导入的,其它包必须导入。Java就这么定义的,记住就可以了】。
java.lang.Math类中包含E和PI两个静态常量,以及进行科学计算的类(static)方法,可以直接通过类名调用。
public static final Double E = 2.7182818284590452354
public static final Double PI = 3.14159265358979323846
public static long abs(double x):传回 x 的绝对值。X也可int long float
public static long sin(double x): 传回x径度的正弦函数值
public static long cos(double x):传回x径度的余弦函数值
public static long tan(double x): 传回x径度的正切函数值
public static long asin(double x):传回x值的反正弦函数值。
public static long acos(double x):传回x值的反余弦函数值。
public static long atan(double x):传回x值的反正切函数值。
public static long atan2(double x, double y):传回极坐标(polar)的θ值
public static long floor(double x):传回不大于x的最大整数值
public static long ceil(double x):传回不小于x的最小整数值。
public static long exp(double x):传回相当于ex值
public static long log(double x):传回x的自然对数函数值
public static long max(double x,double y):传回x、y较大数
public static long min(double x,double y):传回x、y较小数
public static long pow(double x,double y):传回x的y次幂值
public static long sqrt(double x): 传回x开平方值
public static long rint(double x):传回最接近x的整数值
public static long round(double x):传回x的四舍五入值
public static long toDegrees(double angrad):传回将angrad径度转换成角度
public static long toRadians(double angdeg): 传回将angdeg角度转换成径度
public static long random():传回随机数值,产生一个0-1之间的随机数(不包括0和1)
关于 Java图形用户界面 可参见 https://blog.csdn.net/cnds123/article/details/113251233
下面给出多个示例代码,取自网络。
一、绘制sin函数图像
先看效果图
源码文件DrawSin.java内容如下:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class DrawSin extends JFrame {
private static int SCALE_X = 40; // X轴缩放倍数
private static int SCALE_Y = 100; // Y轴缩放倍数
private static int ORIGIN_X = 50; // 原点X
private static int ORIGIN_Y = 0; // 原点Y
private static int END_ARC = 360 * 2; // 画多长
public void paint(Graphics g) {
double ox = 0, oy = 0, x = 0, y = 0, arc = 0;
super.paint(g);
ORIGIN_Y = this.getHeight() / 2;
// 画坐标轴
g.drawLine(ORIGIN_X, ORIGIN_Y, this.getWidth(), ORIGIN_Y); // 横轴
g.drawLine(ORIGIN_X, 0, ORIGIN_X, this.getHeight()); // 纵轴
// 每90度画个标尺
for (int i = 0; i < END_ARC; i += 90) {
arc = Math.PI * i * 2 / 360;
x = ORIGIN_X + arc * SCALE_X;
g.drawLine((int) x, ORIGIN_Y - 10, (int) x, ORIGIN_Y + 10);
}
// 画正弦曲线
g.setColor(Color.RED);
for (int i = 0; i < END_ARC; i += 10) {
arc = Math.PI * i * 2 / 360;
x = ORIGIN_X + arc * SCALE_X;
y = ORIGIN_Y + Math.sin(arc) * SCALE_Y;
if (arc > 0) {
g.drawLine((int) ox, (int) oy, (int) x, (int) y);
}
ox = x;
oy = y;
}
}
public static void main(String[] args) {
DrawSin wnd = new DrawSin();
wnd.setSize(600, 500);
wnd.setVisible(true);
}
}
二、画函数图像
先看效果图
源码文件DrawFn.java 内容如下:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class DrawFn extends JFrame implements ItemListener{
private JTextField txt_c;
private JTextField txt_b;
private JTextField txt_a;
public JComboBox chooseFun;
//draw_actionAdapter adapter;
public int A;
public drawFnPanel panel = new drawFnPanel();
public static void main(String[] args) {
DrawFn frame=new DrawFn();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setBackground(Color.BLACK);
}
public DrawFn() {
super("画函数图像");
final JLabel aLabel = new JLabel();
aLabel.setForeground(Color.WHITE);
aLabel.setText("a=");
aLabel.setBounds(650, 10, 21, 18); //设置位置,大小
getContentPane().add(aLabel);
txt_a = new JTextField();
txt_a.setBounds(680, 8, 60, 18);
getContentPane().add(txt_a);
//
final JLabel bLabel = new JLabel();
bLabel.setForeground(Color.WHITE);
bLabel.setText("b=");
bLabel.setBounds(650, 30, 21, 18);
getContentPane().add(bLabel);
txt_b = new JTextField();
txt_b.setBounds(680, 28, 60, 18);
getContentPane().add(txt_b);
//
final JLabel cLabel = new JLabel();
cLabel.setForeground(Color.WHITE);
cLabel.setText("c=");
cLabel.setBounds(650, 50, 32, 18);
getContentPane().add(cLabel);
//this.setContentPane(cLabel);
txt_c = new JTextField();
txt_c.setBounds(680, 48, 60, 18);
getContentPane().add(txt_c);
//this.setContentPane(txt_c);
//设置按钮
final JButton button = new JButton();
button.addActionListener(new draw_actionAdapter(this));//什么意思?
button.setText("绘制");
button.setBounds(680, 80, 60, 18);
getContentPane().add(button);
//this.setContentPane(button);
//定义下拉菜单
final JLabel choose = new JLabel();
choose.setForeground(Color.WHITE);
choose.setText("请选择函数类型:");
choose.setBounds(20, 5, 120, 20);
getContentPane().add(choose);
JPanel jp1=new JPanel();
String []fun={"ax^2+bx+c","ae^bx+c","a*sin(PIx+b)+c","a*b^x+c","a*x^b+c","敬请期待"};
chooseFun=new JComboBox(fun);
chooseFun.setBounds(20, 30, 120, 20);
jp1.add(chooseFun);
chooseFun.setMaximumRowCount(3);
getContentPane().add(chooseFun);
chooseFun.addItemListener(this);
getContentPane().add(panel);
//this.setContentPane(panel);
}
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==e.SELECTED)
{
A=chooseFun.getSelectedIndex();
//System.out.println(A);
}
}
public void paintFn(ActionEvent e){
panel.paintFn(Integer.parseInt(txt_a.getText()), Integer.parseInt(txt_b.getText()), Integer.parseInt(txt_c.getText()));
}
class draw_actionAdapter implements ActionListener{
private DrawFn adapter;
public draw_actionAdapter(DrawFn adapter){
this.adapter=adapter;
}
public void actionPerformed(ActionEvent e){
//adapter.getA(e);
adapter.paintFn(e);
adapter.repaint();
}
}
class drawFnPanel extends JPanel{
private float fa;
private float fb;
private float fc;
private int UnitLength=100;//可以任意改变该像素值
public void paintFn(int a,int b,int c){
fa=a;
fb=b;
fc=c;
}
public double Fun(double x){
//System.out.println("A="+DrawFn.A);
if(A==0)
return fa*x*x+fb*x+fc;
else if(A==1)
return fa*Math.pow(Math.E, fb*x)+fc ;//这里可以输入任何函数
else if(A==2)
return fa*Math.sin(Math.PI*x+fb)+fc;
else if(A==3)
return fa*Math.pow(fb, x)+fc;
else if(A==4)
return fa*Math.pow(x,fb)+fc;
else
return 0;
}
int width,height;
int X,Y;
//重载paintComponent函数
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
width = this.getWidth();//获得宽度
height = this.getHeight();//获得高度
X=width/2;
Y=height/2;//获得原点坐标
this.drawAxes(g);
this.function(g);
}
//画坐标轴
private void drawAxes(Graphics g)
{
g.setColor(Color.WHITE);
g.drawLine(0, Y, width, Y);
g.drawLine(X, 0, X, height);
g.drawString("0",X + 2,Y +12); //画原点数字
for(int i=1;i*UnitLength<width;i++)
{
g.drawLine(X+i*UnitLength,Y-1,X+i*UnitLength,Y-6);//画X轴正向的小竖线
g.drawLine(X - i*UnitLength, Y-1, X - i*UnitLength, Y-6);//画X轴负向的小竖线
g.drawString(String.valueOf(i), X + i*UnitLength-3, Y + 12); // x轴正向数字
g.drawString(String.valueOf(i*-1), X - i*UnitLength-3, Y + 12); // x轴负向数字
//画Y轴
g.drawLine(X+1,Y+i*UnitLength,X+6,Y+i*UnitLength);
g.drawLine(X+1,Y-i*UnitLength,X+6,Y-i*UnitLength);
g.drawString(String.valueOf(i), X-12, Y - i*UnitLength-3);
g.drawString(String.valueOf(i*-1), X-12, Y + i*UnitLength-3);
}
}
//实现任意函数函数图像
public void function(Graphics g1)
{
Point2D temp1,temp2;
double x,y;//我们看到的坐标值
Graphics2D g = (Graphics2D)g1;
g.setColor(Color.WHITE);
x = -1.0*X/UnitLength;
//temp1返回面板的实际坐标值(以像素为单位)
y = Fun(x);
temp1 = new Point2D.Double(this.alterX(x * UnitLength), this.alterY(y * UnitLength));
for(int i = 0 ; i < width; i++){
x =x + 1.0/UnitLength;//前进一个像素
y = Fun(x);
if ( Math.abs(y) < Y){
temp2 = new Point2D.Double(this.alterX(x * UnitLength), this.alterY(y * UnitLength));
g.draw(new Line2D.Double(temp1, temp2));
temp1 = temp2;
}
}
//repaint();
}
//新坐标对应的原坐标
private double alterX(double x){
return x + X;
}
private double alterY(double y){
return -1 *( y - Y);
}
}
}
三、平面直角坐标系函数图像
源码文件UI.java和MyPanel.java组成
效果图如下:
提示:“输入函数”功能尚未实现!
源码文件UI.java 内容如下
package math;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class UI extends JFrame
{
MyPanel mp;
JPanel pl = new JPanel();
JPanel pl1 = new JPanel(),
pl2 = new JPanel(),
pl3 = new JPanel(),
pl4 = new JPanel();
JRadioButton rb1,rb2;
ButtonGroup bg = new ButtonGroup();
JTextField tf = new JTextField(16);
String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
JComboBox cb;
JButton bn1 = new JButton("变宽"),
bn2 = new JButton("变窄"),
bn3 = new JButton("拉长"),
bn4 = new JButton("压短"),
bn = new JButton("绘图"),
exit = new JButton("退出"),
bn5 = new JButton("左移"),
bn6 = new JButton("右移"),
bn7 = new JButton("上移"),
bn8 = new JButton("下移");
public UI()
{
mp = new MyPanel(this);
pl1.setLayout(new GridLayout(1, 2));
pl2.setLayout(new GridLayout(1, 2));
pl3.setLayout(new GridLayout(1, 2));
pl4.setLayout(new GridLayout(1, 2));
pl1.add(bn1); bn1.setEnabled(false);
pl1.add(bn2); bn2.setEnabled(false);
pl2.add(bn3); bn3.setEnabled(false);
pl2.add(bn4); bn4.setEnabled(false);
pl3.add(bn5); bn5.setEnabled(false);
pl3.add(bn6); bn6.setEnabled(false);
pl4.add(bn7); bn7.setEnabled(false);
pl4.add(bn8); bn8.setEnabled(false);
pl.setLayout(new GridLayout(20, 1));
rb1 = new JRadioButton("输入函数");
rb2 = new JRadioButton("选择已有函数");
rb2.setSelected(true);
tf.setEnabled(false);
bg.add(rb1);
bg.add(rb2);
rb1.addActionListener(mp);
rb2.addActionListener(mp);
pl.add(rb1);
pl.add(tf);
pl.add(rb2);
cb = new JComboBox(s);
pl.add(cb);
pl.add(new JLabel());
pl.add(pl1); pl.add(pl2);
pl.add(pl3); pl.add(pl4);
pl.add(bn);
pl.add(exit);
bn1.addActionListener(mp);
bn2.addActionListener(mp);
bn3.addActionListener(mp);
bn4.addActionListener(mp);
bn5.addActionListener(mp);
bn6.addActionListener(mp);
bn7.addActionListener(mp);
bn8.addActionListener(mp);
bn.addActionListener(mp);
exit.addActionListener(mp);
this.setLayout(new BorderLayout());
this.add(mp, BorderLayout.CENTER);
this.add(pl, BorderLayout.EAST);
this.setTitle("平面直角坐标系函数图像");
this.setSize(797, 600 + 37);
Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new UI();
}
}
源码文件MyPanel.java 内容如下
package math;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
{
UI ui;
int flag;
double h_times;
int w_times;
int dx;
int dy;
String str;
Point pt = new Point(0, 0);
void init()
{
flag = -1;
h_times = Math.PI / 100;
w_times = 100;
dx = 300;
dy = 300;
}
public MyPanel(UI ui)
{
this.addMouseMotionListener(this);
init();
this.ui = ui;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawCoordinate(g2);
Line2D line;
g2.setColor(Color.BLUE);
g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
switch(flag)
{
case 0:
g2.drawString("y = Asin(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 1:
g2.drawString("y = Acos(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 2:
g2.drawString("y = Atan(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 3:
g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
g2.draw(line);
}
break;
case 4:
g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
g2.draw(line);
}
break;
case 5:
g2.drawString("y = Alog(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 6:
g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 7:
g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 8:
g2.drawString("y = a(sita)", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
}
if(flag != -1)
{
g2.drawString("A = " + w_times, 105, 90);
g2.drawString("B= " + h_times, 105, 120);
g2.drawString("C= " + (300 - dx), 105, 150);
g2.drawString("D= " + (300 - dy), 105, 180);
}
}
private double getReal_X(double x)
{
return (x - dx) * h_times;
}
private void drawCoordinate(Graphics2D g2)
{
int len = 20;
Line2D line;
for(int i = 0; i <= 600 / len; i++)
{
g2.setColor(Color.PINK.darker());
if(i == 300 / len)
g2.setColor(Color.RED);
else;
line = new Line2D.Double(0, i * len, 600, i * len);
g2.draw(line);
line = new Line2D.Double(i * len, 0, i * len, 600);
g2.draw(line);
}
drawPoint(g2, 300, 300);
}
private void drawPoint(Graphics2D g2, double x, double y)
{
g2.setColor(Color.YELLOW);
Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
g2.fill(circle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ui.rb1)
{
ui.tf.setEnabled(true);
ui.cb.setEnabled(false);
flag = -1;
}
if(e.getSource() == ui.rb2)
{
ui.tf.setEnabled(false);
ui.cb.setEnabled(true);
}
if(e.getSource() == ui.bn1)
{
h_times /= 1.1;
}
if(e.getSource() == ui.bn2)
{
h_times *= 1.1;
}
if(e.getSource() == ui.bn3)
{
// ui.bn4.setEnabled(true);
w_times += 10;
// if(w_times >= 300)
// ui.bn3.setEnabled(false);
}
if(e.getSource() == ui.bn4)
{
// ui.bn3.setEnabled(true);
w_times -= 10;
// if(w_times <= 0)
// ui.bn4.setEnabled(false);
}
if(e.getSource() == ui.bn5)
{
dx -= 10;
}
if(e.getSource() == ui.bn6)
{
dx += 10;
}
if(e.getSource() == ui.bn7)
{
// ui.bn8.setEnabled(true);
dy -= 10;
// if(dy <= 0)
// ui.bn7.setEnabled(false);
}
if(e.getSource() == ui.bn8)
{
// ui.bn7.setEnabled(true);
dy += 10;
// if(dy >= 600)
// ui.bn8.setEnabled(false);
}
if(e.getSource() == ui.bn)
{
if(ui.tf.isEnabled() == true)
{
str = ui.tf.getText();
if(str == null || str.length() == 0)
{
ui.bn1.setEnabled(false);
ui.bn2.setEnabled(false);
ui.bn3.setEnabled(false);
ui.bn4.setEnabled(false);
ui.bn5.setEnabled(false);
ui.bn6.setEnabled(false);
ui.bn7.setEnabled(false);
ui.bn8.setEnabled(false);
JOptionPane.showMessageDialog(this, "请输入函数表达式 !");
return;
}else
JOptionPane.showMessageDialog(this, "本功能尚未实现 !");
}else
flag = -2;
ui.bn1.setEnabled(true);
ui.bn2.setEnabled(true);
ui.bn3.setEnabled(true);
ui.bn4.setEnabled(true);
ui.bn5.setEnabled(true);
ui.bn6.setEnabled(true);
ui.bn7.setEnabled(true);
ui.bn8.setEnabled(true);
init();
if(ui.cb.isEnabled() == true)
{
flag = ui.cb.getSelectedIndex();
}
}
if(e.getSource() == ui.exit)
System.exit(0);
repaint();
}
public void mouseDragged(MouseEvent arg0)
{
}
public void mouseMoved(MouseEvent e)
{
pt = e.getPoint();
repaint();
}
}
OK!